~ubuntu-branches/ubuntu/raring/gedit-plugins/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/commander/commander/commands/module.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-07-22 20:45:45 UTC
  • mfrom: (1.1.26 upstream)
  • mto: (7.3.1 sid) (1.4.8)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20110722204545-15t6eui58z5tdb6l
Tags: upstream-3.0.5
ImportĀ upstreamĀ versionĀ 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#  module.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., 59 Temple Place, Suite 330,
 
20
#  Boston, MA 02111-1307, USA.
 
21
 
1
22
import sys
2
23
import os
3
24
import types
18
39
 
19
40
                if type(mod) == types.ModuleType:
20
41
                        self.mod = mod
21
 
                        
 
42
 
22
43
                        if '__default__' in mod.__dict__:
23
44
                                self.method = mod.__dict__['__default__']
24
45
                        else:
27
48
                        self.mod = None
28
49
                        self._dirname = mod
29
50
                        self._rollback = rollbackimporter.RollbackImporter()
30
 
        
 
51
 
31
52
        def commands(self):
32
53
                if self._commands == None:
33
54
                        self.scan_commands()
36
57
 
37
58
        def clear(self):
38
59
                self._commands = None
39
 
        
 
60
 
40
61
        def roots(self):
41
62
                if self._roots == None:
42
63
                        if not self.mod:
43
64
                                return []
44
 
                
 
65
 
45
66
                        dic = self.mod.__dict__
46
 
                
 
67
 
47
68
                        if '__root__' in dic:
48
69
                                root = dic['__root__']
49
70
                        else:
50
71
                                root = []
51
 
                
 
72
 
52
73
                        root = filter(lambda x: x in dic and type(dic[x]) == types.FunctionType, root)
53
74
                        self._roots = map(lambda x: method.Method(dic[x], x, self.mod), root)
54
 
                
 
75
 
55
76
                return self._roots
56
 
        
 
77
 
57
78
        def scan_commands(self):
58
79
                self._commands = []
59
 
                
 
80
 
60
81
                if self.mod == None:
61
82
                        return
62
83
 
70
91
                for k in dic:
71
92
                        if k.startswith('_') or k in root:
72
93
                                continue
73
 
                        
 
94
 
74
95
                        item = dic[k]
75
 
                        
 
96
 
76
97
                        if type(item) == types.FunctionType:
77
98
                                bisect.insort(self._commands, method.Method(item, k, self))
78
99
                        elif type(item) == types.ModuleType and utils.is_commander_module(item):
79
100
                                mod = Module(k, item, self)
80
101
                                bisect.insort(self._commands, mod)
81
 
                                
 
102
 
82
103
                                # Insert root functions into this module
83
104
                                for r in mod.roots():
84
105
                                        bisect.insert(self._commands, r)
85
 
        
 
106
 
86
107
        def unload(self):
87
108
                self._commands = None
88
109
 
89
110
                if not self._dirname:
90
111
                        return False
91
 
                
 
112
 
92
113
                self._rollback.uninstall()
93
114
                self.mod = None
94
115
 
95
116
                return True
96
 
        
 
117
 
97
118
        def reload(self):
98
119
                if not self.unload():
99
120
                        return
105
126
 
106
127
                try:
107
128
                        sys.path.insert(0, self._dirname)
108
 
                        
 
129
 
109
130
                        self._rollback.monitor()
110
131
                        self.mod = __import__(self.name, globals(), locals(), [], 0)
111
132
                        self._rollback.cancel()
112
 
                        
 
133
 
113
134
                        if not utils.is_commander_module(self.mod):
114
135
                                raise Exception('Module is not a commander module...')
115
 
                        
 
136
 
116
137
                        if '__default__' in self.mod.__dict__:
117
138
                                self.method = self.mod.__dict__['__default__']
118
139
                        else:
122
143
                except:
123
144
                        sys.path = oldpath
124
145
                        self._rollback.uninstall()
125
 
                        
 
146
 
126
147
                        if self.name in sys.modules:
127
148
                                del sys.modules[self.name]
128
149
                        raise
129
 
                
 
150
 
130
151
                sys.path = oldpath
131
152