# -*- coding: utf-8 -*-
import re

from ppms.migration.packet import *


MODULE_MACROS = {'009ABX':('91d308c32c2fa15e4c200cb6b3ecf5f5', '2d313634bf646bd864ade71da5a3f07e'),
                 '009AII':('6aa5e3a9909801777d2f51697bd4cb54', '83eeb9242854b91ba18d6d50c87d146b'),
                 '009BAS':('c7323ecbbd34bbdab5df72fff0997373', 'f264443102deac37ae6a22c6d200e64e'),
                 '009BI1':('7cdffc219d8b9b1374b1f0e226d1b15d', 'd55a60be2e7b09f32a66fb0976bd917c')}

VALUERANGES = {'057860':('d146d55be906dc74110aa15820bcc029', '2dd501774fef615130a94d1d36197885')}

DF_CONFIG = {('045835',714524):('407de86a24c47b407180ccac5d86c729', '2244d2a025fdd52b1ffaf8b710f4dfd4')}
GLOBAL_SETTINGS = ()
PROCESS_RULES = {'2':('9eec72c14f58ac60649c50b24a2490a8', 'd0589cf5696e680b9dcf4a8a0e24ecab'),
                 '10':('f472dcfc88d8b3502479e07cd9325bbd', '27c98519e8f93a47b24562e24bcbcce5'),
                 '11':('fd81eccaf4f861c310fe8c48941984da', '1f709f7c301b786509bda9631c4d9a61'),
                 '12':('97d9c0a2a8f87cd7f5dd0632665b3e5f', 'd8c4165790303b1e7577851e8d0f5e30')}

SUB = lambda code: re.sub('__builtins__', 'ppms.get_session_dict()', code, 0)

class FixBuiltins(MigratePythonPacket):
    '''Fix Python-Code in the PLANTA customizing that uses __builtins__
    
    This packet will fix Python-Code written by PLANTA that uses __builtins__ to use
    the new ppms.get_session_dict(). This packet might fail if the customizing was
    individualized.
    '''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10501

    def fix(self, runner):
        errors = 0

        self.log("Replacing __builtins__ in module macros.")
        for mod_id, (before_hash, after_hash) in MODULE_MACROS.items():
            if not self.migrate_python(before_hash, after_hash,
                                       self.CU_TYPE_MODULE, mod_id, SUB):
                errors += 1
                self.log("An error has occured while replacing __builtins__ in module macros.")

        self.log("Replacing __builtins__ in valueranges.")
        for di_nr, (before_hash, after_hash) in VALUERANGES.items():
            if not self.migrate_python(before_hash, after_hash,
                                       self.CU_TYPE_DATAITEM, di_nr, SUB):
                errors += 1
                self.log("An error has occured while replacing __builtins__ in valueranges.")

        self.log("Replacing __builtins__ in datafield configurations.")
        for (da_id, df_nr), (before_hash, after_hash) in DF_CONFIG.items():
            if not self.migrate_python(before_hash, after_hash,
                                       self.CU_TYPE_DF_CONFIG, [da_id, df_nr], SUB):
                errors += 1
                self.log("An error has occured while replacing __builtins__ in datafield configuration.")

        self.log("Replacing __builtins__ in process rules.")
        for process_rule_id, (before_hash, after_hash) in PROCESS_RULES.items():
            if not self.migrate_python(before_hash, after_hash,
                                       self.CU_TYPE_PROCESS_RULE, process_rule_id, SUB):
                errors += 1
                self.log("An error has occured while replacing __builtins__ in process rules.")

        if errors:
            return self.fail("%i errors have occured while replacing __builtins__." % errors)

        return self.success()