| Conditions | 26 |
| Total Lines | 110 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like isomer.schemastore.build_schemastore_new() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python |
||
| 44 | |||
| 45 | |||
| 46 | def build_schemastore_new(): |
||
| 47 | available = {} |
||
| 48 | |||
| 49 | for schema_entrypoint in iter_entry_points(group="isomer.schemata", name=None): |
||
| 50 | try: |
||
| 51 | schemata_log("Schemata found: ", schema_entrypoint.name, lvl=verbose) |
||
| 52 | schema = schema_entrypoint.load() |
||
| 53 | available[schema_entrypoint.name] = schema |
||
| 54 | except (ImportError, DistributionNotFound) as e: |
||
| 55 | schemata_log( |
||
| 56 | "Problematic schema: ", schema_entrypoint.name, exc=True, lvl=warn |
||
| 57 | ) |
||
| 58 | |||
| 59 | def schema_insert(dictionary, insert_path, insert_object): |
||
| 60 | insert_path = insert_path.split("/") |
||
| 61 | |||
| 62 | place = dictionary |
||
| 63 | |||
| 64 | for element in insert_path: |
||
| 65 | if element != "": |
||
| 66 | place = place[element] |
||
| 67 | |||
| 68 | place.update(insert_object) |
||
| 69 | |||
| 70 | return dictionary |
||
| 71 | |||
| 72 | def form_insert(insert_form, insert_index, insert_path, insert_object): |
||
| 73 | insert_path = insert_path.split("/") |
||
| 74 | place = None |
||
| 75 | if isinstance(insert_index, str): |
||
| 76 | for widget in insert_form: |
||
| 77 | if isinstance(widget, dict) and widget.get("id", None) is not None: |
||
| 78 | place = widget |
||
| 79 | else: |
||
| 80 | place = insert_form[insert_index] |
||
| 81 | |||
| 82 | if place is None: |
||
| 83 | schemata_log("No place to insert into form found:", insert_path, insert_form, insert_object) |
||
| 84 | return |
||
| 85 | |||
| 86 | for element in insert_path: |
||
| 87 | schemata_log(element, place, lvl=verbose) |
||
| 88 | try: |
||
| 89 | element = int(element) |
||
| 90 | except ValueError: |
||
| 91 | pass |
||
| 92 | if element != "": |
||
| 93 | place = place[element] |
||
| 94 | |||
| 95 | if isinstance(place, dict): |
||
| 96 | place.update(insert_object) |
||
| 97 | else: |
||
| 98 | place.append(insert_object) |
||
| 99 | |||
| 100 | return insert_form |
||
| 101 | |||
| 102 | def _get_field_restrictions(item): |
||
| 103 | result = {} |
||
| 104 | |||
| 105 | for key, thing in item['schema']['properties'].items(): |
||
| 106 | if 'roles' in thing: |
||
| 107 | # schemata_log(thing) |
||
| 108 | result[key] = thing['roles'] |
||
| 109 | |||
| 110 | return result |
||
| 111 | |||
| 112 | for key, item in available.items(): |
||
| 113 | restrictions[key] = _get_field_restrictions(item) |
||
| 114 | schemata_log("Schema", key, "restrictions:", restrictions[key], lvl=debug) |
||
|
|
|||
| 115 | |||
| 116 | extends = item.get("extends", None) |
||
| 117 | if extends is not None: |
||
| 118 | schemata_log(key, "extends:", extends, pretty=True, lvl=verbose) |
||
| 119 | |||
| 120 | for model, extension_group in extends.items(): |
||
| 121 | schema_extensions = extension_group.get("schema", None) |
||
| 122 | form_extensions = extension_group.get("form", None) |
||
| 123 | schema = available[model].get("schema", None) |
||
| 124 | form = available[model].get("form", None) |
||
| 125 | |||
| 126 | original_schema = deepcopy(schema) |
||
| 127 | |||
| 128 | if schema_extensions is not None: |
||
| 129 | schemata_log("Extending schema", model, "from", key, lvl=debug) |
||
| 130 | for path, extensions in schema_extensions.items(): |
||
| 131 | schemata_log( |
||
| 132 | "Item:", path, "Extensions:", extensions, lvl=verbose |
||
| 133 | ) |
||
| 134 | for obj in extensions: |
||
| 135 | available[model]["schema"] = schema_insert( |
||
| 136 | schema, path, obj |
||
| 137 | ) |
||
| 138 | schemata_log("Path:", path, "obj:", obj, lvl=verbose) |
||
| 139 | |||
| 140 | if form_extensions is not None: |
||
| 141 | schemata_log("Extending form of", model, "with", key, lvl=verbose) |
||
| 142 | for index, extensions in form_extensions.items(): |
||
| 143 | schemata_log( |
||
| 144 | "Item:", index, "Extensions:", extensions, lvl=verbose |
||
| 145 | ) |
||
| 146 | for path, obj in extensions.items(): |
||
| 147 | if not isinstance(obj, list): |
||
| 148 | obj = [obj] |
||
| 149 | for thing in obj: |
||
| 150 | available[model]["form"] = form_insert( |
||
| 151 | form, index, path, thing |
||
| 152 | ) |
||
| 153 | schemata_log("Path:", path, "obj:", thing, lvl=verbose) |
||
| 154 | |||
| 237 |