def can_import_required(patch): for module_name in patch.required_modules: try: py.get_module_by_name(module_name) except ModuleNotFoundError: return False return True
def test_get_inherited_classes(): temp_folder = utils.make_tmpdir() pkg_name = 'pike_mgr_inherited_classes' pkg_location = utils.create_working_package(temp_folder, pkg_name) test_file_content = textwrap.dedent(""" class SampleObj(object): pass class OtherObj(SampleObj): pass """) mod_location = os.path.join(pkg_location, 'app.py') utils.write_file(mod_location, test_file_content) # Include module directly on the search path second_file = textwrap.dedent(""" class AnotherObj(object): pass """) mod_location = os.path.join(temp_folder, 'more.py') utils.write_file(mod_location, second_file) classes = [] with PikeManager([temp_folder]) as mgr: app = py.get_module_by_name('{}.app'.format(pkg_name)) classes = mgr.get_all_inherited_classes(app.SampleObj) assert len(classes) == 1
def command(arguments): options = build_options(arguments) file_path = arguments.path input_handler = JsonHandler() output_handler = None if arguments.file_type == 'yml': input_handler = YamlHandler() if arguments.dest == aumbry.PARAM_STORE: output_handler = GenericHandler() if not has_required(arguments.dest, options): print('Missing required options for destination type') return 1 package_ref, _, name = arguments.config_class.partition(':') if not name: print('config_class: requires a package and class reference') print('Example: my_package.sub:AppConfig') return 1 with PikeManager([arguments.package_root]): module = py.get_module_by_name(package_ref) config_cls = getattr(module, name) print('Loading Config File...') cfg = aumbry.load(aumbry.FILE, config_cls, {'CONFIG_FILE_PATH': file_path}, handler=input_handler) print('Uploading Config...') aumbry.save(arguments.dest, cfg, options, handler=output_handler)
def installed(self): current_module = py.get_module_by_name('battlement.plugins') all_classes = py.get_all_inherited_classes( current_module, ProvisionerPluginBase ) return list(all_classes)
def test_child_modules_without_sub_packages(self): mod_location = os.path.join(self.pkg_location, 'app.py') utils.write_file(mod_location) parent_module = py.get_module_by_name(self.pkg_name) child_modules = list(py._child_modules(parent_module)) assert len(child_modules) == 1
def patch_zag_tasks_in_projects(self, project_packages): task_module = importlib.import_module('zag.task') to_patch = [ cls for package in project_packages for child in py.get_child_modules(py.get_module_by_name(package)) for cls in py.get_all_inherited_classes(child, task_module.Task) ] for cls in to_patch: module = py.get_module_by_name(cls.__module__) wrap_function_trace(module, '{0}.pre_execute'.format(cls.__name__)) wrap_function_trace(module, '{0}.execute'.format(cls.__name__)) wrap_function_trace(module, '{0}.post_execute'.format(cls.__name__)) wrap_function_trace(module, '{0}.pre_revert'.format(cls.__name__)) wrap_function_trace(module, '{0}.revert'.format(cls.__name__)) wrap_function_trace(module, '{0}.post_revert'.format(cls.__name__))
def get_classes(self, filter_func=None): """Get all classes within modules on the manager's search paths :param Function filter_func: Custom filter function(cls_obj). :returns: :class:`List` of all found classes """ all_classes = [] # Top-most Modules for module_name in self.get_module_names(): module = py.get_module_by_name(module_name) all_classes.extend(py.classes_in_module(module, filter_func)) # All packages for module_name in self.get_package_names(): module = py.get_module_by_name(module_name) all_classes.extend(py.get_all_classes(module, filter_func)) return all_classes
def get_all_inherited_classes(self, base_class): """Retrieve all inherited classes from manager's search paths :param Class base_class: Base class to filter results by :return: :class:`List` of all found classes """ all_classes = [] # Top-most Modules for module_name in self.get_module_names(): module = py.get_module_by_name(module_name) all_classes.extend(py.get_inherited_classes(module, base_class)) # All packages for module_name in self.get_package_names(): module = py.get_module_by_name(module_name) inherited = py.get_all_inherited_classes(module, base_class) all_classes.extend(inherited) return all_classes
def setting_a_valid_path(self): search_paths = py.get_module_by_name('aumbry.sources').__path__ temp, options = write_temp_file(raw_yaml) cfg = aumbry.load('file', SampleYamlConfig, options, search_paths=search_paths) os.remove(temp.name) expect(cfg.nope).to.equal('testing')
def test_get_inherited_classes_with_fixtures(pike_tmp_package): """Structurally the same than test_get_inherited_classes, but with fixtures (see conftest.py) """ # Actually, this is not really needed. pkg_name = 'pike_mgr_inherited_classes' pike_tmp_package.rename(pike_tmp_package.dirpath() / pkg_name) classes = [] with PikeManager([pike_tmp_package.dirname]) as mgr: app = py.get_module_by_name('{}.app'.format(pkg_name)) classes = mgr.get_all_inherited_classes(app.SampleObj) assert len(classes) == 1
def test_get_child_modules(self): subpkg_location = utils.create_working_package( self.pkg_location, 'sub_mod' ) mod_location = os.path.join(subpkg_location, 'something.py') utils.write_file(mod_location) module = py.get_module_by_name(self.pkg_name) # Recursive assert len(list(py.get_child_modules(module))) == 3 # Non-Recursive assert len(list(py.get_child_modules(module, False))) == 2
def test_child_modules_with_sub_packages(self): subpkg_location = utils.create_working_package( self.pkg_location, 'submod' ) mod_location = os.path.join(self.pkg_location, 'app.py') utils.write_file(mod_location) submod_location = os.path.join(subpkg_location, 'other.py') utils.write_file(submod_location) parent_module = py.get_module_by_name(self.pkg_name) child_modules = list(py._child_modules(parent_module)) assert len(child_modules) == 2
def installed(self): current_module = py.get_module_by_name('battlement.plugins') all_classes = py.get_all_inherited_classes(current_module, ProvisionerPluginBase) return list(all_classes)
def test_get_all_classes(self): module = py.get_module_by_name(self.pkg_name) assert len(list(py.get_all_classes(module))) == 2
def test_classes_in_module(self): module = py.get_module_by_name('{}.app'.format(self.pkg_name)) assert len(list(py.classes_in_module(module))) == 2
def get_reporters_classes(self): module = py.get_module_by_name('specter.reporting') return py.get_all_classes(module, self.reporter_filter)
def find_source(name, search_paths=None): if not search_paths: search_paths = py.get_module_by_name('aumbry.sources').__path__ return find_plugin(name, AbstractSource, search_paths)
def test_get_module_by_name(self): assert py.get_module_by_name(self.pkg_name) is not None
def test_get_inherited_classes(self): module = py.get_module_by_name('{}.app'.format(self.pkg_name)) test_base_class = getattr(module, 'SampleObj') classes = py.get_inherited_classes(module, test_base_class) assert len(classes) == 1