/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tools/brzflakes.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-15 22:21:42 UTC
  • mfrom: (7143.1.3 flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181115222142-6g6sy4fw6yr1mkw1
Add a tool that monkey patches pyflakes to support lazy imports.

Merged from https://code.launchpad.net/~jelmer/brz/flake8/+merge/358593

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
from __future__ import absolute_import
 
4
 
 
5
import ast
 
6
 
 
7
from pyflakes.checker import Checker, ImportationFrom
 
8
 
 
9
# Do some monkey patching..
 
10
def CALL(self, node):
 
11
    if isinstance(node.func, ast.Name) and node.func.id == 'lazy_import':
 
12
        self.LAZY_IMPORT(node)
 
13
    elif (isinstance(node.func, ast.Attribute) and
 
14
            node.func.attr == 'lazy_import' and
 
15
            node.func.value.id == 'lazy_import'):
 
16
        self.LAZY_IMPORT(node)
 
17
    return self.handleChildren(node)
 
18
 
 
19
Checker.CALL = CALL
 
20
 
 
21
def LAZY_IMPORT(self, node):
 
22
    from breezy.lazy_import import ImportProcessor
 
23
    processor = ImportProcessor()
 
24
    if not isinstance(node.args[1], ast.Str):
 
25
        # Not sure how to deal with this..
 
26
        return
 
27
    import_text = node.args[1].s
 
28
    scope = {}
 
29
    processor.lazy_import(scope, import_text)
 
30
    for name, (path, sub, scope) in processor.imports.items():
 
31
        importation = ImportationFrom(name, node, '.'.join(path), scope)
 
32
        self.addBinding(node, importation)
 
33
 
 
34
Checker.LAZY_IMPORT = LAZY_IMPORT
 
35
 
 
36
 
 
37
if __name__ == '__main__':
 
38
    from pyflakes import __main__
 
39
    __main__.main()