comparison mercurial/packagescan.py @ 2981:0171a5432621

Support the demandload syntax "@" in packagescan
author Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
date Thu, 10 Aug 2006 16:59:58 +0200
parents c4325f0a9b91
children 799811087044
comparison
equal deleted inserted replaced
2830:49988d9f0758 2981:0171a5432621
24 def demandload(scope, modules): 24 def demandload(scope, modules):
25 """ fake demandload function that collects the required modules 25 """ fake demandload function that collects the required modules
26 foo import foo 26 foo import foo
27 foo bar import foo, bar 27 foo bar import foo, bar
28 foo.bar import foo.bar 28 foo.bar import foo.bar
29 foo@bar import foo as bar
29 foo:bar from foo import bar 30 foo:bar from foo import bar
30 foo:bar,quux from foo import bar, quux 31 foo:bar,quux from foo import bar, quux
31 foo.bar:quux from foo.bar import quux""" 32 foo.bar:quux from foo.bar import quux"""
32 33
33 for m in modules.split(): 34 for m in modules.split():
36 module, fromlist = m.split(':') 37 module, fromlist = m.split(':')
37 fromlist = fromlist.split(',') 38 fromlist = fromlist.split(',')
38 except: 39 except:
39 module = m 40 module = m
40 fromlist = [] 41 fromlist = []
42 as = None
43 if '@' in module:
44 module, as = module.split("@")
41 mod = __import__(module, scope, scope, fromlist) 45 mod = __import__(module, scope, scope, fromlist)
42 if fromlist == []: 46 if fromlist == []:
43 # mod is only the top package, but we need all packages 47 # mod is only the top package, but we need all packages
44 comp = module.split('.') 48 comp = module.split('.')
45 i = 1 49 i = 1
46 mn = comp[0] 50 mn = comp[0]
47 while True: 51 while True:
48 # mn and mod.__name__ might not be the same 52 # mn and mod.__name__ might not be the same
49 scope[mn] = mod 53 if not as:
54 as = mn
55 scope[as] = mod
50 requiredmodules[mod.__name__] = 1 56 requiredmodules[mod.__name__] = 1
51 if len(comp) == i: break 57 if len(comp) == i: break
52 mod = getattr(mod,comp[i]) 58 mod = getattr(mod,comp[i])
53 mn = string.join(comp[:i+1],'.') 59 mn = string.join(comp[:i+1],'.')
54 i += 1 60 i += 1