Example #1
0
 def testOnlyPackage(self):
     source = """
     class A(object):
         pass
     """
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.package = 'TestPackage'
     codeFinder.module = '__init__'
     compiler.walk(tree, codeFinder)
     expected = {
         'CLASSES': {
             'TestPackage.A':
             dict(docstring='',
                  bases=['object'],
                  constructor=[],
                  methods=[],
                  properties=[])
         },
         'FUNCTIONS': [],
         'CONSTANTS': [],
         'POINTERS': {},
         'HIERARCHY': ['TestPackage']
     }
     actual = eval(pformat(codeFinder.modules))
     self.assertEquals(actual, expected)
Example #2
0
 def getModule(self, source):
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.module = 'TestModule'
     codeFinder.package = 'TestPackage'
     compiler.walk(tree, codeFinder, walker=ExampleASTVisitor(), verbose=1)
     try:
         return eval(pformat(codeFinder.modules))
     except:
         print 'EXCEPTION WHEN EVALING:'
         print pformat(codeFinder.modules)
         print '=-' * 20
         raise
Example #3
0
 def getModule(self, source):
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.module = 'TestModule'
     codeFinder.package = 'TestPackage'
     compiler.walk(tree, codeFinder)
     try:
         return eval(pformat(codeFinder.modules))
     except:
         print('EXCEPTION WHEN EVALING:')
         print(pformat(codeFinder.modules))
         print('=-' * 20)
         raise
Example #4
0
 def testOnlyPackage(self):
     source = """
     class A(object):
         pass
     """
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.package = 'TestPackage'
     codeFinder.module = '__init__'
     compiler.walk(tree, codeFinder, walker=ExampleASTVisitor(), verbose=1)
     expected = {'CLASSES': {'TestPackage.A': dict(docstring='', bases=['object'], constructor=[], methods=[], properties=[])},
         'FUNCTIONS': [], 'CONSTANTS': [], 'POINTERS': {}, 'HIERARCHY': ['TestPackage']}
     actual = eval(pformat(codeFinder.modules))
     self.assertEquals(actual, expected)
Example #5
0
    def get_pysmell_code_walk_to_text(self, text):
        code = compiler.parse(text)

        class GlobalCodeFinder(CodeFinder):
            def visitFunction(self, func):
                self.enterScope(func)
                if self.inClassFunction:
                    if func.name != '__init__':
                        if func.decorators and 'property' in [
                                getName(n) for n in func.decorators
                        ]:
                            self.modules.addProperty(self.currentClass,
                                                     func.name)
                        else:
                            self.modules.addMethod(self.currentClass,
                                                   func.name,
                                                   getFuncArgs(func), func.doc
                                                   or "")
                    else:
                        self.modules.setConstructor(self.currentClass,
                                                    getFuncArgs(func))
                elif len(self.scope) == 1:
                    self.modules.addFunction(func.name,
                                             getFuncArgs(func, inClass=False),
                                             func.doc or "")

                #self.visit(func.code) Remove this line
                self.exitScope()

        if self.scope == SCOPE_GLOBAL:
            codefinder = GlobalCodeFinder()
        else:
            codefinder = CodeFinder()
        codefinder.modules = PyPleteModuleDict()
        return compiler.walk(code, codefinder)
Example #6
0
 def get_pysmell_code_walk_to_text(self, text):
     first_line = text.split("\n")[0]
     m = encoding_line.match(first_line)
     if m:
         encoding = m.groupdict().get('encoding', None)
         if encoding:
             if isinstance(text, str):
                 text = text.decode(encoding)
             text = text.encode(encoding)
     else:
         if isinstance(text, unicode):
             text = text.encode('utf-8')
     code = compiler.parse(text)
     codefinder = CodeFinder()
     codefinder.modules = PyPleteModuleDict()
     return compiler.walk(code, codefinder)
Example #7
0
    def testHierarchy(self):
        class MockNode(object):
            node = 1
        node = MockNode()
        codeFinder = CodeFinder()
        codeFinder.visit = lambda _: None

        codeFinder.package = 'TestPackage'
        codeFinder.module = '__init__'
        codeFinder.visitModule(node)

        codeFinder.module = 'Modulo'
        codeFinder.visitModule(node)

        codeFinder.package = 'TestPackage.Another'
        codeFinder.module = '__init__'
        codeFinder.visitModule(node)

        codeFinder.module = 'Moduli'
        codeFinder.visitModule(node)

        expected = [
            'TestPackage',
            'TestPackage.Modulo',
            'TestPackage.Another',
            'TestPackage.Another.Moduli',
        ]
        self.assertEquals(codeFinder.modules['HIERARCHY'], expected)
Example #8
0
    def testHierarchy(self):
        class MockNode(object):
            node = 1
        node = MockNode()
        codeFinder = CodeFinder()
        codeFinder.visit = lambda _: None

        codeFinder.package = 'TestPackage'
        codeFinder.module = '__init__'
        codeFinder.visitModule(node)

        codeFinder.module = 'Modulo'
        codeFinder.visitModule(node)

        codeFinder.package = 'TestPackage.Another'
        codeFinder.module = '__init__'
        codeFinder.visitModule(node)

        codeFinder.module = 'Moduli'
        codeFinder.visitModule(node)

        expected = [
            'TestPackage',
            'TestPackage.Modulo',
            'TestPackage.Another',
            'TestPackage.Another.Moduli',
        ]
        self.assertEqual(codeFinder.modules['HIERARCHY'], expected)