Пример #1
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'))))
Пример #2
0
    def test_read_file_info_for_failure(self):
        """
        Ensure that and exception is thrown if ffprobe is unable to read a file.

        :return:
        """
        # Set project root path
        tmp_dir = self.tests_tmp_dir
        fail_file = os.path.join(tmp_dir, 'test_failure.mkv')
        # Test
        common.ensure_dir(fail_file)
        common.touch(fail_file)
        with pytest.raises(unffmpeg.exceptions.ffprobe.FFProbeError):
            self.ffmpeg.file_probe(fail_file)
Пример #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'))))