def test_Module_is_valid(): class TestModule0(object): __name__ = 'TestModule0' __file__ = '' class TestModule1(object): __name__ = 'TestModule1' __file__ = 'ddr/repo_models' class TestModule2(object): __name__ = 'TestModule2' __file__ = 'ddr/repo_models' FIELDS = 'not a list' class TestModule3(object): __name__ = 'TestModule3' __file__ = 'ddr/repo_models' FIELDS = ['fake fields'] assert modules.Module(TestModule0()).is_valid() == ( False, "TestModule0 not in 'ddr' Repository repo.") assert modules.Module( TestModule1()).is_valid() == (False, 'TestModule1 has no FIELDS variable.') assert modules.Module( TestModule2()).is_valid() == (False, 'TestModule2.FIELDS is not a list.') assert modules.Module(TestModule3()).is_valid() == (True, 'ok')
def test_Module_document_commit(): module = TestModule() # commit exists document = TestDocument() document.object_metadata = { "models_commit": "20dd4e2096e6f9a9eb7c2db52907b094f41f58de 2015-10-13 17:08:43 -0700", } expected = '20dd4e2096e6f9a9eb7c2db52907b094f41f58de' assert modules.Module(module).document_commit(document) == expected # no commit document = TestDocument() document.object_metadata = {} expected = None assert modules.Module(module).document_commit(document) == expected
def test_check_row_values(): module = modules.Module(TestSchema()) headers = ['id', 'status'] valid_values = { 'status': [ 'inprocess', 'complete', ] } rowd0 = { 'id': 'ddr-test-123', 'status': 'inprocess', } expected0 = [] out0 = csvfile.check_row_values(module, headers, valid_values, rowd0) print('out0 %s' % out0) assert out0 == expected0 # invalid ID rowd1 = { 'id': 'not a valid ID', 'status': 'inprocess', } expected1 = ['id'] out1 = csvfile.check_row_values(module, headers, valid_values, rowd1) print('out1 %s' % out1) assert out1 == expected1 # invalid value rowd2 = { 'id': 'ddr-testing-123', 'status': 'inprogress', } expected2 = ['status'] out2 = csvfile.check_row_values(module, headers, valid_values, rowd2) print('out2 %s' % out2) assert out2 == expected2
def test_Module_labels_values(): module = TestModule() document = TestDocument() data = [ { 'id': 'ddr-test-123' }, { 'modified': '2015-10-20T15:42:26' }, { 'title': 'labels_values' }, ] json_data = models.load_json(document, module, json.dumps(data)) expected = [{ 'value': u'ddr-test-123', 'label': 'Object ID' }, { 'value': u'2015-10-20T15:42:26', 'label': 'Last Modified' }, { 'value': u'labels_values', 'label': 'Title' }] assert modules.Module(module).labels_values(document) == expected
def test_Module_path(): class TestModule(object): pass module = TestModule() module.__file__ = '/var/www/media/base/ddr/repo_models/testmodule.pyc' assert modules.Module( module).path == '/var/www/media/base/ddr/repo_models/testmodule.py'
def test_updateWeights(self): m = M.Module() try: m.updateWeights(0.1) except NotImplementedError: return 0 return 1
def test_resetGradient(self): m = M.Module() try: m.resetGradient() except NotImplementedError: return 0 return 1
def test_backward(self): m = M.Module() try: m.backward([]) except NotImplementedError: return 0 return 1
def test_Module_function(): class TestModule(object): def hello(self, text): return 'hello %s' % text module = TestModule() module.__file__ = 'ddr/repo_models' assert modules.Module(module).function('hello', 'world') == 'hello world'
def test_Module_cmp_model_definition_fields(): module = TestModule() module.FIELDS = [ { 'name': 'id', }, { 'name': 'modified', }, { 'name': 'title', }, ] m = modules.Module(module) data = [ {}, # object_metadata { 'id': 'ddr-test-123' }, { 'modified': '2015-10-20T15:42:26' }, { 'title': 'labels_values' }, ] expected0 = {'removed': [], 'added': []} out0 = m.cmp_model_definition_fields(json.dumps(data)) data.append({'new': 'new field'}) expected1 = {'removed': [], 'added': ['new']} out1 = m.cmp_model_definition_fields(json.dumps(data)) data.pop() # rm new data.pop() # rm title expected2 = {'removed': ['title'], 'added': []} out2 = m.cmp_model_definition_fields(json.dumps(data)) assert out0 == expected0 assert out1 == expected1 assert out2 == expected2
def test_find_invalid_values(): module = modules.Module(TestSchema()) headers = ['id', 'status'] required_fields = ['id', 'status'] valid_values = { 'status': [ 'inprocess', 'complete', ] } # OK rowds0 = [ { 'id': 'ddr-test-123', 'status': 'inprocess', }, { 'id': 'ddr-test-124', 'status': 'complete', }, ] expected0 = [] out0 = csvfile.find_invalid_values(module, headers, valid_values, rowds0) assert out0 == expected0 # error rowds1 = [ { 'id': 'ddr-test-123', 'status': 'inprogress', }, { 'id': 'ddr-test-124', 'status': 'complete', }, ] expected1 = ["row 1: ddr-test-123 ['status']"] out1 = csvfile.find_invalid_values(module, headers, valid_values, rowds1) assert out1 == expected1
def get_defined_names(self, on_import_stmt=False): names = [] for scope in self.follow(): if scope is ImportPath.GlobalNamespace: if self.import_stmt.relative_count == 0: names += self.get_module_names() if self.file_path is not None: path = os.path.abspath(self.file_path) for i in range(self.import_stmt.relative_count - 1): path = os.path.dirname(path) names += self.get_module_names([path]) if self.import_stmt.relative_count: rel_path = self.get_relative_path() + '/__init__.py' try: m = modules.Module(rel_path) names += m.parser.module.get_defined_names() except IOError: pass else: if on_import_stmt and isinstance(scope, parsing.Module) \ and scope.path.endswith('__init__.py'): pkg_path = os.path.dirname(scope.path) names += self.get_module_names([pkg_path]) for s, scope_names in evaluate.get_names_for_scope( scope, include_builtin=False): for n in scope_names: if self.import_stmt.from_ns is None \ or self.is_partial_import: # from_ns must be defined to access module # values plus a partial import means that there # is something after the import, which # automatically implies that there must not be # any non-module scope. continue names.append(n) return names
def _follow_file_system(self): """ Find a module with a path (of the module, like usb.backend.libusb10). """ def follow_str(ns_path, string): debug.dbg('follow_module', ns_path, string) path = None if ns_path: path = ns_path elif self.import_stmt.relative_count: path = self.get_relative_path() if path and not isinstance(path, list): path = [path] global imports_processed imports_processed += 1 if path is not None: return imp.find_module(string, path) else: debug.dbg('search_module', string, self.file_path) # Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work. sys.path, temp = sys_path_mod, sys.path try: i = imp.find_module(string) except ImportError: sys.path = temp raise sys.path = temp return i if self.file_path: sys_path_mod = list(self.sys_path_with_modifications()) sys_path_mod.insert(0, self.file_path) else: sys_path_mod = list(builtin.get_sys_path()) current_namespace = (None, None, None) # now execute those paths rest = [] for i, s in enumerate(self.import_path): try: current_namespace = follow_str(current_namespace[1], s) if not current_namespace[1] and is_py33: loader = importlib.find_loader(s) if loader.__name__ == 'BuiltinImporter': current_namespace = (current_namespace[0], s, current_namespace[2]) except ImportError as ex: if not i and is_py33: loader = importlib.find_loader(s) if 'NamespaceLoader' in repr(loader): try: m = __import__(s) except ImportError: pass else: current_namespace = (None, list(m.__path__), None) continue if self.import_stmt.relative_count \ and len(self.import_path) == 1: # follow `from . import some_variable` rel_path = self.get_relative_path() try: current_namespace = follow_str(rel_path, '__init__') except ImportError: pass if current_namespace[1]: rest = self.import_path[i:] else: raise ModuleNotFound( 'The module you searched has not been found') sys_path_mod.pop(0) # TODO why is this here? path = current_namespace[1] is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY f = None if is_package_directory or current_namespace[0]: # is a directory module if is_package_directory: path += '/__init__.py' with open(path) as f: source = f.read() else: source = current_namespace[0].read() current_namespace[0].close() if path.endswith('.py'): f = modules.Module(path, source) else: f = builtin.Parser(path=path) else: f = builtin.Parser(name=path) return f.parser.module, rest
def test_params(self): m = M.Module() if len(m.param()) == 0: return 0 return 1
def test_Module_parse_commit(): module = TestModule() text = '95a3a0ed3232990ee8fbbc3065a11316bccd0b35 2015-03-26 15:49:58 -0700' expected = '95a3a0ed3232990ee8fbbc3065a11316bccd0b35' assert modules.Module(module)._parse_commit(text) == expected
def _follow_file_system(self): """ Find a module with a path (of the module, like usb.backend.libusb10). """ def follow_str(ns, string): debug.dbg('follow_module', ns, string) path = None if ns: path = ns[1] elif self.import_stmt.relative_count: module = self.import_stmt.get_parent_until() path = os.path.abspath(module.path) for i in range(self.import_stmt.relative_count): path = os.path.dirname(path) global imports_processed imports_processed += 1 if path is not None: return imp.find_module(string, [path]) else: debug.dbg('search_module', string, self.file_path) # Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work. sys.path, temp = sys_path_mod, sys.path try: i = imp.find_module(string) except ImportError: sys.path = temp raise sys.path = temp return i if self.file_path: sys_path_mod = list(self.sys_path_with_modifications()) sys_path_mod.insert(0, self.file_path) else: sys_path_mod = list(builtin.get_sys_path()) current_namespace = None # now execute those paths rest = [] for i, s in enumerate(self.import_path): try: current_namespace = follow_str(current_namespace, s) except ImportError: if current_namespace: rest = self.import_path[i:] else: raise ModuleNotFound( 'The module you searched has not been found') sys_path_mod.pop(0) # TODO why is this here? path = current_namespace[1] is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY f = None if is_package_directory or current_namespace[0]: # is a directory module if is_package_directory: path += '/__init__.py' with open(path) as f: source = f.read() else: source = current_namespace[0].read() current_namespace[0].close() if path.endswith('.py'): f = modules.Module(path, source) else: f = builtin.Parser(path=path) else: f = builtin.Parser(name=path) return f.parser.module, rest
def __init__(self): super(Program, self).__init__(None) self.main_module = modules.Module(self, 'main');
def check_fs(path): with open(path) as f: source = f.read() if name in source: return modules.Module(path, source).parser.module
def check_fs(path): with open(path) as f: source = modules.source_to_unicode(f.read()) if name in source: return modules.Module(path, source).parser.module