from ppms.migration import PacketCategory, MaxVersionDependency, VersionType, ChangeDTPPacket

OLD_MACRO='''from ppms import ppms
from ppms import ppms_cu

#Macro module
module_macro = ppms.get_macro_module()

#Invoker module
mod_obj = ppms.get_target_module()

#Get module
mod_id = module_macro.get_invoker_rec().get_dtp_record().id.get_value()

#Create object of ppms_cu.Action class
ppms_cu_action = ppms_cu.Action(mod_obj)

#Create sub_mod_dictionary
main_mod_dict={}
main_mod_dict['000855'] = {'@L4':[mod_id]}

#Call submodule
ppms_cu_action.call_MAIN(mod_dict=main_mod_dict)['000855'].menu(12)
main_mod_dict["000855"] = { '@L4' : '' }'''

NEW_MACRO='''from ppms import ppms
from ppms import ppms_cu

#Macro module
module_macro = ppms.get_macro_module()

#Invoker module
mod_obj = ppms.get_target_module()

#Get module
if hasattr(module_macro.get_invoker_rec().get_dtp_record(),'id'):
    mod_id = module_macro.get_invoker_rec().get_dtp_record().id.get_value()
else:
    mod_id = module_macro.get_invoker_rec().get_dtp_record().module_id.get_value()

#Create object of ppms_cu.Action class
ppms_cu_action = ppms_cu.Action(mod_obj)

#Create sub_mod_dictionary
main_mod_dict={}
main_mod_dict['000855'] = {'@L4':[mod_id]}

#Call submodule
ppms_cu_action.call_MAIN(mod_dict=main_mod_dict)['000855'].menu(12)
main_mod_dict["000855"] = { '@L4' : '' }'''

class ReplaceMacroForMOD0099CN(ChangeDTPPacket):
    '''Fixes a Python-Error with checking for python-id in module 0099CN'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13772
    
    def get_dependencies(self):
        return (MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),)
    
    def fix(self, runner):
        record = self.get_record(405, ['0099CN'], ['py_macro_text'])
        if not self.change_value(record, 'py_macro_text', OLD_MACRO, NEW_MACRO):
            return self.fail('Could not change macro 0099CN - was it individualized?')
        return self.success()