<commit_before># -*- coding: utf-8 -*-

""" pyKwalify - Rule.py """

__author__ = 'Grokzen <<EMAIL>>'

# python std lib
import os
import re

# python std logging
import logging
Log = logging.getLogger(__name__)

# pyKwalify imports
from pykwalify.types import DEFAULT_TYPE, typeClass, isBuiltinType, isCollectionType


class Rule(object):
    """ Rule class that handles a rule constraint """

    def __init__(self, schema=None, parent=None):
        self._parent = None
        self._name = None
        self._desc = None
        self._required = False
        self._type = None
        self._type_class = None
        self._pattern = None
        self._pattern_regexp = None
        self._enum = None
        self._sequence = None
        self._mapping = None
        self._assert = None
        self._range = None
        self._length = None
        self._ident = None
        self._unique = None
        self._default = None
        self._allowempty_map = None
        self._matching_rule = None
        self._map_regex_rule = None
        self._regex_mappings = None

        self._parent = parent
        self._schema = schema
        self._schema_str = schema

        if isinstance(schema, dict):
            self.init(schema, "")

    def __str__(self):
        return "Rule: {}".format(str(self._schema_str))

    def init(self, schema, path):
        Log.debug("Init schema: {}".format(schema))

        if schema is not None:
            # assert isinstance(schema, dict), "schema is not a dict : {}".format(path)

            if "type" not in schema:
                raise Exception("key 'type' not found in schema rule : {}".format(path))
            else:
                if not isinstance(schema["type"], str):
                    raise Exception("key 'type' in schema rule is not a string type : {}".format(path))

                self._type = schema["type"]

        rule = self

        self._schema_str = schema

        t = schema["type"]
        self.initTypeValue(t, rule, path)

        func_mapping = {
            "type": lambda x, y, z: (),
            "name": self.initNameValue,
            "desc": self.initDescValue,
            "required": self.initRequiredValue,
            "pattern": self.initPatternValue,
            "enum": self.initEnumValue,
            "assert": self.initAssertValue,
            "range": self.initRangeValue,
            "length": self.initRangeValue,
            "ident": self.initIdentValue,
            "unique": self.initUniqueValue,
            "allowempty": self.initAllowEmptyMap,
            "default": self.initDefaultValue,
            "sequence": self.initSequenceValue,
            "mapping": self.initMappingValue,
            "matching-rule": self.initMatchingRule,
        }

        for k, v in schema.items():
            if k in func_mapping:
                func_mapping[k](v, rule, path)
            else:
                raise Exception("Unknown key: {} found : {}".format(k, path))

        self.checkConfliction(schema, rule, path)

    def initMatchingRule(self, v, rule, path):
        Log.debug("Init matching-rule: {}".format(path))
        Log.debug("{} {}".format(v, rule))

        # Verify that the provided rule is part of one of the allowed one
        allowed = ["any"]
        # ["none", "one", "all"] Is currently awaiting proper implementation
        if v not in allowed:<commit_msg>Fixed typo in previous code refactoring<commit_after># -*- coding: utf-8 -*-

""" pyKwalify - Rule.py """

__author__ = 'Grokzen <<EMAIL>>'

# python std lib
import os
import re

# python std logging
import logging
Log = logging.getLogger(__name__)

# pyKwalify imports
from pykwalify.types import DEFAULT_TYPE, typeClass, isBuiltinType, isCollectionType


class Rule(object):
    """ Rule class that handles a rule constraint """

    def __init__(self, schema=None, parent=None):
        self._parent = None
        self._name = None
        self._desc = None
        self._required = False
        self._type = None
        self._type_class = None
        self._pattern = None
        self._pattern_regexp = None
        self._enum = None
        self._sequence = None
        self._mapping = None
        self._assert = None
        self._range = None
        self._length = None
        self._ident = None
        self._unique = None
        self._default = None
        self._allowempty_map = None
        self._matching_rule = None
        self._map_regex_rule = None
        self._regex_mappings = None

        self._parent = parent
        self._schema = schema
        self._schema_str = schema

        if isinstance(schema, dict):
            self.init(schema, "")

    def __str__(self):
        return "Rule: {}".format(str(self._schema_str))

    def init(self, schema, path):
        Log.debug("Init schema: {}".format(schema))

        if schema is not None:
            # assert isinstance(schema, dict), "schema is not a dict : {}".format(path)

            if "type" not in schema:
                raise Exception("key 'type' not found in schema rule : {}".format(path))
            else:
                if not isinstance(schema["type"], str):
                    raise Exception("key 'type' in schema rule is not a string type : {}".format(path))

                self._type = schema["type"]

        rule = self

        self._schema_str = schema

        t = schema["type"]
        self.initTypeValue(t, rule, path)

        func_mapping = {
            "type": lambda x, y, z: (),
            "name": self.initNameValue,
            "desc": self.initDescValue,
            "required": self.initRequiredValue,
            "pattern": self.initPatternValue,
            "enum": self.initEnumValue,
            "assert": self.initAssertValue,
            "range": self.initRangeValue,
            "length": self.initLengthValue,
            "ident": self.initIdentValue,
            "unique": self.initUniqueValue,
            "allowempty": self.initAllowEmptyMap,
            "default": self.initDefaultValue,
            "sequence": self.initSequenceValue,
            "mapping": self.initMappingValue,
            "matching-rule": self.initMatchingRule,
        }

        for k, v in schema.items():
            if k in func_mapping:
                func_mapping[k](v, rule, path)
            else:
                raise Exception("Unknown key: {} found : {}".format(k, path))

        self.checkConfliction(schema, rule, path)

    def initMatchingRule(self, v, rule, path):
        Log.debug("Init matching-rule: {}".format(path))
        Log.debug("{} {}".format(v, rule))

        # Verify that the provided rule is part of one of the allowed one
        allowed = ["any"]
        # ["none", "one", "all"] Is currently awaiting proper implementation
        if v not in allowed:
{'fraction_non_alphanumeric': 0.11194579466784504, 'fraction_numerical': 0.0002945941964943291, 'mean_word_length': 2.0097517730496453, 'pattern_counts': {'":': 32, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7084389', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1820, 'n_words': 526}