1# -*- coding: utf-8 -*-
2#
3#  utils.py - commander
4#
5#  Copyright (C) 2010 - Jesse van den Kieboom
6#
7#  This program is free software; you can redistribute it and/or modify
8#  it under the terms of the GNU General Public License as published by
9#  the Free Software Foundation; either version 2 of the License, or
10#  (at your option) any later version.
11#
12#  This program is distributed in the hope that it will be useful,
13#  but WITHOUT ANY WARRANTY; without even the implied warranty of
14#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#  GNU General Public License for more details.
16#
17#  You should have received a copy of the GNU General Public License
18#  along with this program; if not, write to the Free Software
19#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
20#  Boston, MA 02110-1301, USA.
21
22import os
23import types
24import inspect
25import sys
26
27class Struct(dict):
28    def __getattr__(self, name):
29        if not name in self:
30            val = super(Struct, self).__getattr__(self, name)
31        else:
32            val = self[name]
33
34        return val
35
36    def __setattr__(self, name, value):
37        if not name in self:
38            super(Struct, self).__setattr__(self, name, value)
39        else:
40            self[name] = value
41
42    def __delattr__(self, name):
43        del self[name]
44
45def is_commander_module(mod):
46    if type(mod) == types.ModuleType:
47        return mod and ('__commander_module__' in mod.__dict__)
48    else:
49        mod = str(mod)
50        return mod.endswith('.py') or (os.path.isdir(mod) and os.path.isfile(os.path.join(mod, '__init__.py')))
51
52def getargspec(func):
53    ret = inspect.getargspec(func)
54
55    # Before 2.6 this was just a normal tuple, we don't want that
56    if sys.version_info < (2, 6):
57        ret = Struct({
58            'args': ret[0],
59            'varargs': ret[1],
60            'keywords': ret[2],
61            'defaults': ret[3]
62        })
63
64    return ret
65
66# ex:ts=4:et
67