Example #1
0
class SuiteScanner(object):

    def __init__(self, search_path):
        super(SuiteScanner, self).__init__()
        self.search_path = path.abspath(search_path)
        self.plugin_manager = PikeManager([self.search_path])

    def filter_by_module_name(self, classes, name):
        found = [cls for cls in classes
                 if name in '{}.{}'.format(cls.__module__, cls.__name__)]

        # Only search children if the class cannot be found at the package lvl
        if not found:
            children = [cls.__get_all_child_describes__() for cls in classes]
            children = chain.from_iterable(children)
            found = [cls for cls in children
                     if name in '{}.{}'.format(cls.__module__, cls.__name__)]

        return found

    def scan(self, module_name=None):
        if not path.exists(path.join(self.search_path)):
            return []

        classes = self.plugin_manager.get_classes(Describe.plugin_filter)
        if module_name:
            classes = self.filter_by_module_name(classes, module_name)

        return classes

    def destroy(self):
        self.plugin_manager.cleanup()
Example #2
0
class SuiteScanner(object):
    def __init__(self, search_path):
        super(SuiteScanner, self).__init__()
        self.search_path = path.abspath(search_path)
        self.plugin_manager = PikeManager([self.search_path])

    def filter_by_module_name(self, classes, name):
        found = [
            cls for cls in classes
            if name in '{}.{}'.format(cls.__module__, cls.__name__)
        ]

        # Only search children if the class cannot be found at the package lvl
        if not found:
            children = [cls.__get_all_child_describes__() for cls in classes]
            children = chain.from_iterable(children)
            found = [
                cls for cls in children
                if name in '{}.{}'.format(cls.__module__, cls.__name__)
            ]

        return found

    def scan(self, module_name=None):
        if not path.exists(path.join(self.search_path)):
            return []

        classes = self.plugin_manager.get_classes(Describe.plugin_filter)
        if module_name:
            classes = self.filter_by_module_name(classes, module_name)

        return classes

    def destroy(self):
        self.plugin_manager.cleanup()
Example #3
0
 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
     )
Example #4
0
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
Example #5
0
class BaseTestCase(object):
    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 teardown_method(self, method):
        self.manager.cleanup()
        utils.remove_dir(self.temp_folder)
Example #6
0
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
Example #7
0
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 initplugins():
    with PikeManager([plugin_path]) as mgr:
        classes = mgr.get_classes()

        for item in classes:
            obj = item()
            plugins.append(obj.init())
Example #9
0
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
Example #10
0
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
Example #11
0
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
Example #12
0
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 []
Example #13
0
 def __init__(self, search_path):
     super(SuiteScanner, self).__init__()
     self.search_path = path.abspath(search_path)
     self.plugin_manager = PikeManager([self.search_path])
Example #14
0
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
Example #15
0
def test_double_add_meta_path():
    with PikeManager(['./']) as mgr:
        mgr.add_to_meta_path()
        assert len(finders_in_meta_path()) == 1
Example #16
0
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
Example #17
0
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
Example #18
0
"""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())
Example #19
0
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
Example #20
0
 def __init__(self, search_path):
     super(SuiteScanner, self).__init__()
     self.search_path = path.abspath(search_path)
     self.plugin_manager = PikeManager([self.search_path])
Example #21
0
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)