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 initplugins(): with PikeManager([plugin_path]) as mgr: classes = mgr.get_classes() for item in classes: obj = item() plugins.append(obj.init())
def find_plugin(name, base_cls, search_paths): with PikeManager(search_paths) as mgr: handlers = mgr.get_all_inherited_classes(base_cls) for handler in handlers: if handler.extras_name == name: return handler
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 setup_method(self, method): self.temp_folder = utils.make_tmpdir() self.manager = PikeManager([self.temp_folder]) self.pkg_name = 'pike_{}'.format(method.__name__) self.pkg_location = utils.create_working_package( self.temp_folder, self.pkg_name )
def test_get_classes_with_fixtures(pike_tmp_package): """Structurally the same than test_get_classes, but with fixtures (see conftest.py) """ classes = [] with PikeManager([str(pike_tmp_package)]) as mgr: classes = mgr.get_classes() assert len(classes) == 3
def test_double_cleanup_shouldnt_fail(): """Making sure multiple cleanups don't cause problems This case happens when Python's GC calls the destructor on the manager """ mgr = PikeManager(['./']) mgr.cleanup() mgr.cleanup() assert mgr.module_finder not in sys.meta_path
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 discover_hosts(src, metrics=None): discovery_type = src['type'] try: host_list = list() with PikeManager(['.', 'drivers']): discovery = pike.discovery.py.get_module_by_name('hemApp.drivers.discovery_' + discovery_type) if None != metrics: src['metrics'] = metrics try: host_list = discovery.hosts(**src) except Exception as e: logging.error("{} discovery of failed with exception".format(discovery_type)) logging.exception(e) host_list = [] return host_list except ImportError as e: logging.exception(e) click.echo("Discovery method {} not found".format(discovery_type)) return []
def __init__(self, search_path): super(SuiteScanner, self).__init__() self.search_path = path.abspath(search_path) self.plugin_manager = PikeManager([self.search_path])
def initialise_metrics(metricConfig): with PikeManager(['.', 'drivers']): metrics_driver = pike.discovery.py.get_module_by_name( 'hemApp.drivers.metrics_' + metricConfig.get('type', 'console')) return metrics_driver.instance(metricConfig)
def load_plugins(are_plugins_blocked,additional_locations): global PLUGINS global AUTOCOMPLETE global HELP PLUGINS = [] ERRORS = [] DIRECTORIES = [] AUTOCOMPLETE = [] HELP = [] if not config.ENABLE_PLUGINS: return [] if are_plugins_blocked: return [] DIRECTORIES.append(PLUGIN_DIRECTORY) if len(additional_locations)>0: for loc in additional_locations: DIRECTORIES.append(loc) for d in config.ADDITIONAL_PLUGIN_LOCATIONS: DIRECTORIES.append(d) # Remove duplicates from the list DIRECTORIES = list(dict.fromkeys(DIRECTORIES)) with PikeManager(DIRECTORIES) as mgr: classes = mgr.get_classes() for c in classes: # Ignore the base plugin class if c.__name__=="Plugin": continue # Create an instance of the plugin class obj = c() # Create the plugin entry for the registry (and errors) entry = PluginEntry(c,obj) # Make sure the class has any required attributes had_error = False if not hasattr(obj,"NAME"): entry.errors.append("No name attribute") had_error = True else: n = obj.NAME.strip() if len(n)==0: entry.errors.append("Name entry is blank") had_error = True if not hasattr(obj,"VERSION"): entry.errors.append("No version attribute") had_error = True # Make sure the plugin inherits from the "Plugin" class if not issubclass(type(obj), Plugin): entry.errors.append("Plugin doesn't inherit from \"Plugin\"") had_error = True # Make sure that the input() event method is valid, if the # plugin has one if check_for_bad_input(obj): entry.errors.append("Malicious input() event method detected") had_error = True # Make sure that the plugin has at least *one* event method counter = 0 for e in EVENTS: if hasattr(obj,e): counter = counter+1 if counter==0: entry.errors.append("Plugin doesn't have any "+APPLICATION_NAME+" event methods") had_error = True entry.number_of_events = counter # If we had an error, don't add the plugin to the registry if had_error: ERRORS.append(entry) continue # Add the plugin to the registry PLUGINS.append(entry) # Return any errors return ERRORS
"""This is an example using Pike Install the necessary package with $ pip install pike Currently unable to get it working. """ from pike.manager import PikeManager if __name__ == "__main__": services = ['dns', 'loadbalancer'] all_plugins = [] for service in services: path = './foremast/{}'.format(service) all_plugins.append(path) print(all_plugins) with PikeManager(all_plugins) as mgr: classes = mgr.get_classes() print(classes) for item in classes: obj = item() print(obj.create())
def test_del_removes_from_meta_path(): mgr = PikeManager(['./']) assert mgr.module_finder in sys.meta_path mgr.__del__() assert len(finders_in_meta_path()) == 0
def test_double_add_meta_path(): with PikeManager(['./']) as mgr: mgr.add_to_meta_path() assert len(finders_in_meta_path()) == 1
def test_manager_as_context_manager(): mgr = PikeManager(['./']) with mgr: assert mgr.module_finder in sys.meta_path assert mgr.module_finder not in sys.meta_path
def test_manager_with_normal_instantiation(): mgr = PikeManager(['./']) assert mgr.module_finder in sys.meta_path mgr.cleanup() assert mgr.module_finder not in sys.meta_path