Esempio n. 1
0
    def reload_plugin_from_disk(self):
        # Fetch list of installed plugins
        plugins = PluginsHandler()
        order = [{
            "column": 'name',
            "dir": 'asc',
        }]
        plugin_results = plugins.get_plugin_list_filtered_and_sorted(
            order=order, start=0, length=None)

        # Build choice selection list from installed plugins
        for plugin in plugin_results:
            plugin_path = os.path.join(self.plugins_directory,
                                       plugin.get('plugin_id'))
            # Read plugin info.json
            info_file = os.path.join(plugin_path, 'info.json')
            with open(info_file) as json_file:
                plugin_info = json.load(json_file)

            # Insert plugin details to DB
            try:
                PluginsHandler.write_plugin_data_to_db(plugin_info,
                                                       plugin_path)
            except Exception as e:
                print("Exception while saving plugin info to DB. - {}".format(
                    str(e)))
                return

            install_plugin_requirements(plugin_path)
        print()
        print()
Esempio n. 2
0
    def create_new_plugins(self):
        plugin_details = inquirer.prompt(menus.get('create_plugin'))

        # Ensure results are not empty
        if not plugin_details.get('plugin_name') or not plugin_details.get('plugin_id'):
            print("ERROR! Invalid input.")
            return

        # TODO: Ensure plugin ID has only underscore and a-z, 0-9

        # Create new plugin path
        new_plugin_path = os.path.join(self.plugins_directory, plugin_details.get('plugin_id'))
        if not os.path.exists(new_plugin_path):
            os.makedirs(new_plugin_path)

        # Touch main python file
        main_python_file = os.path.join(new_plugin_path, 'plugin.py')
        plugin_template = [
            "#!/usr/bin/env python3",
            "# -*- coding: utf-8 -*-",
            "",
            "from unmanic.libs.unplugins.settings import PluginSettings",
            "",
            "",
            "class Settings(PluginSettings):",
            "    settings = {}",
            "",
            ""
        ]
        if not os.path.exists(main_python_file):
            with open(main_python_file, 'a') as outfile:
                for plugin_template_line in plugin_template:
                    outfile.write("{}\n".format(plugin_template_line))

        common.touch(os.path.join(new_plugin_path, 'plugin.py'))
        # Create plugin info.json
        info_file = os.path.join(new_plugin_path, 'info.json')
        plugin_info = {
            "id":          plugin_details.get('plugin_id'),
            "name":        plugin_details.get('plugin_name'),
            "author":      "",
            "version":     "0.0.1",
            "tags":        "",
            "description": "",
            "icon":        ""
        }
        if not os.path.exists(info_file):
            with open(info_file, 'w') as outfile:
                json.dump(plugin_info, outfile, sort_keys=True, indent=4)

        # Insert plugin details to DB

        try:
            PluginsHandler.write_plugin_data_to_db(plugin_info, new_plugin_path)
        except Exception as e:
            print("Exception while saving plugin info to DB. - {}".format(str(e)))
            return

        print("Plugin created - '{}'".format((plugin_details.get('plugin_id'))))
Esempio n. 3
0
    def create_new_plugins(self):
        plugin_details = inquirer.prompt(menus.get('create_plugin'))

        # Ensure results are not empty
        if not plugin_details.get('plugin_name') or not plugin_details.get(
                'plugin_id'):
            print("ERROR! Invalid input.")
            return

        # Ensure plugin ID has only underscore and a-z, 0-9
        plugin_details['plugin_id'] = re.sub('[^0-9a-zA-Z]+', '_',
                                             plugin_details.get('plugin_id'))
        # Ensure plugin ID is lower case
        plugin_details['plugin_id'] = plugin_details.get('plugin_id').lower()

        # Get list of plugin types
        all_plugin_types = plugin_types.get_all_plugin_types()

        # Build choice selection list from installed plugins
        plugin_details_by_runner = {}
        choices = []
        for plugin_type in all_plugin_types:
            choices.append(all_plugin_types[plugin_type].get('name'))
            plugin_details_by_runner[all_plugin_types[plugin_type].get(
                'name')] = all_plugin_types[plugin_type]

        # Generate menu menu
        print()
        print(
            'INFO: https://docs.unmanic.app/docs/plugins/writing_plugins/plugin_runner_types'
        )
        plugin_runners_inquirer = inquirer.List(
            'selected_plugin',
            message="Which Plugin runner will be used?",
            choices=choices,
        )

        # Prompt for selection of Plugin by ID
        runner_selection = inquirer.prompt([plugin_runners_inquirer])

        # Fetch plugin type details from selection
        plugin_type_details = plugin_details_by_runner[runner_selection.get(
            'selected_plugin')]
        selected_plugin_runner = plugin_type_details.get('runner')
        selected_plugin_runner_docstring = plugin_type_details.get(
            'runner_docstring')

        # Create new plugin path
        new_plugin_path = os.path.join(self.plugins_directory,
                                       plugin_details.get('plugin_id'))
        if not os.path.exists(new_plugin_path):
            os.makedirs(new_plugin_path)

        # Create main python file template
        main_plugin_template = [
            "#!/usr/bin/env python3", "# -*- coding: utf-8 -*-", "",
            "from unmanic.libs.unplugins.settings import PluginSettings", "",
            "", "class Settings(PluginSettings):", "    settings = {}", "", ""
        ]

        # Create runner function template
        runner_template = [
            'def {}(data):'.format(selected_plugin_runner),
            '    """{}'.format(selected_plugin_runner_docstring),
            '    """',
            '    return data',
        ]

        # Write above templates to main python file
        main_python_file = os.path.join(new_plugin_path, 'plugin.py')
        if not os.path.exists(main_python_file):
            with open(main_python_file, 'a') as outfile:
                # Write out main template
                for template_line in main_plugin_template:
                    outfile.write("{}\n".format(template_line))
                # Write out runner function template
                for template_line in runner_template:
                    outfile.write("{}\n".format(template_line))

        # Write plugin info.json
        info_file = os.path.join(new_plugin_path, 'info.json')
        plugin_info = {
            "id": plugin_details.get('plugin_id'),
            "name": plugin_details.get('plugin_name'),
            "author": "",
            "version": "0.0.1",
            "tags": "",
            "description": "",
            "icon": ""
        }
        if not os.path.exists(info_file):
            with open(info_file, 'w') as outfile:
                json.dump(plugin_info, outfile, sort_keys=True, indent=4)

        # Create requirements.txt file
        common.touch(os.path.join(new_plugin_path, 'requirements.txt'))

        # Create Plugin .gitignore
        plugin_gitignore = os.path.join(new_plugin_path, '.gitignore')
        gitignore_template = [
            '**/__pycache__',
            '*.py[cod]',
            '**/site-packages',
            'settings.json',
        ]
        if not os.path.exists(plugin_gitignore):
            with open(plugin_gitignore, 'a') as outfile:
                for template_line in gitignore_template:
                    outfile.write("{}\n".format(template_line))

        # Insert plugin details to DB
        try:
            PluginsHandler.write_plugin_data_to_db(plugin_info,
                                                   new_plugin_path)
        except Exception as e:
            print("Exception while saving plugin info to DB. - {}".format(
                str(e)))
            return

        print("Plugin created - '{}'".format(
            (plugin_details.get('plugin_id'))))