include basic stdlib description from the pylint-brain project (https://bitbucket.org/sthenault/pylint-brain). Closes #100013

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changesete907d421c802
branchdefault
phasepublic
hiddenno
parent revision#80c265e655cf Fix raw_building.object_build for pypy implementation. Closes #99583
child revision#11f8d29b8ac0 0.24
files modified by this revision
ChangeLog
__init__.py
brain/py2stdlib.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1342537115 -7200
# Tue Jul 17 16:58:35 2012 +0200
# Node ID e907d421c8025baa23529c3dc9f0691a60c1459f
# Parent 80c265e655cfbe09fe302e87f74bddd1ac97e0be
include basic stdlib description from the pylint-brain project (https://bitbucket.org/sthenault/pylint-brain). Closes #100013

diff --git a/ChangeLog b/ChangeLog
@@ -1,10 +1,14 @@
1  Change log for the astng package
2  ================================
3 
4  --
5 +    * include pylint brain extension, describing some stuff not properly understood until then.
6 +      (#100013, #53049, #23986, #72355)
7 +
8      * #99583: fix raw_building.object_build for pypy implementation
9 +
10      * use `open` rather than `file` in scoped_nodes as 2to3 miss it
11 
12  2011-12-08  --  0.23.1
13      * #62295: avoid "OSError: Too many open files" by moving
14        .file_stream as a Module property opening the file only when needed
diff --git a/__init__.py b/__init__.py
@@ -1,6 +1,6 @@
15 -# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
16 +# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
17  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
18  # copyright 2003-2010 Sylvain Thenault, all rights reserved.
19  # contact mailto:thenault@gmail.com
20  #
21  # This file is part of logilab-astng.
@@ -69,5 +69,17 @@
22  # make a manager instance (borg) as well as Project and Package classes
23  # accessible from astng package
24  from logilab.astng.manager import ASTNGManager, Project
25  MANAGER = ASTNGManager()
26  del ASTNGManager
27 +
28 +# load brain plugins
29 +from os import listdir
30 +from os.path import join, dirname
31 +BRAIN_MODULES_DIR = join(dirname(__file__), 'brain')
32 +if BRAIN_MODULES_DIR not in sys.path:
33 +    # add it to the end of the list so user path take precedence
34 +    sys.path.append(BRAIN_MODULES_DIR)
35 +# load modules in this directory
36 +for module in listdir(BRAIN_MODULES_DIR):
37 +    if module.endswith('.py'):
38 +        __import__(module[:-3])
diff --git a/brain/py2stdlib.py b/brain/py2stdlib.py
@@ -0,0 +1,119 @@
39 +"""ASTNG hooks for the Python 2 standard library.
40 +
41 +Currently help understanding of :
42 +
43 +* hashlib.md5 and hashlib.sha1
44 +"""
45 +
46 +from logilab.astng import MANAGER
47 +from logilab.astng.builder import ASTNGBuilder
48 +
49 +MODULE_TRANSFORMS = {}
50 +
51 +def hashlib_transform(module):
52 +    fake = ASTNGBuilder(MANAGER).string_build('''
53 +
54 +class md5(object):
55 +  def __init__(self, value): pass
56 +  def hexdigest(self):
57 +    return u''
58 +
59 +class sha1(object):
60 +  def __init__(self, value): pass
61 +  def hexdigest(self):
62 +    return u''
63 +
64 +''')
65 +    for hashfunc in ('sha1', 'md5'):
66 +        module.locals[hashfunc] = fake.locals[hashfunc]
67 +
68 +def collections_transform(module):
69 +    fake = ASTNGBuilder(MANAGER).string_build('''
70 +
71 +class defaultdict(dict):
72 +    default_factory = None
73 +    def __missing__(self, key): pass
74 +
75 +class deque(object):
76 +    maxlen = 0
77 +    def __init__(iterable=None, maxlen=None): pass
78 +    def append(self, x): pass
79 +    def appendleft(self, x): pass
80 +    def clear(self): pass
81 +    def count(self, x): return 0
82 +    def extend(self, iterable): pass
83 +    def extendleft(self, iterable): pass
84 +    def pop(self): pass
85 +    def popleft(self): pass
86 +    def remove(self, value): pass
87 +    def reverse(self): pass
88 +    def rotate(self, n): pass
89 +
90 +''')
91 +
92 +    for klass in ('deque', 'defaultdict'):
93 +        module.locals[klass] = fake.locals[klass]
94 +
95 +def pkg_resources_transform(module):
96 +    fake = ASTNGBuilder(MANAGER).string_build('''
97 +
98 +def resource_exists(package_or_requirement, resource_name):
99 +    pass
100 +
101 +def resource_isdir(package_or_requirement, resource_name):
102 +    pass
103 +
104 +def resource_filename(package_or_requirement, resource_name):
105 +    pass
106 +
107 +def resource_stream(package_or_requirement, resource_name):
108 +    pass
109 +
110 +def resource_string(package_or_requirement, resource_name):
111 +    pass
112 +
113 +def resource_listdir(package_or_requirement, resource_name):
114 +    pass
115 +
116 +def extraction_error():
117 +    pass
118 +
119 +def get_cache_path(archive_name, names=()):
120 +    pass
121 +
122 +def postprocess(tempname, filename):
123 +    pass
124 +
125 +def set_extraction_path(path):
126 +    pass
127 +
128 +def cleanup_resources(force=False):
129 +    pass
130 +
131 +''')
132 +
133 +    for func_name, func in fake.locals.items():
134 +        module.locals[func_name] = func
135 +
136 +    # for func in ('resource_exists', 'resource_isdir', 'resource_filename',
137 +    #     'resource_stream', 'resource_string', 'resource_listdir',
138 +    #     'extraction_error', 'get_cache_path', 'postprocess',
139 +    #     'set_extraction_path', 'cleanup_resources'):
140 +
141 +    #     module.locals[func] = fake.locals[func]
142 +
143 +MODULE_TRANSFORMS['hashlib'] = hashlib_transform
144 +MODULE_TRANSFORMS['collections'] = collections_transform
145 +MODULE_TRANSFORMS['pkg_resources'] = pkg_resources_transform
146 +
147 +
148 +def transform(module):
149 +    try:
150 +        tr = MODULE_TRANSFORMS[module.name]
151 +    except KeyError:
152 +        pass
153 +    else:
154 +        tr(module)
155 +
156 +from logilab.astng import MANAGER
157 +MANAGER.register_transformer(transform)