<commit_before># Copyright 2013 - Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

from oslo.config import cfg

from mistral import context
from mistral.db.v2 import api as db_api
from mistral.services import trusts
from mistral import utils
from mistral.workbook import parser as spec_parser


WORKFLOWS_PATH = '../resources/workflows'


def register_standard_workflows():
    workflow_paths = utils.get_file_list(WORKFLOWS_PATH)

    for wf_path in workflow_paths:
        workflow_definition = open(wf_path).read()
        update_workflows(workflow_definition, scope='public')


def sync_db():
    register_standard_workflows()


def create_workflows(definition, scope='private'):
    wf_list_spec = spec_parser.get_workflow_list_spec_from_yaml(definition)

    db_wfs = []

    with db_api.transaction():
        for wf_spec in wf_list_spec.get_workflows():
            db_wfs.append(_create_workflow(wf_spec, definition, scope))

    return db_wfs


def update_workflows(definition, scope='private'):
    wf_list_spec = spec_parser.get_workflow_list_spec_from_yaml(definition)

    db_wfs = []

    with db_api.transaction():
        for wf_spec in wf_list_spec.get_workflows():
            db_wfs.append(_create_or_update_workflow(
                wf_spec,
                definition,
                scope
            ))

    return db_wfs


def _get_workflow_values(wf_spec, definition, scope):
    values = {
        'name': wf_spec.get_name(),
        'tags': wf_spec.get_tags(),
        'definition': definition,
        'spec': wf_spec.to_dict(),
        'scope': scope
    }

    _add_security_info(values)

    return values


def _create_workflow(wf_spec, definition, scope):
    return db_api.create_workflow(
        _get_workflow_values(wf_spec, definition, scope)
    )


def _create_or_update_workflow(wf_spec, definition, scope):
    values = _get_workflow_values(wf_spec, definition, scope)

    return db_api.create_or_update_workflow(values['name'], values)


def _add_security_info(values):
    if cfg.CONF.pecan.auth_enable and not values['name'].startswith('std.'):
        values.update({
            'trust_id': trusts.create_trust().id,
            'project_id': context.ctx().project_id
        })
<commit_msg>Fixing condition in workflow service<commit_after># Copyright 2013 - Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

from oslo.config import cfg

from mistral import context
from mistral.db.v2 import api as db_api
from mistral.services import trusts
from mistral import utils
from mistral.workbook import parser as spec_parser


WORKFLOWS_PATH = '../resources/workflows'


def register_standard_workflows():
    workflow_paths = utils.get_file_list(WORKFLOWS_PATH)

    for wf_path in workflow_paths:
        workflow_definition = open(wf_path).read()
        update_workflows(workflow_definition, scope='public')


def sync_db():
    register_standard_workflows()


def create_workflows(definition, scope='private'):
    wf_list_spec = spec_parser.get_workflow_list_spec_from_yaml(definition)

    db_wfs = []

    with db_api.transaction():
        for wf_spec in wf_list_spec.get_workflows():
            db_wfs.append(_create_workflow(wf_spec, definition, scope))

    return db_wfs


def update_workflows(definition, scope='private'):
    wf_list_spec = spec_parser.get_workflow_list_spec_from_yaml(definition)

    db_wfs = []

    with db_api.transaction():
        for wf_spec in wf_list_spec.get_workflows():
            db_wfs.append(_create_or_update_workflow(
                wf_spec,
                definition,
                scope
            ))

    return db_wfs


def _get_workflow_values(wf_spec, definition, scope):
    values = {
        'name': wf_spec.get_name(),
        'tags': wf_spec.get_tags(),
        'definition': definition,
        'spec': wf_spec.to_dict(),
        'scope': scope
    }

    _add_security_info(values, scope)

    return values


def _create_workflow(wf_spec, definition, scope):
    return db_api.create_workflow(
        _get_workflow_values(wf_spec, definition, scope)
    )


def _create_or_update_workflow(wf_spec, definition, scope):
    values = _get_workflow_values(wf_spec, definition, scope)

    return db_api.create_or_update_workflow(values['name'], values)


def _add_security_info(values, scope):
    if cfg.CONF.pecan.auth_enable and scope == 'private':
        values.update({
            'trust_id': trusts.create_trust().id,
            'project_id': context.ctx().project_id
        })

{'fraction_non_alphanumeric': 0.08637992831541219, 'fraction_numerical': 0.0032258064516129032, 'mean_word_length': 3.373824451410658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '3841002', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1694, 'n_words': 432}