389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563 | def get_class_structure(self) -> tuple[dict, dict]:
"""
Retrieve the structure of the class.
This method recursively traverses the class structure to extract the
class attributes, types, and other related information. The returned
structure includes whether the attribute allows multiple values,
is required or not, and other extra information.
Returns: tuple[dict, dict] : A tuple containing two dictionaries
(Structure / Info).
The first dictionary represents the class structure with each
attribute as a key, and its value is either a nested dictionary (for
complex types) or an empty dictionary (for basic types). The second
dictionary contains additional information about each attribute,
including its name, title, description, type, and whether it allows
multiple values and is required.
Notes
-----
This method makes use of several nested functions:
* `loop_through_classes_recursively` : Traverses the class
attributes and constructs the class structure.
* `is_basic_type` : Checks if a given type is basic (i.e., not
defined in the current file).
* `assemble_line` : Assembles a string representation of the current
attribute path.
* `gather_info` : Gathers and stores additional information about an
attribute.
These helper functions are used to organize the logic of the
`get_class_structure` method and make it easier to understand.
"""
def loop_through_classes_recursely(
cls: ModelMetaclass, dict_o: dict
) -> None:
# get (type_class , attribute_name)
nonlocal stack, prepend_string, stack_required, info
types_names_multiple: list[
tuple[ModelMetaclass, str, bool, bool, dict]
] = []
for field in cls.__fields__:
model_field: ModelField = cls.__fields__[field]
name: str = model_field.name
required: bool = model_field.required
type_: ModelMetaclass = self.get_type_of(model_field)
multiple = self.get_multiple_of(model_field)
extra_info = self.get_extra_info(model_field)
types_names_multiple.append(
(type_, name, multiple, required, extra_info)
)
for (
cls_type,
cls_name,
multiple_allowed,
required,
extra_info,
) in types_names_multiple:
if (cls_type, cls_name) in stack:
continue # recursion blocker
stack.append((cls_type, cls_name))
stack_required.append(required)
if isinstance(dict_o, list):
dict_o = dict_o[0]
if dict_o.get(cls_name, None) is None:
if multiple_allowed is False:
dict_o[cls_name] = dict()
else:
dict_o[cls_name] = [{}]
def is_basic_type(cls_type_: ModelMetaclass) -> bool:
# if type class not in this file defined, then is basic type
origin_path = inspect.getfile(self.root)
origin_file_name = Path(origin_path).stem
# basic_types = ['int','float','str','Email']
# for basic_type in basic_types:
# if basic_type in str(cls_type_):
# return True
# return False
#
type_ = cls_type_ # for debugging
val = origin_file_name not in str(cls_type_)
return val
def assemble_line() -> str:
required_string = ""
prepend_string_str = "".join(prepend_string)
stack_str = ".".join([val[1] for val in stack])
return prepend_string_str + required_string + stack_str
def gather_info(
cls_type_: ModelMetaclass, extra_info_: dict, info_: dict
) -> None:
type__ = (
str(cls_type_)
.removeprefix("<class '")
.removesuffix("'>")
)
if "EmailStr" in type__:
type__ = "email"
controlled_vocabulary = None
name_ = ".".join([val[1] for val in stack])
if type__.startswith("<enum"):
# Get the first member of the enum
first_member = next(iter(cls_type_))
# Get the underlying data type of the enum value
value_type = type(first_member.value)
type__ = (
str(value_type)
.removeprefix("<class '")
.removesuffix("'>")
)
controlled_vocabulary = [
member.value for member in cls_type_
]
# extract 'read' type from complete qualified name
# (e.g. 'dataset.metadata.provenance' -> 'provenance')
type__ = type__.split(".")[-1]
line_info = {
"name": name_,
"title": extra_info_.get("title"),
"description": extra_info_.get("description"),
"allow_multiples": multiple_allowed,
"required": required,
"type": type__,
}
if controlled_vocabulary:
line_info[
"controlledVocabulary"
] = controlled_vocabulary
info_[name_] = line_info
gather_info(cls_type, extra_info, info)
if is_basic_type(cls_type):
line_ = assemble_line()
# print(line_)
# f.write(line_ + '\n')
# name, title, description, allowmultiples, required
else:
# update forward references to allow "wrong" order of
# classes in metadate definition file
# cls_type.update_forward_refs()
loop_through_classes_recursely(cls_type, dict_o[cls_name])
stack_required.pop(-1)
stack.pop(-1)
info = {}
stack: list[tuple[ModelMetaclass, str]] = []
stack_required: list[bool] = []
prepend_string: list[str] = []
dict_output: dict = {}
loop_through_classes_recursely(self.root, dict_output)
return dict_output, info
|