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

from ppms.migration.packet import *


class FindBuiltinsUsagePacket(DeprecatedPythonPacket):
    '''Search Python code base for usage of "__builtins__".

    Usage of "__builtins__" should be avoided in favor of
    "ppms.get_session_dict()" since all threads/sessions use
    the same "__builtins__".
    This packet will always fail as long as there are still
    occurences of "__builtins__" in the system.

    Note, that since there is only one interpreter instance
    on the client, it is safe to use "__builtins__" in pure
    IronPython code.

    This packet only serves as a source of documentation and
    writes its findings to the logfile.
    '''

    wi_number = 10256
    version = 1
    category = PacketCategory.CATEGORY_HELPER

    def get_matching_objects(self, iterable_to_filter):
        search = lambda x: re.search('__builtins__', x.source)

        # Load the full thing into memory, we don't have that much source code :P
        return list(filter(search, iterable_to_filter))

    @property
    def custom_server_file_blacklist(self):
        return ('WI10501_replace_builtins.py', 'ppms.py', 'ppms_cu.py', 'module_macro_helper.py', 'validator.py', 'import_export_main.py', 'object_usage.py', 'get_file_from_client.py', 'dimmer.py')
