import os
import json

this_dir = os.path.dirname(__file__)


def string_list(val):
    return [item.strip() for item in val.split(',') if item.strip()]


def boolean(val):
    return val == "1"


class Profile(dict):
    """
    Convert profile json files to dict with params
    """
    COMPONENTS = ['brain', 'ecm', 'repair', 'sensor', 'weapons', 'body', 'propulsion', 'construction']

    def __init__(self, file_name):
        self.file_name = file_name
        with open(os.path.join(this_dir, '../ini_tools/fields.json')) as f:
            fields = json.load(f)

        with open(os.path.join(this_dir, '../ini_tools/profile', '%s.json' % file_name)) as f:
            data = json.load(f)

        self.field_order = []

        for field in data:
            self.field_order.append(field['name'])
            field_type = field['type']
            profile = fields[field_type].copy()
            profile.update(**field)
            self[field['name']] = profile

    def get_reference_keys(self, field_name):
        field = self[field_name]
        assert 'reference' in field, "%s field '%s' has missed reference" % (self.file_name, field_name)
        reference = field['reference']
        items = reference.split('|')
        result_keys = []
        for item in items:
            if item == 'component':
                result_keys.extend(self.COMPONENTS)
            else:
                result_keys.append(item)
        return result_keys


def get_profiles_name_list():
    return [name[:-5] for name in os.listdir(os.path.join(this_dir, '../ini_tools/profile')) if name.endswith('.json')]


def get_profiles():
    return [Profile(name[:-5]) for name in
            os.listdir(os.path.join(this_dir, '../ini_tools/profile')) if name.endswith('.json')]
@@ -380,16 +380,335 @@
 uction'%5D
+%0A    HANDLERS = %7B%0A        'string_list': string_list,%0A        'int': int,%0A        'bool': int,%0A        'str': str%0A    %7D%0A%0A    def get_deafult_handlers(self):%0A        handlers = %7B%7D%0A        for field, field_info in self.items():%0A            handlers%5Bfield%5D = self.HANDLERS%5Bfield_info%5B'convert_to'%5D%5D%0A        return handlers
 %0A%0A    de
@@ -1773,16 +1773,18 @@
 _keys%0A%0A%0A
+%0A%0A
 def get_

add handlers