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

from ppms.migration.packet import *

BEFORE_HASH = 'fd01cd51262f1d7d56c1c042071cdec3'
AFTER_HASH = 'd41d8cd98f00b204e9800998ecf8427e'
SUB = lambda _: ''


class ChangePythonReloaderMacro(MigratePythonPacket, ChangeDTPPacket, SQLPacket):
    '''Migrate the new Python reloader macro from Venus to Earth'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 8353

    fname = 'wi8353_python_reloader.sql'

    def fix(self, runner):
        failures = 0

        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.fname)
        queries = self.get_queries_from_file(sqlpath, encoding='utf-8')

        for query in queries:
            if not self.modify_and_log(query):
                failures += 1

        record = self.get_record(405, ['009BBA'], [56952])
        if not self.change_value(record, 'python_module_subclass', '',
                                 'ppms.module_subclasses.macros.reloader.ReloaderMacro') and \
                not self.change_value(record, 'python_module_subclass', 'ppms.module_subclasses.base_class.Base',
                                      'ppms.module_subclasses.macros.reloader.ReloaderMacro'):
            failures += 1

        if not self.migrate_python(BEFORE_HASH, AFTER_HASH,
                                   self.CU_TYPE_MODULE, '009BBA', SUB):
            failures += 1

        if failures:
            return self.fail()

        return self.success()
