Conditions | 17 |
Total Lines | 100 |
Code Lines | 71 |
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 provisions.base.provision() 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 |
||
156 | def provision( |
||
157 | list_provisions=False, |
||
158 | overwrite=False, |
||
159 | clear_provisions=False, |
||
160 | package=None, |
||
161 | installed=None, |
||
162 | ): |
||
163 | from isomer.provisions import build_provision_store |
||
164 | from isomer.database import objectmodels |
||
165 | |||
166 | provision_store = build_provision_store() |
||
167 | |||
168 | if installed is None: |
||
169 | installed = [] |
||
170 | |||
171 | def sort_dependencies(items): |
||
172 | """Topologically sort the dependency tree""" |
||
173 | |||
174 | g = DiGraph() |
||
175 | log("Sorting dependencies") |
||
176 | |||
177 | for key, item in items: |
||
178 | log("key: ", key, "item:", item, pretty=True, lvl=debug) |
||
179 | dependencies = item.get("dependencies", []) |
||
180 | if isinstance(dependencies, str): |
||
181 | dependencies = [dependencies] |
||
182 | |||
183 | if key not in g: |
||
184 | g.add_node(key) |
||
185 | |||
186 | for link in dependencies: |
||
187 | g.add_edge(key, link) |
||
188 | |||
189 | if not is_directed_acyclic_graph(g): |
||
190 | log("Cycles in provisioning dependency graph detected!", lvl=error) |
||
191 | log("Involved provisions:", list(simple_cycles(g)), lvl=error) |
||
192 | |||
193 | topology = list(topological_sort(g)) |
||
194 | topology.reverse() |
||
195 | topology = list(set(topology).difference(installed)) |
||
196 | |||
197 | # log(topology, pretty=True) |
||
198 | |||
199 | return topology |
||
200 | |||
201 | sorted_provisions = sort_dependencies(provision_store.items()) |
||
202 | |||
203 | # These need to be installed first in that order: |
||
204 | if "system" in sorted_provisions: |
||
205 | sorted_provisions.remove("system") |
||
206 | if "user" in sorted_provisions: |
||
207 | sorted_provisions.remove("user") |
||
208 | if "system" not in installed: |
||
209 | sorted_provisions.insert(0, "system") |
||
210 | if "user" not in installed: |
||
211 | sorted_provisions.insert(0, "user") |
||
212 | |||
213 | if list_provisions: |
||
214 | log(sorted_provisions, pretty=True) |
||
215 | exit() |
||
216 | |||
217 | def provision_item(provision_name): |
||
218 | """Provision a single provisioning element""" |
||
219 | |||
220 | item = provision_store[provision_name] |
||
221 | |||
222 | method = item.get("method", provisionList) |
||
223 | model = item.get("model") |
||
224 | data = item.get("data") |
||
225 | |||
226 | method(data, model, overwrite=overwrite, clear=clear_provisions) |
||
227 | |||
228 | confirm_provision(provision_name) |
||
229 | |||
230 | def confirm_provision(provision_name): |
||
231 | if provision_name == "user": |
||
232 | log("Not confirming system user provision") |
||
233 | return |
||
234 | systemconfig = objectmodels["systemconfig"].find_one({"active": True}) |
||
235 | if provision_name not in systemconfig.provisions["packages"]: |
||
236 | systemconfig.provisions["packages"].append(provision_name) |
||
237 | systemconfig.save() |
||
238 | |||
239 | if package is not None: |
||
240 | if package in provision_store: |
||
241 | log("Provisioning ", package, pretty=True) |
||
242 | provision_item(package) |
||
243 | else: |
||
244 | log( |
||
245 | "Unknown package: ", |
||
246 | package, |
||
247 | "\nValid provisionable packages are", |
||
248 | list(provision_store.keys()), |
||
249 | lvl=error, |
||
250 | emitter="MANAGE", |
||
251 | ) |
||
252 | else: |
||
253 | for name in sorted_provisions: |
||
254 | log("Provisioning", name, pretty=True) |
||
255 | provision_item(name) |
||
256 |