def _parse_python_lib(self, library, args): lib_with_args = self._lib_arg_formatter(library, args) kws = {} try: lib = self.libdoc.build(lib_with_args) except DataError: raise ValueError( 'Library does not exist: {0}'.format(library)) if library in STDLIBS: import_name = 'robot.libraries.' + library else: import_name = library importer = Importer('test library') libcode = importer.import_class_or_module( import_name, return_source=False) kw_with_deco = self._get_keywords_with_robot_name(libcode) for keyword in lib.keywords: kw = {} kw[DBJsonSetting.keyword_name] = keyword.name kw[DBJsonSetting.tags] = list(keyword.tags._tags) kw[DBJsonSetting.keyword_arguments] = keyword.args kw[DBJsonSetting.documentation] = keyword.doc if keyword.name in kw_with_deco: function_name = kw_with_deco[keyword.name] else: function_name = keyword.name kw[DBJsonSetting.keyword_file] = self._get_library_kw_source( libcode, function_name) kws[strip_and_lower(keyword.name)] = kw return kws
def _parse_python_lib(self, library, args): lib_with_args = self._lib_arg_formatter(library, args) kws = {} try: lib = self.libdoc.build(lib_with_args) except DataError: raise ValueError('Library does not exist: {0}'.format(library)) if library in STDLIBS: import_name = 'robot.libraries.' + library else: import_name = library importer = Importer('test library') libcode = importer.import_class_or_module(import_name, return_source=False) kw_with_deco = self._get_keywords_with_robot_name(libcode) for keyword in lib.keywords: kw = {} kw[DBJsonSetting.keyword_name] = keyword.name kw[DBJsonSetting.tags] = list(keyword.tags._tags) kw[DBJsonSetting.keyword_arguments] = keyword.args kw[DBJsonSetting.documentation] = keyword.doc if keyword.name in kw_with_deco: function_name = kw_with_deco[keyword.name] else: function_name = keyword.name kw[DBJsonSetting.keyword_file] = self._get_library_kw_source( libcode, function_name) kws[strip_and_lower(keyword.name)] = kw return kws
def test_non_absolute(self): path = os.listdir('.')[0] assert_raises_with_msg(DataError, "Importing '%s' failed: Import path must be absolute." % path, Importer().import_class_or_module_by_path, path) assert_raises_with_msg(DataError, "Importing file '%s' failed: Import path must be absolute." % path, Importer('file').import_class_or_module_by_path, path)
def test_invalid_format(self): path = join(CURDIR, '..', '..', 'README.txt') assert_raises_with_msg(DataError, "Importing '%s' failed: Not a valid file or directory to import." % path, Importer().import_class_or_module_by_path, path) assert_raises_with_msg(DataError, "Importing xxx '%s' failed: Not a valid file or directory to import." % path, Importer('xxx').import_class_or_module_by_path, path)
def _get_reader_class_from_module(self, reader_name): importer = Importer('DataReader') self._debug(f'[ DataDriver ] Reader Module: {reader_name}') reader = importer.import_class_or_module(reader_name) if not inspect.isclass(reader): message = f"Importing custom DataReader class {reader_name} failed." raise ImportError(message) return reader
def test_non_existing(self): path = 'non-existing.py' assert_raises_with_msg(DataError, "Importing '%s' failed: File or directory does not exist." % path, Importer().import_class_or_module_by_path, path) path = abspath(path) assert_raises_with_msg(DataError, "Importing test file '%s' failed: File or directory does not exist." % path, Importer('test file').import_class_or_module_by_path, path)
def _get_reader_class_from_path(self, file_name): self._debug(f'[ DataDriver ] Loading Reader from file {file_name}') abs_path = os.path.abspath(file_name) importer = Importer('DataReader') self._debug(f'[ DataDriver ] Reader path: {abs_path}') reader = importer.import_class_or_module_by_path(abs_path) if not inspect.isclass(reader): message = f"Importing custom DataReader class from {abs_path} failed." raise ImportError(message) return reader
def _import(self, path, name=None, remove=None): if remove and remove in sys.modules: del sys.modules[remove] self.logger = LoggerStub() importer = Importer(name, self.logger) sys_path_before = sys.path[:] try: return importer.import_class_or_module_by_path(path) finally: assert_equals(sys.path, sys_path_before)
def _parse_listener(self, event_firing_webdriver): listener_module = self._string_to_modules(event_firing_webdriver) listener_count = len(listener_module) if listener_count > 1: message = f"Is is possible import only one listener but there was {listener_count} listeners." raise ValueError(message) listener_module = listener_module[0] importer = Importer("test library") listener = importer.import_class_or_module(listener_module.module) if not isclass(listener): message = f"Importing test Selenium lister class '{listener_module.module}' failed." raise DataError(message) return listener
def _parse_plugins(self, plugins): libraries = [] importer = Importer('test library') for parsed_plugin in self._string_to_modules(plugins): plugin = importer.import_class_or_module(parsed_plugin.module) if not isclass(plugin): message = "Importing test library: '%s' failed." % parsed_plugin.module raise DataError(message) plugin = plugin(self, *parsed_plugin.args, **parsed_plugin.kw_args) if not isinstance(plugin, LibraryComponent): message = 'Plugin does not inherit SeleniumLibrary.base.LibraryComponent' raise PluginError(message) self._store_plugin_keywords(plugin) libraries.append(plugin) return libraries
def test_instantiate_failure(self): err = assert_raises(DataError, Importer().import_class_or_module, 'ExampleLibrary', ['accepts', 'no', 'args']) assert_true( unicode(err).startswith("Importing 'ExampleLibrary' failed: " "Creating instance failed: TypeError:"))
def test_argument_conversion(self): path = create_temp_file('conversion.py', extra_content=''' class conversion: def __init__(self, arg: int): self.arg = arg ''') lib = Importer().import_class_or_module_by_path(path, ['42']) assert_true(not inspect.isclass(lib)) assert_equal(lib.__class__.__name__, 'conversion') assert_equal(lib.arg, 42) assert_raises_with_msg( DataError, "Importing xxx '%s' failed: " "Argument 'arg' got value 'invalid' that cannot be converted to integer." % path, Importer('XXX').import_class_or_module, path, ['invalid'] )
def test_escape_equals(self): lib = Importer().import_class_or_module('libswithargs.Mixed', [r'default\=b', r'mandatory\=a']) assert_equal(lib.get_args(), (r'default\=b', r'mandatory\=a', '')) lib = Importer().import_class_or_module('libswithargs.Mixed', [r'default\=b', 'default=a']) assert_equal(lib.get_args(), (r'default\=b', 'a', ''))
def test_arguments_when_importing_by_path(self): path = create_temp_file('args.py', extra_content=''' class args: def __init__(self, arg='default'): self.arg = arg ''') importer = Importer().import_class_or_module_by_path for args, expected in [((), 'default'), (['positional'], 'positional'), (['arg=named'], 'named')]: lib = importer(path, args) assert_true(not inspect.isclass(lib)) assert_equal(lib.__class__.__name__, 'args') assert_equal(lib.arg, expected)
def _import_modules(self, plugins): importer = Importer('test library') return [ importer.import_class_or_module(plugin.plugin) for plugin in plugins ]
def test_logging(self): logger = LoggerStub(remove_extension=True) Importer(logger=logger).import_module('ExampleLibrary') logger.assert_message("Imported module 'ExampleLibrary' from '%s'." % join(LIBDIR, 'ExampleLibrary'))
def test_modules_do_not_take_arguments(self): path = create_temp_file('no_args_allowed.py') assert_raises_with_msg(DataError, "Importing '%s' failed: Modules do not take arguments." % path, Importer().import_class_or_module_by_path, path, ['invalid'])
def test_when_importing_by_path(self): path = create_temp_file('args.py', extra_content='class args: a=1') lib = Importer().import_class_or_module_by_path(path, ()) assert_true(not inspect.isclass(lib)) assert_equals(lib.__class__.__name__, 'args') assert_equals(lib.a, 1)
def test_with_arguments(self): lib = Importer().import_class_or_module('libswithargs.Mixed', range(5)) assert_equals(lib.get_args(), (0, 1, '2 3 4'))
def test_when_importing_by_name(self): from ExampleLibrary import ExampleLibrary lib = Importer().import_class_or_module('ExampleLibrary', instantiate_with_args=()) assert_true(not inspect.isclass(lib)) assert_true(isinstance(lib, ExampleLibrary))
def _failing_import(self, name): importer = Importer().import_class_or_module return assert_raises(DataError, importer, name)
def _import(self, name, type=None, logger=None): return Importer(type, logger or LoggerStub()).import_class_or_module(name)
def test_named_arguments(self): lib = Importer().import_class_or_module('libswithargs.Mixed', ['default=b', 'mandatory=a']) assert_equal(lib.get_args(), ('a', 'b', ''))
def test_escaping_not_needed_if_args_do_not_match_names(self): lib = Importer().import_class_or_module('libswithargs.Mixed', ['foo=b', 'bar=a']) assert_equal(lib.get_args(), ('foo=b', 'bar=a', ''))
def test_import_module(self): module = Importer().import_module('ExampleLibrary') assert_equal(module.ExampleLibrary().return_string_from_library('xxx'), 'xxx')
def test_instantiate_failure(self): assert_raises_with_msg( DataError, "Importing xxx 'ExampleLibrary' failed: Xxx 'ExampleLibrary' expected 0 arguments, got 3.", Importer('XXX').import_class_or_module, 'ExampleLibrary', ['accepts', 'no', 'args'] )