|
| 1 | +from inspect import isclass |
| 2 | + |
| 3 | +primitives = [str, int, float, list, dict, bool, type(None)] |
| 4 | + |
| 5 | +def cast_primitive(value, get_type=False): |
| 6 | + if value in primitives: return str(value) if not get_type else value |
| 7 | + determine = None |
| 8 | + try: |
| 9 | + determine = type(value) |
| 10 | + if determine not in primitives: determine = determine.__bases__[0] |
| 11 | + except: pass |
| 12 | + if determine not in primitives: raise Exception('Cannot find a primitive type for: ' + str(value)) |
| 13 | + if get_type: |
| 14 | + if determine is None or type(None) == determine: return lambda: None |
| 15 | + return determine |
| 16 | + return str(determine) |
| 17 | + |
| 18 | +def generate_frozenset(structure): return frozenset({key: original_primitives.get(structure[key], structure[key]) if not isinstance(structure[key], JSObject) else structure[key].__serialize_structure__() for key in structure}.items()) |
| 19 | + |
| 20 | +list_hash = hash(list) |
| 21 | +dict_hash = hash(dict) |
| 22 | + |
| 23 | +object_cache = {} |
| 24 | + |
| 25 | +class JSObject: |
| 26 | + |
| 27 | + structure = None |
| 28 | + |
| 29 | + def __call__(self, structure=None): |
| 30 | + class_structure = structure |
| 31 | + class Object(JSObject): |
| 32 | + |
| 33 | + structure = class_structure |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def __serialize_structure__(self): |
| 37 | + if not self.structure: return frozenset() |
| 38 | + return generate_frozenset(self.structure) |
| 39 | + |
| 40 | + if not structure: |
| 41 | + if None in object_cache: return object_cache[None] |
| 42 | + object_cache[None] = Object |
| 43 | + return Object |
| 44 | + for key in structure: |
| 45 | + setattr(Object, key, cast_primitive(structure[key], get_type=True)() if not isclass(structure[key]) or not issubclass(structure[key], JSObject) else None) |
| 46 | + structure_serialized = generate_frozenset(structure) |
| 47 | + if structure_serialized in object_cache: return object_cache[structure_serialized] |
| 48 | + object_cache[structure_serialized] = Object |
| 49 | + return Object |
| 50 | + |
| 51 | +Object = JSObject() |
| 52 | + |
| 53 | +list_cache = {} |
| 54 | + |
| 55 | +class List(list): |
| 56 | + |
| 57 | + current_type = None |
| 58 | + |
| 59 | + def __getitem__(self, type): |
| 60 | + if isinstance(type, tuple): raise Exception('Expected one type for List') |
| 61 | + current_type = cast_primitive(type) |
| 62 | + if current_type in list_cache: return list_cache[current_type] |
| 63 | + new = ListClass() |
| 64 | + new.current_type = current_type |
| 65 | + list_cache[current_type] = new |
| 66 | + return new |
| 67 | + |
| 68 | + def __hash__(self): |
| 69 | + return list_hash if self.current_type is None else hash(self.current_type) |
| 70 | + |
| 71 | +ListClass = List |
| 72 | +List = List() |
| 73 | + |
| 74 | +dict_cache = {} |
| 75 | + |
| 76 | +class Dict(dict): |
| 77 | + |
| 78 | + current_types = [None, None] |
| 79 | + |
| 80 | + def __getitem__(self, types): |
| 81 | + if not isinstance(types, tuple): |
| 82 | + types = [types, None] |
| 83 | + elif len(types) != 2: raise Exception('Expected 2 types for Dict (key and value)') |
| 84 | + current_types = [cast_primitive(type) for type in types] |
| 85 | + types_stringified = str(current_types) |
| 86 | + if types_stringified in dict_cache: return dict_cache[types_stringified] |
| 87 | + new = DictClass() |
| 88 | + new.current_types = current_types |
| 89 | + dict_cache[types_stringified] = new |
| 90 | + return new |
| 91 | + |
| 92 | + def __hash__(self): |
| 93 | + return dict_hash if self.current_types == [None, None] else hash(str(self.current_types)) |
| 94 | + |
| 95 | +DictClass = Dict |
| 96 | +Dict = Dict() |
| 97 | + |
| 98 | +original_primitives = {List: list, Dict: dict} |
| 99 | + |
| 100 | +list = List |
| 101 | +dict = Dict |
| 102 | + |
| 103 | +String = str |
| 104 | +Integer = int |
| 105 | +Float = float |
| 106 | +Boolean = bool |
| 107 | +Bool = bool |
| 108 | + |
| 109 | +str = String |
| 110 | +int = Integer |
| 111 | +float = Float |
| 112 | +bool = Bool |
| 113 | + |
| 114 | +PlainObject = Object() |
0 commit comments