libentry 1.24.3__py3-none-any.whl → 1.24.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
libentry/argparse.py CHANGED
@@ -126,28 +126,40 @@ class ArgumentParser(argparse.ArgumentParser):
126
126
  self.schema_dict[name] = (schema, default)
127
127
  self._add_schema(name, schema)
128
128
 
129
- def _add_schema(self, prefix: str, schema: Type[BaseModel]):
130
- for name, info in schema.model_fields.items():
129
+ def _add_schema(self, prefix: str, schema: Type[BaseModel], instance: Optional[BaseModel] = None):
130
+ model_fields = schema.model_fields
131
+ assert isinstance(model_fields, dict)
132
+ for name, info in model_fields.items():
131
133
  anno = info.annotation
132
134
  nested = False
133
135
 
134
136
  if isinstance(anno, type) and issubclass(anno, BaseModel):
135
137
  nested = True
136
138
 
137
- if get_origin(anno) is Union and any(issubclass(t, BaseModel) for t in get_args(anno)):
138
- for t in get_args(anno):
139
- if issubclass(t, BaseModel):
139
+ if get_origin(anno) is Union:
140
+ # Here we just choose the first BaseModel.
141
+ # So you should avoid define a Union contains multiple BaseModel types.
142
+ for sub_anno in get_args(anno):
143
+ if isinstance(sub_anno, type) and issubclass(sub_anno, BaseModel):
144
+ anno = sub_anno
140
145
  nested = True
141
- anno = t
142
146
  break
143
147
 
148
+ default_value = info.default
149
+ if isinstance(instance, schema):
150
+ default_value = getattr(instance, name, None)
151
+
144
152
  if nested:
145
- self._add_schema(f"{prefix}.{name}", anno)
153
+ self._add_schema(
154
+ f"{prefix}.{name}",
155
+ schema=anno,
156
+ instance=default_value
157
+ )
146
158
  else:
147
159
  self.add_argument(
148
160
  f"--{prefix}.{name}",
149
161
  type=literal_eval,
150
- default=DefaultValue(info.default),
162
+ default=DefaultValue(default_value),
151
163
  # required=info.is_required(),
152
164
  help=info.description
153
165
  )
@@ -228,83 +240,3 @@ def _list_field_names(obj):
228
240
  return [*obj.model_fields]
229
241
  else:
230
242
  return [obj.__dict__]
231
-
232
- # T = TypeVar('T')
233
- #
234
- #
235
- # class ArgumentParser(argparse.ArgumentParser):
236
- # PATTERN_ARG_NAME = re.compile(r"^--?[a-zA-Z][\w\-.]*$")
237
- # PATTERN_ARG_PREFIX = re.compile(r"^--?")
238
- # DATACLASS_OBJ_KEY = 'target'
239
- #
240
- # def __init__(self):
241
- # super().__init__()
242
- # self.obj_dict = {}
243
- #
244
- # def add_argument(self, *args, **kwargs):
245
- # if self.DATACLASS_OBJ_KEY in kwargs:
246
- # obj = kwargs[self.DATACLASS_OBJ_KEY]
247
- # for prefix in args:
248
- # self.obj_dict[prefix] = obj
249
- # return obj
250
- # elif len(args) >= 2 and is_dataclass(args[-1]) and all(isinstance(arg, str) for arg in args[:-1]):
251
- # obj = args[-1]
252
- # for prefix in args[:-1]:
253
- # self.obj_dict[prefix] = obj
254
- # return obj
255
- # else:
256
- # return super().add_argument(*args, **kwargs)
257
- #
258
- # def add_dataclass(self, prefix, d: Union[Type[T], T]) -> T:
259
- # assert is_dataclass(d)
260
- # if isinstance(d, type):
261
- # d = d()
262
- # self.obj_dict[prefix] = d
263
- # return d
264
- #
265
- # def parse_args(self, args=None, namespace=None, parse_unknown=False):
266
- # args, unknown_args = super().parse_known_args()
267
- #
268
- # d = {}
269
- # name = None
270
- # values = []
271
- # for arg in unknown_args:
272
- # if arg.startswith('-'):
273
- # if self.PATTERN_ARG_NAME.match(arg):
274
- # if name is not None:
275
- # d[name] = values[0] if len(values) == 1 else values
276
- # values = []
277
- # name = self.PATTERN_ARG_PREFIX.sub("", arg).replace('-', '_')
278
- # else:
279
- # value = literal_eval(arg)
280
- # if isinstance(value, str):
281
- # logger.warning(f'The value "{arg}" may be incorrect.')
282
- # if name is not None:
283
- # values.append(value)
284
- # else:
285
- # values.append(literal_eval(arg))
286
- # if name is not None:
287
- # d[name] = values[0] if len(values) == 1 else values
288
- #
289
- # for name, value in d.items():
290
- # sections = name.split('.')
291
- # if len(sections) == 1:
292
- # args.__dict__[name] = value
293
- # else:
294
- # prefix = sections[0]
295
- # members = sections[1:-1]
296
- # attr = sections[-1]
297
- # if prefix not in self.obj_dict:
298
- # raise RuntimeError(f'There is no {prefix} argument.')
299
- # obj = self.obj_dict[prefix]
300
- #
301
- # for member in members:
302
- # if not hasattr(obj, member):
303
- # raise RuntimeError(f'There is no {prefix} argument {name}.')
304
- # obj = getattr(obj, member)
305
- #
306
- # if not hasattr(obj, attr):
307
- # raise RuntimeError(f'There is no {prefix} argument {name}.')
308
- # setattr(obj, sections[-1], value)
309
- #
310
- # return args
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: libentry
3
- Version: 1.24.3
3
+ Version: 1.24.5
4
4
  Summary: Entries for experimental utilities.
