BVB Source Codes

voltron Show plugin.py Source code

Return Download voltron: download plugin.py Source code - Download voltron Source code - Type:.py
  1. import logging
  2. import inspect
  3. import os
  4. from collections import defaultdict
  5.  
  6. from scruffy.plugin import Plugin
  7.  
  8. import voltron
  9.  
  10. log = logging.getLogger('plugin')
  11.  
  12.  
  13. class PluginManager(object):
  14.     """
  15.    Collects and validates API, debugger and view plugins. Provides methods to
  16.    access and search the plugin collection.
  17.  
  18.    Plugin loading itself is handled by scruffy, which is configured in the
  19.    environment specification in `env.py`.
  20.    """
  21.     def __init__(self):
  22.         """
  23.        Initialise a new PluginManager.
  24.        """
  25.         self._api_plugins = defaultdict(lambda: None)
  26.         self._debugger_plugins = defaultdict(lambda: None)
  27.         self._view_plugins = defaultdict(lambda: None)
  28.         self._web_plugins = defaultdict(lambda: None)
  29.         self._command_plugins = defaultdict(lambda: None)
  30.  
  31.     def register_plugins(self):
  32.         for p in voltron.env.plugins:
  33.             self.register_plugin(p)
  34.  
  35.     def register_command_plugins(self):
  36.         for p in voltron.env.plugins:
  37.             if issubclass(p, CommandPlugin):
  38.                 self.register_plugin(p)
  39.  
  40.     @property
  41.     def api_plugins(self):
  42.         return self._api_plugins
  43.  
  44.     @property
  45.     def debugger_plugins(self):
  46.         return self._debugger_plugins
  47.  
  48.     @property
  49.     def view_plugins(self):
  50.         return self._view_plugins
  51.  
  52.     @property
  53.     def web_plugins(self):
  54.         return self._web_plugins
  55.  
  56.     @property
  57.     def command_plugins(self):
  58.         return self._command_plugins
  59.  
  60.     def register_plugin(self, plugin):
  61.         """
  62.        Register a new plugin with the PluginManager.
  63.  
  64.        `plugin` is a subclass of scruffy's Plugin class.
  65.  
  66.        This is called by __init__(), but may also be called by the debugger
  67.        host to load a specific plugin at runtime.
  68.        """
  69.         if hasattr(plugin, 'initialise'):
  70.             plugin.initialise()
  71.         if self.valid_api_plugin(plugin):
  72.             log.debug("Registering API plugin: {}".format(plugin))
  73.             self._api_plugins[plugin.request] = plugin()
  74.         elif self.valid_debugger_plugin(plugin):
  75.             log.debug("Registering debugger plugin: {}".format(plugin))
  76.             self._debugger_plugins[plugin.host] = plugin()
  77.         elif self.valid_view_plugin(plugin):
  78.             log.debug("Registering view plugin: {}".format(plugin))
  79.             self._view_plugins[plugin.name] = plugin()
  80.         elif self.valid_web_plugin(plugin):
  81.             log.debug("Registering web plugin: {}".format(plugin))
  82.             self._web_plugins[plugin.name] = plugin()
  83.         elif self.valid_command_plugin(plugin):
  84.             log.debug("Registering command plugin: {}".format(plugin))
  85.             self._command_plugins[plugin.name] = plugin()
  86.             if voltron.debugger:
  87.                 voltron.debugger.register_command_plugin(plugin.name, plugin.command_class)
  88.         else:
  89.             log.debug("Ignoring invalid plugin: {}".format(plugin))
  90.  
  91.     def valid_api_plugin(self, plugin):
  92.         """
  93.        Validate an API plugin, ensuring it is an API plugin and has the
  94.        necessary fields present.
  95.  
  96.        `plugin` is a subclass of scruffy's Plugin class.
  97.        """
  98.         if (issubclass(plugin, APIPlugin)       and
  99.             hasattr(plugin, 'plugin_type')      and plugin.plugin_type == 'api' and
  100.             hasattr(plugin, 'request')          and plugin.request != None and
  101.             hasattr(plugin, 'request_class')    and plugin.request_class != None and
  102.             hasattr(plugin, 'response_class')   and plugin.response_class != None):
  103.             return True
  104.         return False
  105.  
  106.     def valid_debugger_plugin(self, plugin):
  107.         """
  108.        Validate a debugger plugin, ensuring it is a debugger plugin and has
  109.        the necessary fields present.
  110.  
  111.        `plugin` is a subclass of scruffy's Plugin class.
  112.        """
  113.         if (issubclass(plugin, DebuggerAdaptorPlugin) and
  114.             hasattr(plugin, 'plugin_type')      and plugin.plugin_type == 'debugger' and
  115.             hasattr(plugin, 'host')             and plugin.host != None):
  116.             return True
  117.         return False
  118.  
  119.     def valid_view_plugin(self, plugin):
  120.         """
  121.        Validate a view plugin, ensuring it is a view plugin and has the
  122.        necessary fields present.
  123.  
  124.        `plugin` is a subclass of scruffy's Plugin class.
  125.        """
  126.         if (issubclass(plugin, ViewPlugin)      and
  127.             hasattr(plugin, 'plugin_type')      and plugin.plugin_type == 'view' and
  128.             hasattr(plugin, 'name')             and plugin.name != None and
  129.             hasattr(plugin, 'view_class')       and plugin.view_class != None):
  130.             return True
  131.         return False
  132.  
  133.     def valid_web_plugin(self, plugin):
  134.         """
  135.        Validate a web plugin, ensuring it is a web plugin and has the
  136.        necessary fields present.
  137.  
  138.        `plugin` is a subclass of scruffy's Plugin class.
  139.        """
  140.         if (issubclass(plugin, WebPlugin)      and
  141.             hasattr(plugin, 'plugin_type')      and plugin.plugin_type == 'web' and
  142.             hasattr(plugin, 'name')             and plugin.name != None):
  143.             return True
  144.         return False
  145.  
  146.     def valid_command_plugin(self, plugin):
  147.         """
  148.        Validate a command plugin, ensuring it is a command plugin and has the
  149.        necessary fields present.
  150.  
  151.        `plugin` is a subclass of scruffy's Plugin class.
  152.        """
  153.         if (issubclass(plugin, CommandPlugin)   and
  154.             hasattr(plugin, 'plugin_type')      and plugin.plugin_type == 'command' and
  155.             hasattr(plugin, 'name')             and plugin.name != None):
  156.             return True
  157.         return False
  158.  
  159.     def api_plugin_for_request(self, request=None):
  160.         """
  161.        Find an API plugin that supports the given request type.
  162.        """
  163.         return self.api_plugins[request]
  164.  
  165.     def debugger_plugin_for_host(self, host=None):
  166.         """
  167.        Find a debugger plugin that supports the debugger host.
  168.        """
  169.         return self.debugger_plugins[host]
  170.  
  171.     def view_plugin_with_name(self, name=None):
  172.         """
  173.        Find a view plugin that for the given view name.
  174.        """
  175.         return self.view_plugins[name]
  176.  
  177.     def web_plugin_with_name(self, name=None):
  178.         """
  179.        Find a web plugin that for the given view name.
  180.        """
  181.         return self.web_plugins[name]
  182.  
  183.     def command_plugin_with_name(self, name=None):
  184.         """
  185.        Find a command plugin that for the given view name.
  186.        """
  187.         return self.command_plugins[name]
  188.  
  189.  
  190. class VoltronPlugin(Plugin):
  191.     @classmethod
  192.     def initialise(cls):
  193.         pass
  194.  
  195.  
  196. class APIPlugin(VoltronPlugin):
  197.     """
  198.    Abstract API plugin class. API plugins subclass this.
  199.  
  200.    `plugin_type` is 'api'
  201.    `request` is the request type (e.g. 'version')
  202.    `request_class` is the APIRequest subclass (e.g. APIVersionRequest)
  203.    `response_class` is the APIResponse subclass (e.g. APIVersionResponse)
  204.    `supported_hosts` is an array of debugger adaptor plugins that this plugin
  205.    supports (e.g. 'lldb', 'gdb'). There is also a special host type called
  206.    'core' If the plugin requires only the 'core' debugger host, it can be used
  207.    with any debugger adaptor plugin that implements the full interface (ie.
  208.    the included 'lldb' and 'gdb' plugins). If it requires a specifically named
  209.    debugger host plugin, then it will only work with those plugins specified.
  210.    This allows developers to add custom API plugins that communicate directly
  211.    with their chosen debugger host API, to do things that the standard
  212.    debugger adaptor plugins don't support.
  213.  
  214.    See the core API plugins in voltron/plugins/api/ for examples.
  215.    """
  216.     plugin_type = 'api'
  217.     request = None
  218.     request_class = None
  219.     response_class = None
  220.     supported_hosts = ['core']
  221.  
  222.     @classmethod
  223.     def initialise(cls):
  224.         if cls.request_class:
  225.             cls.request_class._plugin = cls
  226.             cls.request_class.request = cls.request
  227.  
  228.  
  229. class DebuggerAdaptorPlugin(VoltronPlugin):
  230.     """
  231.    Debugger adaptor plugin parent class.
  232.  
  233.    `plugin_type` is 'debugger'
  234.    `host` is the name of the debugger host (e.g. 'lldb' or 'gdb')
  235.    `adaptor_class` is the debugger adaptor class that can be queried
  236.    See the core debugger plugins in voltron/plugins/debugger/ for examples.
  237.    """
  238.     plugin_type = 'debugger'
  239.     host = None
  240.     adaptor_class = None
  241.     supported_hosts = ['core']
  242.  
  243.     @classmethod
  244.     def initialise(cls):
  245.         if cls.adaptor_class:
  246.             cls.adaptor_class._plugin = cls
  247.  
  248.  
  249. class ViewPlugin(VoltronPlugin):
  250.     """
  251.    View plugin parent class.
  252.  
  253.    `plugin_type` is 'view'
  254.    `name` is the name of the view (e.g. 'register' or 'disassembly')
  255.    `view_class` is the main view class
  256.  
  257.    See the core view plugins in voltron/plugins/view/ for examples.
  258.    """
  259.     plugin_type = 'view'
  260.     name = None
  261.     view_class = None
  262.  
  263.     @classmethod
  264.     def initialise(cls):
  265.         if cls.view_class:
  266.             cls.view_class._plugin = cls
  267.             cls.view_class.view_type = cls.name
  268.  
  269.  
  270. class WebPlugin(VoltronPlugin):
  271.     """
  272.    Web plugin parent class.
  273.  
  274.    `plugin_type` is 'web'
  275.    `name` is the name of the web plugin (e.g. 'webview')
  276.    `app` is a Flask app (or whatever, optional)
  277.    """
  278.     _dir = None
  279.  
  280.     plugin_type = 'web'
  281.     name = None
  282.     app = None
  283.  
  284.     def __init__(self):
  285.         self._dir = os.path.dirname(inspect.getfile(self.__class__))
  286.  
  287.  
  288. class CommandPlugin(VoltronPlugin):
  289.     """
  290.    Command plugin parent class.
  291.  
  292.    `plugin_type` is 'command'
  293.    `name` is the name of the command plugin
  294.    """
  295.     plugin_type = 'command'
  296.     name = None
  297.  
  298.  
  299. class VoltronCommand(object):
  300.     pass
  301.  
  302.  
  303. #
  304. # Shared plugin manager and convenience methods
  305. #
  306.  
  307. pm = PluginManager()
  308.  
  309.  
  310. def api_request(request, *args, **kwargs):
  311.     """
  312.    Create an API request.
  313.  
  314.    `request_type` is the request type (string). This is used to look up a
  315.    plugin, whose request class is instantiated and passed the remaining
  316.    arguments passed to this function.
  317.    """
  318.     plugin = pm.api_plugin_for_request(request)
  319.     if plugin and plugin.request_class:
  320.         req = plugin.request_class(*args, **kwargs)
  321.     else:
  322.         raise Exception("Invalid request type")
  323.     return req
  324.  
  325.  
  326. def api_response(request, *args, **kwargs):
  327.     plugin = pm.api_plugin_for_request(request)
  328.     if plugin and plugin.response_class:
  329.         req = plugin.response_class(*args, **kwargs)
  330.     else:
  331.         raise Exception("Invalid request type")
  332.     return req
  333.  
  334.  
  335. def debugger_adaptor(host, *args, **kwargs):
  336.     plugin = pm.debugger_plugin_for_host(host)
  337.     if plugin and plugin.adaptor_class:
  338.         adaptor = plugin.adaptor_class(*args, **kwargs)
  339.     else:
  340.         raise Exception("Invalid debugger host")
  341.     return adaptor
  342.  
  343.  
  344. def view(name, *args, **kwargs):
  345.     plugin = pm.view_plugin_with_name(name)
  346.     if plugin and plugin.view_class:
  347.         view = plugin.view_class(*args, **kwargs)
  348.     else:
  349.         raise Exception("Invalid view name")
  350.     return view
  351.  
  352.  
  353. def command(name, *args, **kwargs):
  354.     plugin = pm.command_plugin_with_name(name)
  355.     if plugin and plugin.command_class:
  356.         command = plugin.command_class(*args, **kwargs)
  357.     else:
  358.         raise Exception("Invalid command name")
  359.     return command
  360.  
  361.  
  362. def web_plugins():
  363.     return pm.web_plugins
  364.  
