<commit_before># MIT License
#
# Copyright (c) 2016 <NAME> aka kronenthaler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import re


class DynamicObject(object):
    """
    Generic class that creates internal attributes to match the structure of the tree used to create the element.
    Also, prints itself using the openstep format. Extensions might be required to insert comments on right places.
    """
    _VALID_KEY_REGEX = '[a-zA-Z0-9\\._/-]*'

    def parse(self, value):
        if isinstance(value, dict):
            return self._parse_dict(value)

        return value

    def _parse_dict(self, obj):
        # all top level objects are added as variables to this object
        for key, value in obj.iteritems():
            if not hasattr(self, key):
                setattr(self, key, self._get_instance(key, value))

        return self

    def _get_instance(self, type, content):
        # check if the key maps to a kind of object
        module = __import__("mod_pbxproj2")
        if hasattr(module, type):
            class_ = getattr(module, type)
            return class_().parse(content)

        return DynamicObject().parse(content)

    def __repr__(self):
        return self.print_object()

    def print_object(self, indentation_depth="", single_line=False):
        entry_separator = "\n"
        object_start = "\n"
        indentation_increment = "\t"

        if single_line:
            entry_separator = " "
            object_start = ""
            indentation_increment = ""

        ret = "{" + object_start
        for key in [x for x in dir(self) if not x.startswith("_") and not hasattr(getattr(self, x), '__call__')]:
            value = getattr(self, key)
            if hasattr(value, 'print_object'):
                value = value.print_object(indentation_depth + indentation_increment)
            elif isinstance(value, list):
                value = self._print_list(value, indentation_depth + indentation_increment)
            else:
                value = DynamicObject._escape(value.__str__())

            # use key decorators, could simplify the generation of the comments.
            ret += indentation_depth + "{3}{0} = {1};{2}".format(DynamicObject._escape(key), value, entry_separator, indentation_increment)
        ret += indentation_depth + "}"
        return ret

    def _print_list(self, value, indent):
        ret = "(\n"
        for item in value:
            ret += indent+"\t{0},\n".format(item)
        ret += indent+")"<commit_msg>wip: allow arrays to be printed on a single line<commit_after># MIT License
#
# Copyright (c) 2016 <NAME> aka kronenthaler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import re


class DynamicObject(object):
    """
    Generic class that creates internal attributes to match the structure of the tree used to create the element.
    Also, prints itself using the openstep format. Extensions might be required to insert comments on right places.
    """
    _VALID_KEY_REGEX = '[<KEY>]*'

    def parse(self, value):
        if isinstance(value, dict):
            return self._parse_dict(value)

        return value

    def _parse_dict(self, obj):
        # all top level objects are added as variables to this object
        for key, value in obj.iteritems():
            if not hasattr(self, key):
                setattr(self, key, self._get_instance(key, value))

        return self

    def _get_instance(self, type, content):
        # check if the key maps to a kind of object
        module = __import__("mod_pbxproj2")
        if hasattr(module, type):
            class_ = getattr(module, type)
            return class_().parse(content)

        return DynamicObject().parse(content)

    def __repr__(self):
        return self.print_object()

    def print_object(self, indentation_depth="", single_line=False):
        entry_separator = object_start = "\n"
        indentation_increment = "\t"

        if single_line:
            entry_separator = " "
            object_start = indentation_increment = ""

        ret = "{" + object_start
        for key in [x for x in dir(self) if not x.startswith("_") and not hasattr(getattr(self, x), '__call__')]:
            value = getattr(self, key)
            if hasattr(value, 'print_object'):
                value = value.print_object(indentation_depth + indentation_increment)
            elif isinstance(value, list):
                value = self._print_list(value, indentation_depth + indentation_increment)
            else:
                value = DynamicObject._escape(value.__str__())

            # use key decorators, could simplify the generation of the comments.
            ret += indentation_depth + "{3}{0} = {1};{2}".format(DynamicObject._escape(key), value, entry_separator, indentation_increment)
        ret += indentation_depth + "}"
        return ret

    def _print_list(self, value, indentation_depth="", single_line=False):
        entry_separator = object_start = "\n"
        indentation_increment = "\t"

        if single_line:
            entry_separator = " "
            object_start = indentation_increment = ""

        ret = "(" + object_start
        for item in value:
            ret += indentation_depth + "{1}{0},{2}".format(item, indentation_increment, entry_separator)
        ret += indentation_depth + ")"
{'fraction_non_alphanumeric': 0.08521031207598372, 'fraction_numerical': 0.0032564450474898234, 'mean_word_length': 2.8876582278481013, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1844645', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1912, 'n_words': 761}