def test_get_module_functions(self): from inspect import isroutine module_filename = "tmp_module2.py" module = __load_module__(module_filename,allowdupe=True) routines = [name for name in _dir(module) if isroutine(getattr(module,name))] self.assertListEqual(routines,['myfunc'])
def test_get_module_classes(self): from inspect import isclass module_filename = "tmp_module2.py" module = __load_module__(module_filename,allowdupe=True) classes = [name for name in _dir(module) if isclass(getattr(module,name))] self.assertEquals(classes,['foobar','foobar2','foobar3','mybase'])
def test_get_module_functions(self): from inspect import isroutine module = __load_module__(self.tmp_module2, allowdupe=True) routines = [ name for name in _dir(module) if isroutine(getattr(module, name)) ] self.assertListEqual(routines, ['myfunc'])
def test_get_module_classes(self): from inspect import isclass module = __load_module__(self.tmp_module2, allowdupe=True) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] self.assertEquals(classes, ['foobar', 'foobar2', 'foobar3', 'mybase'])
def test_load_module_not_in_cwd_relpath_forced(self): # force the module to not be in the current path import os from inspect import isclass module_filename = join(self.test_dir,"./.tmp/tmp_module4.py") module = __load_module__(module_filename,allowdupe=True) classes = [name for name in _dir(module) if isclass(getattr(module,name))] self.assertEquals(classes,['foobar','foobar2','foobar3','mybase'])
def test_get_module_class_methods(self): from inspect import isclass module_filename = "tmp_module2.py" module = __load_module__(module_filename,allowdupe=True) classes = [name for name in _dir(module) if isclass(getattr(module,name))] m=[] for cls in classes: m = m + _getclassmethods(getattr(module,cls)) self.assertListEqual(['boo','boohoo','boo2','boo3'],m)
def test_load_module_not_in_cwd_relpath_forced(self): # force the module to not be in the current path import os from inspect import isclass module_filename = path.join(TESTDIR, "tmp_module4.py") module = __load_module__(module_filename, allowdupe=True) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] self.assertEquals(classes, ['foobar', 'foobar2', 'foobar3', 'mybase'])
def test_get_module_class_methods(self): from inspect import isclass module = __load_module__(self.tmp_module2, allowdupe=True) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] m = [] for cls in classes: m = m + _getclassmethods(getattr(module, cls)) self.assertListEqual(['boo', 'boohoo', 'boo2', 'boo3'], m)
def test_load_module_same_module_twice(self): # force the module to not be in the current path import os from inspect import isclass module_filename = join(self.test_dir,"./.tmp/tmp_module5.py") module = __load_module__(module_filename) classes = [name for name in _dir(module) if isclass(getattr(module,name))] self.assertEquals(classes,['foobar','foobar2','foobar3','mybase']) module_filename = "./.tmp_dupe/tmp_module5.py" with self.assertRaises(Exception): module = __load_module__(module_filename)
def test_load_module_not_in_cwd_abspath(self): # if module.py in /home/burtnolej; move cwd to /tmp and load # /home/burtnolej/module.py import os from inspect import isclass _cwd = os.getcwd() os.chdir("/tmp") module_filename = "tmp_module3.py" module_path = os.path.join(_cwd,"tmp_module3.py") module = __load_module__(module_path,allowdupe=True) os.chdir(_cwd) classes = [name for name in _dir(module) if isclass(getattr(module,name))] self.assertEquals(classes,['foobar','foobar2','foobar3','mybase'])
def py2xml(self,**kwargs): for k,v in kwargs.iteritems(): setattr(self,k,v) if not hasattr(self,'module'): self.module = __load_module__(self.input_filename,allowdupe=True) from inspect import getmembers, isclass, isroutine tokens = __tokenize__(self.input_filename) root = xmltree.Element('root') for item in _dir(self.module): obj = getattr(self.module,item) if isclass(obj): if isuserclass(item,self.input_filename): cls_element = __add_element__(root,item,'class') for m in _getclassmethods(obj): clsmth_element = __add_element__(cls_element,m,'method') for arg in _getargsfromsource(getattr(obj,m),tokens): __add_element__(clsmth_element,arg,'arg') for var in _getvarfromsource(getattr(obj,m)): __add_element__(clsmth_element,var,'var') for memvar in _getclassmethods(obj,True): __add_element__(cls_element,memvar,'var') elif isroutine(obj): if isuserfunc(item,self.input_filename): def_element = __add_element__(root,item,'def') for arg in _getargsfromsource(obj,tokens): __add_element__(def_element,arg,'arg') for var in _getvarfromsource(obj): __add_element__(def_element,var,'var') else: __add_element__(root,item,'var') output_filename = __convert_filename__(self.input_filename,".xml") __xmltofile__(root,output_filename) return(output_filename)
def test_get_module_class_vars(self): from inspect import isclass, ismethod module_filename = "tmp_module2.py" module = __load_module__(module_filename,allowdupe=True) classes = [name for name in _dir(module) if isclass(getattr(module,name))] v=[] _m = _getmembers(module.foobar) for key, obj in _m.iteritems(): if not ismethod(obj): v.append(key) self.assertListEqual(['blahblah'],v)
def test_load_module_same_module_twice(self): # force the module to not be in the current path import os from inspect import isclass module_filename = path.join(TESTDIR, "tmp_module5.py") module = __load_module__(module_filename) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] self.assertEquals(classes, ['foobar', 'foobar2', 'foobar3', 'mybase']) module_filename = "./.tmp_dupe/tmp_module5.py" with self.assertRaises(Exception): module = __load_module__(module_filename)
def test_load_module_not_in_cwd_abspath(self): # if module.py in /home/burtnolej; move cwd to /tmp and load # /home/burtnolej/module.py import os from inspect import isclass _cwd = os.getcwd() os.chdir(LOGDIR) module_path = path.join(TESTDIR, "tmp_module3.py") module = __load_module__(module_path, allowdupe=True) os.chdir(_cwd) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] self.assertEquals(classes, ['foobar', 'foobar2', 'foobar3', 'mybase'])
def test_get_module_class_vars(self): from inspect import isclass, ismethod module = __load_module__(self.tmp_module2, allowdupe=True) classes = [ name for name in _dir(module) if isclass(getattr(module, name)) ] v = [] _m = _getmembers(module.foobar) for key, obj in _m.iteritems(): if not ismethod(obj): v.append(key) self.assertListEqual(['blahblah'], v)