downloadplugin.py Source code - Download voltron Source code
Related Source Codes/Software:
IGListKit - A data-driven UICollectionView framework for build... 2017-01-11
androidmvp - MVP Android Example 2017-01-11
svelte - The magical disappearing UI framework ... 2017-01-11
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
poisontap - Exploits locked/password protected computers over ... 2017-01-10
ecma262 - Status, process, and documents for ECMA262 ... 2017-01-11
react-native-elements - React Native Elements UI Toolkit 2017-01-11
tensorflow-zh - Google new open source ai system TensorFlow Chines... 2017-01-11
forum - Blue light (the Lantern) the official BBS 2017-01-11
socketcluster - Highly scalable realtime framework ... 2017-01-11
mobile-chrome-apps - Chrome apps on Android and iOS 2017-02-20
pycrumbs - Bits and bytes of Python from the Internet 2017-02-20
drupal - Verbatim mirror of the git.drupal.org repository f... 2017-02-20
elasticsearch-head - A web front end for an elastic search cluster 2017-02-19
launchrocket - A Mac PrefPane to manage all your Homebrew-install... 2017-02-19
jquery-boilerplate - A jump-start for jQuery plugins development ... 2017-02-19
MVVMReactiveCocoa - GitBucket iOS A 2017-02-19
ios-webkit-debug-proxy - A DevTools proxy (Chrome Remote Debugging Protocol... 2017-02-19
jump.js - A modern smooth scrolling library. ... 2017-02-19
GoSublime - A Golang plugin collection for SublimeText **3**, ... 2017-02-19

 Back to top