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)
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)
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)
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