[PATCH 1 of 4 V2] import-checker: add utility to examine what module is imported easily

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri May 15 09:13:45 CDT 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1431699038 -32400
#      Fri May 15 23:10:38 2015 +0900
# Node ID d7a3d39a03eccc94a164ba44704452d7233c71d5
# Parent  d1bd0fd07ee6adf4ab3be2b0a0a7c0df54d55abf
import-checker: add utility to examine what module is imported easily

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -26,6 +26,58 @@ def dotted_name_of_path(path, trimpure=F
         return '.'.join(p for p in parts if p != 'pure')
     return '.'.join(parts)
 
+def fromlocalfunc(modulename, localmods):
+    """Get a function to examine whether specified name causes
+    importing locally defined one
+
+    Returned function takes `name` argument, and returns `(absname,
+    dottedpath, hassubmod)` tuple if `name` matches against locally
+    defined module. Otherwise, it returns False.
+
+    `absname` is absolute module name of `name`. This can be used to
+    compose prefix for sub items or so.
+
+    `dottedpath` is `dotted_name_of_path()`-ed source file path. This
+    may have `.__init__` at tne end of it. This is used to follow
+    import chain.
+
+    `hassubmod` is whether it may have sub modules under it (for
+    convenient, even though this is also equivalent to "absname !=
+    dottednpath")
+
+    >>> localmods = {'foo.__init__': True, 'foo.foo1': True,
+    ...              'foo.bar.__init__': True, 'foo.bar.bar1': True,
+    ...              'baz.__init__': True, 'baz.baz1': True }
+    >>> fromlocal = fromlocalfunc('foo.xxx', localmods)
+    >>> # relative
+    >>> fromlocal('foo1')
+    ('foo.foo1', 'foo.foo1', False)
+    >>> fromlocal('bar')
+    ('foo.bar', 'foo.bar.__init__', True)
+    >>> fromlocal('bar.bar1')
+    ('foo.bar.bar1', 'foo.bar.bar1', False)
+    >>> # absolute
+    >>> fromlocal('baz')
+    ('baz', 'baz.__init__', True)
+    >>> fromlocal('baz.baz1')
+    ('baz.baz1', 'baz.baz1', False)
+    >>> # unknown = maybe standard library
+    >>> fromlocal('os')
+    False
+    """
+    prefix = '.'.join(modulename.split('.')[:-1])
+    if prefix:
+        prefix += '.'
+    def fromlocal(name):
+        # check relative name at first
+        for n in prefix + name, name:
+            if n in localmods:
+                return (n, n, False)
+            dottedpath = n + '.__init__'
+            if dottedpath in localmods:
+                return (n, dottedpath, True)
+        return False
+    return fromlocal
 
 def list_stdlib_modules():
     """List the modules present in the stdlib.


More information about the Mercurial-devel mailing list