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

from ppms.migration.packet import *
from ppms.migration import DeprecatedPythonPacket, MaxVersionDependency


class FindServerDictUsage(DeprecatedPythonPacket):
    '''Search Python code base for usage of "get_server_dict()".

    The function "get_server_dict()" can't be used at the moment, as
    the server dictionary is only session wide.
    This packet will always fail as long as there are still
    occurences of "get_server_dict()" in the system.

    This packet only serves as a source of documentation and
    writes its findings to the logfile.
    '''
    wi_number = 15387
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY

    def get_dependencies(self):
        return MaxVersionDependency(version='39.5.7'),
    
    def get_matching_objects(self, iterable_to_filter):
        search = lambda x: re.search('get_server_dict()', x.source)

        return list(filter(search, iterable_to_filter))

    @property
    def custom_server_file_blacklist(self):
        return ('wi15387_usage_of_server_dict.py', # exlude self
                'ppms.py',                         # exlude as function is defined there             
                'testGenericFunctions.py')         # exlude it's regtest