예제 #1
0
import sys
from myDevices.utils.config import Config
from myDevices.utils.logger import setInfo, info, error
from myDevices.plugins.manager import PLUGIN_FOLDER

if __name__ == '__main__':
    # Run the code to disable a plugin in a script so it can be called via sudo
    setInfo()
    info(sys.argv)
    if len(sys.argv) != 3:
        error('Plugin not disabled, invalid arguments')
        sys.exit(1)
    filename = sys.argv[1]
    if filename.startswith(PLUGIN_FOLDER) and filename.endswith('.plugin'):
        section = sys.argv[2]
        config = Config(filename)
        if section in config.sections():
            config.set(section, 'enabled', 'false')
        else:
            error('Section \'{}\' not found in {}'.format(section, filename))
            sys.exit(1)
    else:
        sys.exit(1)
예제 #2
0
 def load_plugin_from_file(self, filename):
     """Loads a plugin from a specified plugin config file and adds it to the plugin list."""
     try:
         info('Loading plugin: {}'.format(filename))
         loaded = []
         config = Config(filename)
         plugin_name = os.path.splitext(os.path.basename(filename))[0]
         info('Sections: {}'.format(config.sections()))
         inherited_from = set()
         for section in config.sections():
             inherit = config.get(section, 'inherit', None)
             if inherit:
                 inherited_from.add(inherit)
         for section in config.sections():
             try:
                 enabled = config.get(section, 'enabled',
                                      'true').lower() == 'true'
                 inherit = config.get(section, 'inherit', None)
                 if enabled or section in inherited_from:
                     plugin = {
                         'enabled': enabled,
                         'filename': filename,
                         'section': section,
                         'name': config.get(section, 'name', section),
                     }
                     try:
                         plugin['channel'] = config.get(section, 'channel')
                         plugin[
                             'id'] = plugin_name + ':' + plugin['channel']
                     except NoOptionError:
                         plugin['id'] = plugin_name + ':' + section
                     inherit_items = {}
                     if inherit in config.sections():
                         if inherit == section:
                             raise ValueError(
                                 'Section \'{}\' cannot inherit from itself'
                                 .format(section))
                         inherit_from = self.get_plugin(filename, inherit)
                         inherit_items = {
                             key: value
                             for key, value in inherit_from.items()
                             if key not in plugin.keys()
                         }
                         plugin.update(inherit_items)
                     elif inherit:
                         raise ValueError(
                             'Section \'{}\' cannot inherit from \'{}\'. Check spelling and section ordering.'
                             .format(section, inherit))
                     self.override_plugin_value(config, section, 'module',
                                                plugin)
                     self.override_plugin_value(config, section, 'class',
                                                plugin)
                     if 'init_args' not in plugin:
                         plugin['init_args'] = '{}'
                     self.override_plugin_value(config, section,
                                                'init_args', plugin)
                     if not inherit_items or [
                             key for key in ('module', 'class', 'init_args')
                             if inherit_items[key] is not plugin[key]
                     ]:
                         info('Creating instance of {} for {}'.format(
                             plugin['class'], plugin['name']))
                         folder = os.path.dirname(filename)
                         if folder not in sys.path:
                             sys.path.append(folder)
                         imported_module = importlib.import_module(
                             plugin['module'])
                         device_class = getattr(imported_module,
                                                plugin['class'])
                         plugin['instance'] = device_class(
                             **json.loads(plugin['init_args']))
                     self.override_plugin_value(config, section, 'read',
                                                plugin)
                     if 'read_args' not in plugin:
                         plugin['read_args'] = '{}'
                     self.override_plugin_value(config, section,
                                                'read_args', plugin)
                     try:
                         self.override_plugin_value(config, section,
                                                    'write', plugin)
                         if 'write_args' not in plugin:
                             plugin['write_args'] = '{}'
                         self.override_plugin_value(config, section,
                                                    'write_args', plugin)
                     except:
                         pass
                     try:
                         self.override_plugin_value(config, section,
                                                    'set_function', plugin)
                     except:
                         pass
                     try:
                         self.override_plugin_value(config, section,
                                                    'register_callback',
                                                    plugin)
                         getattr(plugin['instance'],
                                 plugin['register_callback'])(
                                     lambda value, plugin=plugin: self.
                                     data_changed(value, plugin))
                     except:
                         pass
                     try:
                         self.override_plugin_value(config, section,
                                                    'unregister_callback',
                                                    plugin)
                     except:
                         pass
                     self.plugins[plugin['id']] = plugin
                     loaded.append(section)
             except Exception as e:
                 error(e)
     except Exception as e:
         error(e)
     info('Loaded sections: {}'.format(loaded))