|
| 1 | +import typing |
| 2 | +import inspect |
| 3 | +import json |
| 4 | +import enum |
| 5 | + |
| 6 | + |
| 7 | +# https://github.com/pyrogram/pyrogram/blob/v1.1.0/pyrogram/types/object.py#L26 |
| 8 | +class MetaClass(type, metaclass=type("", (type,), {"__str__": lambda _: "~hi"})): |
| 9 | + |
| 10 | + def __str__(self): |
| 11 | + return f"<class 'kai.{self.__name__}'>" |
| 12 | + |
| 13 | + |
| 14 | +# https://github.com/pyrogram/pyrogram/blob/v1.1.0/pyrogram/types/object.py#L31 |
| 15 | +class BaseClass(metaclass = MetaClass): |
| 16 | + |
| 17 | + def __str__(self): |
| 18 | + return json.dumps( |
| 19 | + self, |
| 20 | + indent = 4, |
| 21 | + default = self._serializer_callback, |
| 22 | + ensure_ascii = False |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +class Dict(BaseClass): |
| 27 | + |
| 28 | + def __iter__(self): |
| 29 | + iterables = {} |
| 30 | + |
| 31 | + code = self.__init__.__code__ |
| 32 | + arguments = inspect.getargs(code).args |
| 33 | + |
| 34 | + arguments.remove("self") |
| 35 | + |
| 36 | + for argument in arguments: |
| 37 | + attribute = getattr(self, argument) |
| 38 | + |
| 39 | + if isinstance(attribute, List): |
| 40 | + iterables.update( |
| 41 | + { |
| 42 | + argument: list(attribute) |
| 43 | + } |
| 44 | + ) |
| 45 | + elif isinstance(attribute, Dict): |
| 46 | + iterables.update( |
| 47 | + { |
| 48 | + argument: dict(attribute) |
| 49 | + } |
| 50 | + ) |
| 51 | + elif isinstance(attribute, enum.Enum): |
| 52 | + iterables.update( |
| 53 | + { |
| 54 | + argument: str(attribute) |
| 55 | + } |
| 56 | + ) |
| 57 | + else: |
| 58 | + iterables.update( |
| 59 | + { |
| 60 | + argument: attribute |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + #print(iterables) |
| 65 | + |
| 66 | + yield from iterables.items() |
| 67 | + |
| 68 | + def __getitem__(self, item): |
| 69 | + return getattr(self, item) |
| 70 | + |
| 71 | + def __setitem__(self, key, value): |
| 72 | + setattr(self, key, value) |
| 73 | + |
| 74 | + def __delitem__(self, item): |
| 75 | + delattr(self, item) |
| 76 | + |
| 77 | + def __repr__(self): |
| 78 | + code = self.__init__.__code__ |
| 79 | + arguments = inspect.getargs(code).args |
| 80 | + |
| 81 | + arguments.remove("self") |
| 82 | + |
| 83 | + data = [] |
| 84 | + |
| 85 | + for argument in arguments: |
| 86 | + attribute = getattr(self, argument) |
| 87 | + |
| 88 | + if isinstance(attribute, List): |
| 89 | + data.append(f"{argument} = {attribute.__class__.__name__}({repr(attribute.base_list)})") |
| 90 | + else: |
| 91 | + data.append(f"{argument} = {repr(attribute)}") |
| 92 | + |
| 93 | + class_name = self.__class__.__name__ |
| 94 | + representation = ", ".join(data) |
| 95 | + |
| 96 | + return f"kai.{class_name}({representation})" |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def _serializer_callback(obj): |
| 100 | + serialized_object = {} |
| 101 | + |
| 102 | + code = obj.__init__.__code__ |
| 103 | + arguments = inspect.getargs(code).args |
| 104 | + |
| 105 | + arguments.remove("self") |
| 106 | + |
| 107 | + for argument in arguments: |
| 108 | + attribute = getattr(obj, argument) |
| 109 | + |
| 110 | + if isinstance(attribute, List): |
| 111 | + serialized_object.update( |
| 112 | + { |
| 113 | + argument: list(attribute) |
| 114 | + } |
| 115 | + ) |
| 116 | + elif isinstance(attribute, enum.Enum): |
| 117 | + serialized_object.update( |
| 118 | + { |
| 119 | + argument: str(attribute) |
| 120 | + } |
| 121 | + ) |
| 122 | + else: |
| 123 | + serialized_object.update( |
| 124 | + { |
| 125 | + argument: attribute |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + return serialized_object |
| 130 | + |
| 131 | +class List(BaseClass): |
| 132 | + |
| 133 | + def __init__(self, base_list = None): |
| 134 | + self.base_list = [] if base_list is None else base_list |
| 135 | + |
| 136 | + def __iter__(self): |
| 137 | + iterable = [] |
| 138 | + |
| 139 | + for item in self.base_list: |
| 140 | + if isinstance(item, Dict): |
| 141 | + iterable.append(dict(item)) |
| 142 | + elif isinstance(item, (str, int, float, bool, type(None))): |
| 143 | + iterable.append(item) |
| 144 | + else: |
| 145 | + raise TypeError |
| 146 | + |
| 147 | + return iter(iterable) |
| 148 | + |
| 149 | + def __getitem__(self, item): |
| 150 | + return self.base_list[item] |
| 151 | + |
| 152 | + def __len__(self): |
| 153 | + return len(self.base_list) |
| 154 | + |
| 155 | + def append(self, item): |
| 156 | + self.base_list.append(item) |
| 157 | + |
| 158 | + def iter(self): |
| 159 | + return iter(self.base_list) |
| 160 | + |
| 161 | + def list(self): |
| 162 | + return list(self.base_list) |
| 163 | + |
| 164 | + def __repr__(self): |
| 165 | + class_name = self.__class__.__name__ |
| 166 | + representation = repr(self.base_list) |
| 167 | + |
| 168 | + return f"kai.{class_name}({representation})" |
| 169 | + |
| 170 | + @staticmethod |
| 171 | + def _serializer_callback(obj): |
| 172 | + if isinstance(obj, List): |
| 173 | + return getattr(obj, "base_list") |
| 174 | + |
| 175 | + if isinstance(obj, Dict): |
| 176 | + return dict(obj) |
| 177 | + |
| 178 | + raise TypeError |
0 commit comments