~washort/exocet/katamari

« back to all changes in this revision

Viewing changes to exocet/_exocet.py

  • Committer: Allen Short
  • Date: 2011-08-06 19:18:16 UTC
  • mfrom: (22.1.3 exocet)
  • Revision ID: washort@divmod.com-20110806191816-ael3ak60vlte61ps
Merge a patch from Corbin Simpson.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from zope.interface import Interface, implements
9
9
 
10
10
DEBUG = False
11
 
_sysModulesSpecialCases = {"os": ['path']}
 
11
_sysModulesSpecialCases = {
 
12
    "os": ['path'],
 
13
    "twisted.internet": ["reactor"],
 
14
}
12
15
 
13
16
def trace(*args):
14
17
    if DEBUG:
156
159
 
157
160
 
158
161
 
 
162
class ExclusiveMapper(object):
 
163
    """
 
164
    A mapper that wraps another mapper, but excludes certain names.
 
165
 
 
166
    This mapper can be used to implement a blacklist.
 
167
    """
 
168
 
 
169
    implements(IMapper)
 
170
 
 
171
    def __init__(self, submapper, excluded):
 
172
        self._submapper = submapper
 
173
        self._excluded = excluded
 
174
 
 
175
 
 
176
    def lookup(self, name):
 
177
        """
 
178
        @see l{Imapper.lookup}
 
179
        """
 
180
 
 
181
        if name in self._excluded:
 
182
            raise ImportError("Module %s blacklisted in mapper %s"
 
183
                % (name, self))
 
184
        return self._submapper.lookup(name)
 
185
 
 
186
 
 
187
    def contains(self, name):
 
188
        """
 
189
        @see l{Imapper.contains}
 
190
        """
 
191
 
 
192
        if name in self._excluded:
 
193
            return False
 
194
        return self._submapper.contains(name)
 
195
 
 
196
 
 
197
    def withOverrides(self, overrides):
 
198
        """
 
199
        @see L{IMapper.withOverrides}
 
200
        """
 
201
        return _StackedMapper([DictMapper(overrides), self])
 
202
 
 
203
 
 
204
 
159
205
class _PackageMapper(CallableMapper):
160
206
    """
161
207
    A mapper that allows direct mutation as a result of intrapackage
477
523
                                                    self.mapper)
478
524
                    setattr(parent, seg, new)
479
525
                    parent = new
480
 
            setattr(parent, modulePath[-1], initModule)
 
526
                setattr(parent, modulePath[-1], initModule)
481
527
        self.allModules  = analyzePackage(self.packageMaker)
482
528
        sortedModules = robust_topological_sort(self.allModules)
483
529
        self.loadOrder = list(itertools.chain(*sortedModules))