5
5
  Home-page: https://github.com/XoriieInpottn/libentry
6
6
  Author: xi
@@ -1,6 +1,6 @@
1
1
  libentry/__init__.py,sha256=ko2YBIIx5H3dD0tedBkialzJGEDczFaP_PZmT1cIlak,148
2
2
  libentry/api.py,sha256=UkXdBv9oqQhaSESRReLWEPj8venBUoCBppIg-FAXqKA,24146
3
- libentry/argparse.py,sha256=NxzXV-jBN51ReZsNs5aeyOfzwYQ5A5nJ95rWoa-FYCs,10415
3
+ libentry/argparse.py,sha256=j_wv7rBE_9TFgxnR6zJtdUkugKP8vOwwIMkL-RaHRsw,7901
4
4
  libentry/dataclasses.py,sha256=AQV2PuxplJCwGZ5HKX72U-z-POUhTdy3XtpEK9KNIGQ,4541
5
5
  libentry/executor.py,sha256=cTV0WxJi0nU1TP-cOwmeodN8DD6L1691M2HIQsJtGrU,6582
6
6
  libentry/experiment.py,sha256=ejgAHDXWIe9x4haUzIFuz1WasLY0_aD1z_vyEVGjTu8,4922
@@ -22,10 +22,10 @@ libentry/service/list.py,sha256=ElHWhTgShGOhaxMUEwVbMXos0NQKjHsODboiQ-3AMwE,1397
22
22
  libentry/service/running.py,sha256=FrPJoJX6wYxcHIysoatAxhW3LajCCm0Gx6l7__6sULQ,5105
23
23
  libentry/service/start.py,sha256=mZT7b9rVULvzy9GTZwxWnciCHgv9dbGN2JbxM60OMn4,1270
24
24
  libentry/service/stop.py,sha256=wOpwZgrEJ7QirntfvibGq-XsTC6b3ELhzRW2zezh-0s,1187
25
- libentry-1.24.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
26
- libentry-1.24.3.dist-info/METADATA,sha256=_Jhwomm28Zsl3Jw1NHslnqvQe_eUW-dLe0j0SrGNHnI,1135
27
- libentry-1.24.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
28
- libentry-1.24.3.dist-info/entry_points.txt,sha256=1v_nLVDsjvVJp9SWhl4ef2zZrsLTBtFWgrYFgqvQBgc,61
29
- libentry-1.24.3.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
30
- libentry-1.24.3.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
31
- libentry-1.24.3.dist-info/RECORD,,
25
+ libentry-1.24.5.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
26
+ libentry-1.24.5.dist-info/METADATA,sha256=Up8a5KSfalbtpaRZs1s8fo9uNDbYU7GfdEacO49v2QM,1135
27
+ libentry-1.24.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
28
+ libentry-1.24.5.dist-info/entry_points.txt,sha256=1v_nLVDsjvVJp9SWhl4ef2zZrsLTBtFWgrYFgqvQBgc,61
29
+ libentry-1.24.5.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
30
+ libentry-1.24.5.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
31
+ libentry-1.24.5.dist-info/RECORD,,