##// END OF EJS Templates
import-checker: add utility to examine what module is imported easily...
FUJIWARA Katsunori -
r25173:7358b5d9 default
parent child Browse files
Show More
@@ -26,6 +26,72 b' def dotted_name_of_path(path, trimpure=F'
26 return '.'.join(p for p in parts if p != 'pure')
26 return '.'.join(p for p in parts if p != 'pure')
27 return '.'.join(parts)
27 return '.'.join(parts)
28
28
29 def fromlocalfunc(modulename, localmods):
30 """Get a function to examine which locally defined module the
31 target source imports via a specified name.
32
33 `modulename` is an `dotted_name_of_path()`-ed source file path,
34 which may have `.__init__` at the end of it, of the target source.
35
36 `localmods` is a dict (or set), of which key is an absolute
37 `dotted_name_of_path()`-ed source file path of locally defined (=
38 Mercurial specific) modules.
39
40 This function assumes that module names not existing in
41 `localmods` are ones of Python standard libarary.
42
43 This function returns the function, which takes `name` argument,
44 and returns `(absname, dottedpath, hassubmod)` tuple if `name`
45 matches against locally defined module. Otherwise, it returns
46 False.
47
48 It is assumed that `name` doesn't have `.__init__`.
49
50 `absname` is an absolute module name of specified `name`
51 (e.g. "hgext.convert"). This can be used to compose prefix for sub
52 modules or so.
53
54 `dottedpath` is a `dotted_name_of_path()`-ed source file path
55 (e.g. "hgext.convert.__init__") of `name`. This is used to look
56 module up in `localmods` again.
57
58 `hassubmod` is whether it may have sub modules under it (for
59 convenient, even though this is also equivalent to "absname !=
60 dottednpath")
61
62 >>> localmods = {'foo.__init__': True, 'foo.foo1': True,
63 ... 'foo.bar.__init__': True, 'foo.bar.bar1': True,
64 ... 'baz.__init__': True, 'baz.baz1': True }
65 >>> fromlocal = fromlocalfunc('foo.xxx', localmods)
66 >>> # relative
67 >>> fromlocal('foo1')
68 ('foo.foo1', 'foo.foo1', False)
69 >>> fromlocal('bar')
70 ('foo.bar', 'foo.bar.__init__', True)
71 >>> fromlocal('bar.bar1')
72 ('foo.bar.bar1', 'foo.bar.bar1', False)
73 >>> # absolute
74 >>> fromlocal('baz')
75 ('baz', 'baz.__init__', True)
76 >>> fromlocal('baz.baz1')
77 ('baz.baz1', 'baz.baz1', False)
78 >>> # unknown = maybe standard library
79 >>> fromlocal('os')
80 False
81 """
82 prefix = '.'.join(modulename.split('.')[:-1])
83 if prefix:
84 prefix += '.'
85 def fromlocal(name):
86 # check relative name at first
87 for n in prefix + name, name:
88 if n in localmods:
89 return (n, n, False)
90 dottedpath = n + '.__init__'
91 if dottedpath in localmods:
92 return (n, dottedpath, True)
93 return False
94 return fromlocal
29
95
30 def list_stdlib_modules():
96 def list_stdlib_modules():
31 """List the modules present in the stdlib.
97 """List the modules present in the stdlib.
General Comments 0
You need to be logged in to leave comments. Login now