def testAnalyzeFile(self): path = os.path.abspath('File.py') source = dedent("""\ CONSTANT = 1 """) expectedDict = ModuleDict() expectedDict.enterModule('File') expectedDict.addProperty(None, 'CONSTANT') outDict = analyzeFile(path, getSafeTree(source, 1)) self.assertEquals(outDict, expectedDict, '%r != %r' % (outDict._modules, expectedDict._modules))
def process(filesOrDirectories, excluded, inputDict=None, verbose=False): """ Visit every package in ``filesOrDirectories`` and return a ModuleDict for everything, that can be used to generate a PYSMELLTAGS file. filesOrDirectories: list of paths to process. They can either be directories or files. Directories can either be packages or they can contain packages. excluded: list of directories to exclude (eg. ['test', '.svn']) inputDict: a ModuleDict instance to update with any new or updated python namespaces. verbose: flag that turns on verbose logging (print what is going on). returns: The generated ModuleDict instance for the directories provided in ``filesOrDirectories``. """ modules = ModuleDict() if inputDict: modules.update(inputDict) for rootPackage in filesOrDirectories: if os.path.isdir(rootPackage): for path, dirs, files in os.walk(rootPackage): for exc in excluded: if exc in dirs: if verbose: print 'removing', exc, 'in', path dirs.remove(exc) for f in files: if not f.endswith(".py"): continue #path here is relative, make it absolute absPath = os.path.abspath(path) if verbose: print 'processing', absPath, f newmodules = processFile(f, absPath) modules.update(newmodules) else: # single file filename = rootPackage absPath, filename = os.path.split(filename) if not absPath: absPath = os.path.abspath(".") else: absPath = os.path.abspath(absPath) #path here is absolute if verbose: print 'processing', absPath, filename newmodules = processFile(filename, absPath) modules.update(newmodules) return modules
def process(argList, excluded, output, verbose=False): modules = ModuleDict() for rootPackage in argList: if os.path.isdir(rootPackage): for path, dirs, files in os.walk(rootPackage): for exc in excluded: if exc in dirs: if verbose: print 'removing', exc, 'in', path dirs.remove(exc) if rootPackage == '.': rootPackage = os.path.split(os.getcwd())[-1] for f in files: if not f.endswith(".py"): continue #path here is relative, make it absolute absPath = os.path.abspath(path) if verbose: print 'processing', absPath, f newmodules = processFile(f, absPath, rootPackage) modules.update(newmodules) else: # single file filename = rootPackage absPath, filename = os.path.split(filename) rootPackageList = findRootPackageList(os.getcwd(), filename) if not absPath: absPath = os.path.abspath(".") else: absPath = os.path.abspath(absPath) #path here is absolute if verbose: print 'processing', absPath, filename newmodules = processFile(filename, absPath, rootPackageList[0]) modules.update(newmodules) generateClassTag(modules, output)
def get_dynamic_tags(module, visited_modules=None): if visited_modules == None: visited_modules = set() mod = __import__(module) print 'dynamic tags of', module visited_modules.add(module) attrs = [a for a in dir(mod) if not a.startswith('__')] pysmelld = ModuleDict() pysmelld['HIERARCHY'].append(module) for attr in attrs: o = getattr(mod, attr) if hasattr(o, '__module__') and o.__module__ != module: pysmelld['POINTERS']['%s.%s' % (module, attr)] = '%s.%s' % (o.__module__, attr) continue if inspect.isroutine(o): _add_function(mod, o, pysmelld) elif inspect.isclass(o): _add_class(o, pysmelld) elif inspect.ismodule(o) and attr in skip_modules and attr in visited_modules: pysmelld.update(get_dynamic_tags(o.__name__, visited_modules)) else: # Assume it's a constant. _add_constant(mod, attr, pysmelld) return pysmelld
def get_dynamic_tags(module, visited_modules=None): if visited_modules == None: visited_modules = set() mod = __import__(module) print('dynamic tags of', module) visited_modules.add(module) attrs = [a for a in dir(mod) if not a.startswith('__')] pysmelld = ModuleDict() pysmelld['HIERARCHY'].append(module) for attr in attrs: o = getattr(mod, attr) if hasattr(o, '__module__') and o.__module__ != module: pysmelld['POINTERS']['%s.%s' % (module, attr)] = '%s.%s' % (o.__module__, attr) continue if inspect.isroutine(o): _add_function(mod, o, pysmelld) elif inspect.isclass(o): _add_class(o, pysmelld) elif inspect.ismodule(o) and attr in skip_modules and attr in visited_modules: pysmelld.update(get_dynamic_tags(o.__name__, visited_modules)) else: # Assume it's a constant. _add_constant(mod, attr, pysmelld) return pysmelld
def testAnalyzeFile(self): path = os.path.abspath('File.py') source = dedent("""\ CONSTANT = 1 """) expectedDict = ModuleDict() expectedDict.enterModule('File') expectedDict.addProperty(None, 'CONSTANT') outDict = analyzeFile(path, getSafeTree(source, 1)) self.assertEqual(outDict, expectedDict, '%r != %r' % (outDict._modules, expectedDict._modules))
def process(argList, excluded, output, inputDict=None, verbose=False): modules = ModuleDict() if inputDict: modules.update(inputDict) for rootPackage in argList: if os.path.isdir(rootPackage): for path, dirs, files in os.walk(rootPackage): for exc in excluded: if exc in dirs: if verbose: print 'removing', exc, 'in', path dirs.remove(exc) for f in files: if not f.endswith(".py"): continue #path here is relative, make it absolute absPath = os.path.abspath(path) if verbose: print 'processing', absPath, f newmodules = processFile(f, absPath) modules.update(newmodules) else: # single file filename = rootPackage absPath, filename = os.path.split(filename) if not absPath: absPath = os.path.abspath(".") else: absPath = os.path.abspath(absPath) #path here is absolute if verbose: print 'processing', absPath, filename newmodules = processFile(filename, absPath) modules.update(newmodules) generateClassTag(modules, output)
def testAddPointer(self): md = ModuleDict() md.addPointer('something', 'other') self.assertEquals(md['POINTERS'], {'something': 'other'})
def testUpdate(self): total = ModuleDict() total.enterModule('mod1') total.enterClass('cls1', [], 'doc1') total.enterModule('mod2') total.enterClass('cls2', [], 'doc2') self.assertEquals(pformat(total), pformat(total._modules)) md1 = ModuleDict() md1.enterModule('mod1') md1.enterClass('cls1', [], 'doc1') md2 = ModuleDict() md2.enterModule('mod2') md2.enterClass('cls2', [], 'doc2') md3 = ModuleDict() md3.update(md1) self.assertEquals(pformat(md3), pformat(md1)) md3.update(md2) self.assertEquals(pformat(md3), pformat(total)) md3.update(None) self.assertEquals(pformat(md3), pformat(total))
def testAddPointer(self): md = ModuleDict() md.addPointer('something', 'other') self.assertEqual(md['POINTERS'], {'something': 'other'})
def testUpdate(self): total = ModuleDict() total.enterModule('mod1') total.enterClass('cls1', [], 'doc1') total.enterModule('mod2') total.enterClass('cls2', [], 'doc2') self.assertEqual(pformat(total), pformat(total._modules)) md1 = ModuleDict() md1.enterModule('mod1') md1.enterClass('cls1', [], 'doc1') md2 = ModuleDict() md2.enterModule('mod2') md2.enterClass('cls2', [], 'doc2') md3 = ModuleDict() md3.update(md1) self.assertEqual(pformat(md3), pformat(md1)) md3.update(md2) self.assertEqual(pformat(md3), pformat(total)) md3.update(None) self.assertEqual(pformat(md3), pformat(total))