<filename>Python/empire/betsiamites/python/interpreter.py<gh_stars>0
from empire.betsiamites.python.lexer.tokenizer.tokenizer import Tokenizer


if __name__ == '__main__':
    interpreter = Tokenizer("""from typing import Union, Any, List
from rhetorik.system.rhetorikTypes import JSON

from rhetorik.system.structs.entities.entity import Entity
from rhetorik.system.structs.sources.source import Source
from rhetorik.system.structs.tasks.taskDeferred import TaskDeferred
from rhetorik.system.structs.tasks.taskSerialStandard import TaskSerialStandard
from rhetorik.system.structs.structUtil import StructUtil
from rhetorik.system.structs.iStruct import IStruct
from rhetorik.system.enums.taskEnum import TaskCategories

from rhetorik.basehandler.exceptions.handledException import HandledException
from rhetorik.utils.pythonUtil import PythonUtil
from rhetorik.utils.json.jsonUtil import JsonUtil
from rhetorik.utils.logger import *


class ItemConfiguration(IStruct):
    def __init__(self, configuration_as_dict: Union[JSON, None] = None):
        self._created: int = 0
        self._deferred: List[TaskDeferred] = list()
        self._deleted: bool = False
        self._entity: Entity = Entity()
        self._mappings: List[str] = list()
        self._name: str = ''
        self._organization_id: str = ''
        self._source: Source = Source()
        self._source_id: str = ''
        self._transformations: List[TaskSerialStandard] = list()
        self._updated: int = 0
        self._validations: List[TaskSerialStandard] = list()

        if configuration_as_dict is not None:
            self._initialize_configuration(configuration_as_dict=configuration_as_dict)

    def copy(self) -> Any:
        new: ItemConfiguration = ItemConfiguration()
        new._initialize_configuration(other_configuration=self)
        return new

    def _initialize_configuration(self, configuration_as_dict: Union[JSON, None] = None, other_configuration=None):
        if configuration_as_dict:
            try:
                self._created = configuration_as_dict.get('created', 0)
                self._deferred = [TaskDeferred(task) for task in configuration_as_dict.get('deferred', list())]
                self._deleted = configuration_as_dict.get('deleted', False)
                if 'entity' in configuration_as_dict and type(configuration_as_dict['entity']) is dict and len(configuration_as_dict['entity'].keys()) > 0:
                    self._entity = Entity(configuration_as_dict['entity'])
                self._mappings = [mapping for mapping in configuration_as_dict.get('mappings', list())]
                self._name = configuration_as_dict.get('name', '')
                self._organization_id = configuration_as_dict.get('organizationId', '')
                self._source = Source(configuration_as_dict.get('source', ''))
                self._source_id = configuration_as_dict.get('sourceId', '')
                self._transformations = [TaskSerialStandard(task) for task in configuration_as_dict.get('transformations', '')]
                self._updated = configuration_as_dict.get('updated', '')
                self._validations = [TaskSerialStandard(task) for task in configuration_as_dict.get('validations', '')]
            except Exception as error:
                if not isinstance(error, HandledException):
                    Log.fatal('Invalid dict', 'structs.ItemConfiguration', '_initialize_configuration', fatality=error, dict=configuration_as_dict)
                    raise HandledException(error)
                else:
                    Log.info('Raising HandledException', 'structs.ItemConfiguration', '_initialize_configuration')
                    raise error
        else:
            self._created = other_configuration.created_on
            self._deferred = [task.copy() for task in other_configuration.deferred_tasks]
            self._deleted = other_configuration.is_deleted
            self._entity = other_configuration.entity.copy()
            self._mappings = list(other_configuration.mappings)
            self._name = other_configuration.name
            self._organization_id = other_configuration.organization_id
            self._source = other_configuration.source.copy()
            self._source_id = other_configuration.source_id
            self._transformations = [task.copy() for task in other_configuration.transformation_tasks]
            self._updated = other_configuration.updated_on
            self._validations = [task.copy() for task in other_configuration.validation_tasks]

    @property
    def created_on(self) -> int:
        return self._created

    @property
    def is_deleted(self) -> bool:
        return self._deleted

    @property
    def deferred_tasks(self) -> List[TaskDeferred]:
        return self._deferred

    @property
    def has_been_deleted(self) -> bool:
        return self._deleted

    @property
    def entity(self) -> Entity:
        return self._entity

    @entity.setter
    def entity(self, entity: Entity) -> None:
        self._entity = entity

    @property
    def mappings(self) -> List[str]:
        return self._mappings

    @property
    def name(self) -> str:
        return self._name

    @property
    def organization_id(self) -> str:
        return self._organization_id

    @property
    def source(self) -> Source:
        return self._source

    @property
    def source_id(self) -> str:
        return self._source_id

    @property
    def transformation_tasks(self) -> List[TaskSerialStandard]:
        return self._transformations

    @property
    def updated_on(self) -> int:
        return self._updated

    @property
    def validation_tasks(self) -> List[TaskSerialStandard]:
        return self._validations

    def merge_json(self, dict_to_merge: JSON) -> None:
        self_dict: JSON = self.to_dict()
        if len(dict_to_merge.get('deferred', dict())) > 0:
            self_dict['deferred'] = JsonUtil.merge_dicts(self_dict['deferred'], dict_to_merge['deferred'])

        if len(dict_to_merge.get('mappings', list())) > 0:
            self_dict['mappings'].extend(dict_to_merge['mappings'])

        if len(dict_to_merge.get('source', dict())) > 0:
            self_dict['source'] = JsonUtil.merge_dicts(self_dict['source'], dict_to_merge['source'])

        if len(dict_to_merge.get('transformations', dict())) > 0:
            self_dict['transformations'] = JsonUtil.merge_dicts(self_dict['transformations'], dict_to_merge['transformations'])

        if len(dict_to_merge.get('validations', dict())) > 0:
            self_dict['validations'] = JsonUtil.merge_dicts(self_dict['validations'], dict_to_merge['validations'])

        self._initialize_configuration(self_dict)

    def get_tasks_by_category(self, category: str) -> List[Union[TaskDeferred, TaskSerialStandard]]:
        if category == TaskCategories.VALIDATION:
            return self._validations
        elif category == TaskCategories.TRANSFORMATION:
            return self._transformations
        elif category == TaskCategories.DEFERRED:
            return self._deferred
        else:
            return list()

    def to_dict(self) -> dict:
        return {
            'entity': self._entity.to_dict(),
            'deferred': [deferred.to_dict() for deferred in self._deferred],
            'updated': self._updated,
            'deleted': self._deleted,
            'transformations': [transformation.to_dict() for transformation in self._transformations],
            'organizationId': self._organization_id,
            'created': self._created,
            'source': self._source.to_dict(),
            'sourceId': self._source_id,
            'validations': [validation.to_dict() for validation in self._validations],
            'mappings': self._mappings,
            'name': self._name
        }

    def __str__(self) -> str:
        s: str = ''
        s += '\rCreated on: {}'.format(PythonUtil.format_full_date(self._created))
        s += f'\rName: {self._name}'
        s += '\rIs deleted: {}'.format('Yes' if self._deleted else 'No')
        s += f'\rSource ID: {self._source_id}'
        s += f'\rOrganization ID: {self._organization_id}'
        s += '\rUpdated on: {}'.format(PythonUtil.format_full_date(self._updated))
        s += '\rSource: ' + str(self._source).replace('\r', '\r\t')
        s += '\rMappings: ' + '\n\t'.join(self._mappings)
        s += '\rEntity: ' + str(self._entity).replace('\r', '\r\t')
        s += '\rValidations: ' + StructUtil.stringify_tasks(standard=self._validations)
        s += '\rTransformations: ' + StructUtil.stringify_tasks(standard=self._transformations)
        s += '\rDeferred: ' + StructUtil.stringify_tasks(deferred=self._deferred)

        return s

    def __repr__(self) -> str:
        return str(self)

    def __eq__(self, other) -> bool:
        try:
            return self._created == other.created_on and self._name == other.name and self._deleted == other.has_been_deleted and self._source_id == other.source_id and self._organization_id == other.organization_id and self._updated == other.updated_on and self._mappings == other.mappings and self._entity == other.entity and self._validations == other.validation_tasks and \
                   self._transformations == other.transformation_tasks and self._deferred == other.deferred_tasks
        except Exception:
            return False

    def __ne__(self, other) -> bool:
        return not (self == other)
""")

    interpreter.tokenize_code()
