OLD | NEW |
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of astroid. | 4 # This file is part of astroid. |
5 # | 5 # |
6 # astroid is free software: you can redistribute it and/or modify it | 6 # astroid is free software: you can redistribute it and/or modify it |
7 # under the terms of the GNU Lesser General Public License as published by the | 7 # under the terms of the GNU Lesser General Public License as published by the |
8 # Free Software Foundation, either version 2.1 of the License, or (at your | 8 # Free Software Foundation, either version 2.1 of the License, or (at your |
9 # option) any later version. | 9 # option) any later version. |
10 # | 10 # |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 class AsStringRegexpPredicate(object): | 73 class AsStringRegexpPredicate(object): |
74 """Class to be used as predicate that may be given to `register_transform` | 74 """Class to be used as predicate that may be given to `register_transform` |
75 | 75 |
76 First argument is a regular expression that will be searched against the `as
_string` | 76 First argument is a regular expression that will be searched against the `as
_string` |
77 representation of the node onto which it's applied. | 77 representation of the node onto which it's applied. |
78 | 78 |
79 If specified, the second argument is an `attrgetter` expression that will be | 79 If specified, the second argument is an `attrgetter` expression that will be |
80 applied on the node first to get the actual node on which `as_string` should | 80 applied on the node first to get the actual node on which `as_string` should |
81 be called. | 81 be called. |
| 82 |
| 83 WARNING: This can be fairly slow, as it has to convert every AST node back |
| 84 to Python code; you should consider examining the AST directly instead. |
82 """ | 85 """ |
83 def __init__(self, regexp, expression=None): | 86 def __init__(self, regexp, expression=None): |
84 self.regexp = re.compile(regexp) | 87 self.regexp = re.compile(regexp) |
85 self.expression = expression | 88 self.expression = expression |
86 | 89 |
87 def __call__(self, node): | 90 def __call__(self, node): |
88 if self.expression is not None: | 91 if self.expression is not None: |
89 node = attrgetter(self.expression)(node) | 92 node = attrgetter(self.expression)(node) |
90 return self.regexp.search(node.as_string()) | 93 return self.regexp.search(node.as_string()) |
91 | 94 |
92 def inference_tip(infer_function): | 95 def inference_tip(infer_function): |
93 """Given an instance specific inference function, return a function to be | 96 """Given an instance specific inference function, return a function to be |
94 given to MANAGER.register_transform to set this inference function. | 97 given to MANAGER.register_transform to set this inference function. |
95 | 98 |
96 Typical usage | 99 Typical usage |
97 | 100 |
98 .. sourcecode:: python | 101 .. sourcecode:: python |
99 | 102 |
100 MANAGER.register_transform(CallFunc, inference_tip(infer_named_tuple), | 103 MANAGER.register_transform(CallFunc, inference_tip(infer_named_tuple), |
101 AsStringRegexpPredicate('namedtuple', 'func')) | 104 predicate) |
102 """ | 105 """ |
103 def transform(node, infer_function=infer_function): | 106 def transform(node, infer_function=infer_function): |
104 node._explicit_inference = infer_function | 107 node._explicit_inference = infer_function |
105 return node | 108 return node |
106 return transform | 109 return transform |
107 | 110 |
| 111 |
| 112 def register_module_extender(manager, module_name, get_extension_mod): |
| 113 def transform(node): |
| 114 extension_module = get_extension_mod() |
| 115 for name, obj in extension_module.locals.items(): |
| 116 node.locals[name] = obj |
| 117 |
| 118 manager.register_transform(Module, transform, lambda n: n.name == module_nam
e) |
| 119 |
| 120 |
108 # load brain plugins | 121 # load brain plugins |
109 from os import listdir | 122 from os import listdir |
110 from os.path import join, dirname | 123 from os.path import join, dirname |
111 BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') | 124 BRAIN_MODULES_DIR = join(dirname(__file__), 'brain') |
112 if BRAIN_MODULES_DIR not in sys.path: | 125 if BRAIN_MODULES_DIR not in sys.path: |
113 # add it to the end of the list so user path take precedence | 126 # add it to the end of the list so user path take precedence |
114 sys.path.append(BRAIN_MODULES_DIR) | 127 sys.path.append(BRAIN_MODULES_DIR) |
115 # load modules in this directory | 128 # load modules in this directory |
116 for module in listdir(BRAIN_MODULES_DIR): | 129 for module in listdir(BRAIN_MODULES_DIR): |
117 if module.endswith('.py'): | 130 if module.endswith('.py'): |
118 __import__(module[:-3]) | 131 __import__(module[:-3]) |
OLD | NEW |