|
1 """https://pastebin.logilab.fr/show/860/""" |
|
2 |
|
3 from logilab.astng import MANAGER, nodes, scoped_nodes |
|
4 |
|
5 def turn_function_to_class(node): |
|
6 """turn a Function node into a Class node (in-place)""" |
|
7 node.__class__ = scoped_nodes.Class |
|
8 node.bases = () |
|
9 # remove return nodes so that we don't get warned about 'return outside |
|
10 # function' by pylint |
|
11 for rnode in node.nodes_of_class(nodes.Return): |
|
12 rnode.parent.body.remove(rnode) |
|
13 # that seems to be enough :) |
|
14 |
|
15 |
|
16 def cubicweb_transform(module): |
|
17 # handle objectify_selector decorator. Only look at module level functions, |
|
18 # should be enough |
|
19 for assnodes in module.locals.values(): |
|
20 for node in assnodes: |
|
21 if isinstance(node, scoped_nodes.Function) and node.decorators: |
|
22 for decorator in node.decorators.nodes: |
|
23 for infered in decorator.infer(): |
|
24 if infered.name == 'objectify_selector': |
|
25 turn_function_to_class(node) |
|
26 break |
|
27 else: |
|
28 continue |
|
29 break |
|
30 # add yams base types into 'yams.buildobjs', astng doesn't grasp globals() |
|
31 # magic in there |
|
32 if module.name == 'yams.buildobjs': |
|
33 from yams import BASE_TYPES |
|
34 for etype in BASE_TYPES: |
|
35 module.locals[etype] = [scoped_nodes.Class(etype, None)] |
|
36 |
|
37 MANAGER.register_transformer(cubicweb_transform) |
|
38 |
|
39 def register(linter): |
|
40 """called when loaded by pylint --load-plugins, nothing to do here""" |
|
41 |