File: backendpluginFactory.py

package info (click to toggle)
python-libnmap 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,416 kB
  • sloc: xml: 5,572; python: 4,299; makefile: 149
file content (37 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
import inspect
import sys


class BackendPluginFactory(object):
    """
    This is a backend plugin factory a backend instance MUST be
    created via the static method create()
    ie : mybackend = BackendPluginFactory.create()
    """

    @classmethod
    def create(cls, plugin_name="mongodb", **kwargs):
        """Import the needed lib and return an object NmapBackendPlugin
        representing the backend of your desire.
        NmapBackendPlugin is an abstract class, to know what argument
        need to be given, review the code of the subclass you need
        :param plugin_name: str : name of the py file without .py
        :return: NmapBackend (abstract class on top of all plugin)
        """
        backendplugin = None
        plugin_path = "libnmap.plugins.{0}".format(plugin_name)
        __import__(plugin_path)
        pluginobj = sys.modules[plugin_path]
        pluginclasses = inspect.getmembers(pluginobj, inspect.isclass)
        for classname, classobj in pluginclasses:
            if inspect.getmodule(classobj).__name__.find(plugin_path) == 0:
                try:
                    backendplugin = classobj(**kwargs)
                except Exception as error:
                    raise Exception(
                        "Cannot create Backend {0}: {1}".format(
                            classname, error
                        )
                    )
        return backendplugin