<commit_before># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.
import os
import shutil

from gosa.backend.routes.sse.main import SseHandler
from gosa.common.event import EventMaker
from gosa.common import Environment
from gosa.common.error import GosaErrorHandler as C
from gosa.common.components import Plugin, PluginRegistry
from gosa.common.components.command import Command
from gosa.common.utils import N_
from gosa.backend.components.workflow import Workflow, WorkflowException
from lxml import objectify, etree
from pkg_resources import resource_filename

# Register the errors handled  by us
C.register_codes(dict(
    WORKFLOW_DIR_ERROR=N_("Workflow directory '%(path)s' does not exist"),
    WORKFLOW_PERMISSION_DELETE=N_("No permission to delete workflow '%(id)s'"),
    WORKFLOW_DELETE_ERROR=N_("Error removing workflow '%(id)s': %(error)s"),
    WORKFLOW_NOT_FOUND=N_("Workflow '%(id)s' does not exist")
))


"""
Workflow Registery
==================

TODO: documentation
"""
class WorkflowRegistry(Plugin):
    _target_ = "workflow"
    instance = None
    env = None

    @staticmethod
    def get_instance():
        if not WorkflowRegistry.instance:
            WorkflowRegistry.instance = WorkflowRegistry()
        return WorkflowRegistry.instance

    def __init__(self):
        self.env = Environment.getInstance()
        self.__path = self.env.config.get("core.workflow_path", "/var/lib/gosa/workflows")

        if not os.path.exists(self.__path) or not os.path.isdir(self.__path):
            raise WorkflowException(C.make_error('WORKFLOW_DIR_ERROR', path=self.__path))

        if WorkflowRegistry.instance is None:
            WorkflowRegistry.instance = self
            self._update_map()
        else:
            raise Exception("This is a singleton, please use getInstance instead")

    def stop(self):
        # called from PluginRegistry.shutdown()
        del WorkflowRegistry.instance

    @Command(needsUser=True, __help__=N_("List available workflows"))<commit_msg>create folder if it does not exist<commit_after># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.
import logging
import os
import shutil

from gosa.backend.routes.sse.main import SseHandler
from gosa.common.event import EventMaker
from gosa.common import Environment
from gosa.common.error import GosaErrorHandler as C
from gosa.common.components import Plugin, PluginRegistry
from gosa.common.components.command import Command
from gosa.common.utils import N_
from gosa.backend.components.workflow import Workflow, WorkflowException
from lxml import objectify, etree
from pkg_resources import resource_filename

# Register the errors handled  by us
C.register_codes(dict(
    WORKFLOW_DIR_ERROR=N_("Workflow directory '%(path)s' does not exist"),
    WORKFLOW_PERMISSION_DELETE=N_("No permission to delete workflow '%(id)s'"),
    WORKFLOW_DELETE_ERROR=N_("Error removing workflow '%(id)s': %(error)s"),
    WORKFLOW_NOT_FOUND=N_("Workflow '%(id)s' does not exist"),
    WORKFLOW_DIR_CREATION_ERROR=N_("Error creating workflow directory '%(path)s': %(error)s")
))


"""
Workflow Registery
==================

TODO: documentation
"""
class WorkflowRegistry(Plugin):
    _target_ = "workflow"
    instance = None
    env = None

    @staticmethod
    def get_instance():
        if not WorkflowRegistry.instance:
            WorkflowRegistry.instance = WorkflowRegistry()
        return WorkflowRegistry.instance

    def __init__(self):
        self.env = Environment.getInstance()
        self.log = logging.getLogger(__name__)
        self.__path = self.env.config.get("core.workflow_path", "/var/lib/gosa/workflows")

        if not os.path.exists(self.__path):
            # try to create dir
            try:
                os.mkdir(self.__path)
            except Exception as e:
                self.log.error(str(e))
                raise WorkflowException(C.make_error('WORKFLOW_DIR_CREATION_ERROR', path=self.__path, error=str(e)))

        if not os.path.exists(self.__path) or not os.path.isdir(self.__path):
            raise WorkflowException(C.make_error('WORKFLOW_DIR_ERROR', path=self.__path))

        if WorkflowRegistry.instance is None:
            WorkflowRegistry.instance = self
            self._update_map()
        else:
            raise Exception("This is a singleton, please use getInstance instead")

    def stop(self):
        # called from PluginRegistry.shutdown()
        del WorkflowRegistry.instance

    @Command(needsUser=True, __help__=N_("List available workflows"))
{'fraction_non_alphanumeric': 0.10401647785787847, 'fraction_numerical': 0.0016477857878475798, 'mean_word_length': 3.6737247353224256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1213445', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1408, 'n_words': 377}