|
| 1 | +from json import load as jload |
| 2 | +from os.path import join, split |
| 3 | + |
| 4 | + |
| 5 | +class ClassInfo: |
| 6 | + def __init__(self): |
| 7 | + self.class_dict = self.__init_dict__() |
| 8 | + |
| 9 | + def __init_dict__(self): |
| 10 | + """Method to get a dict on the pyleecan classes, i.e. class name, description, |
| 11 | + properties, methods, etc. |
| 12 | +
|
| 13 | + Returns |
| 14 | + ------- |
| 15 | + class_dict : dict |
| 16 | + dict of class information |
| 17 | + """ |
| 18 | + # load the class dict |
| 19 | + path = split(__file__)[0] |
| 20 | + with open(join(path, "Class_Dict.json")) as fp: |
| 21 | + class_dict = jload(fp) |
| 22 | + |
| 23 | + # create inheritance information |
| 24 | + for cls_name in class_dict.keys(): |
| 25 | + mother_name = class_dict[cls_name]["mother"] |
| 26 | + inherit = [] |
| 27 | + while mother_name: |
| 28 | + inherit.append(mother_name) |
| 29 | + mother_name = class_dict[mother_name]["mother"] |
| 30 | + |
| 31 | + class_dict[cls_name]["inherit"] = inherit |
| 32 | + |
| 33 | + # from pprint import pprint |
| 34 | + # pprint(sorted([ClassInfo().get_prop_types()])) |
| 35 | + |
| 36 | + # complete properties and methods on each class |
| 37 | + for cls_dict in class_dict.values(): |
| 38 | + prop_names = [prop["name"] for prop in cls_dict["properties"]] |
| 39 | + for mother in cls_dict["inherit"]: |
| 40 | + mother_props = class_dict[mother]["properties"] |
| 41 | + for mother_prop in mother_props: |
| 42 | + if not mother_prop["name"] in prop_names: |
| 43 | + cls_dict["properties"].append(mother_prop) |
| 44 | + |
| 45 | + # update property names |
| 46 | + prop_names = [prop["name"] for prop in cls_dict["properties"]] |
| 47 | + |
| 48 | + # convert properties to dict |
| 49 | + cls_dict["prop_dict"] = dict() |
| 50 | + for prop in cls_dict["properties"]: |
| 51 | + cls_dict["prop_dict"][prop["name"]] = prop |
| 52 | + |
| 53 | + return class_dict |
| 54 | + |
| 55 | + def get_dict(self): |
| 56 | + return self.class_dict |
| 57 | + |
| 58 | + def get_prop_types(self): |
| 59 | + """Get a set of all defined property types of all classes.""" |
| 60 | + type_set = set() |
| 61 | + |
| 62 | + for cls in self.class_dict.values(): |
| 63 | + for prop in cls["prop_dict"].values(): |
| 64 | + type_set.add(prop["type"]) |
| 65 | + |
| 66 | + return type_set |
| 67 | + |
| 68 | + def get_base_classes(self): |
| 69 | + """Get the base classes, i.e. classes that have no mother class.""" |
| 70 | + bases = set() |
| 71 | + for key, item in self.class_dict.items(): |
| 72 | + if not item["mother"]: |
| 73 | + bases.add(key) |
| 74 | + |
| 75 | + bases = sorted(list(bases)) |
| 76 | + |
| 77 | + return bases |
| 78 | + |
| 79 | + def get_mothers(self, cls_name, stop=""): |
| 80 | + """Get a ordered list of the mothers of a class.""" |
| 81 | + mothers = [] |
| 82 | + if stop not in self.class_dict: |
| 83 | + stop = "" |
| 84 | + if cls_name in self.class_dict: |
| 85 | + mother = self.class_dict[cls_name]["mother"] |
| 86 | + while mother and cls_name != stop: |
| 87 | + mothers.append(mother) |
| 88 | + cls_name = mother |
| 89 | + mother = self.class_dict[mother]["mother"] |
| 90 | + |
| 91 | + return mothers |
0 commit comments