Conditions | 15 |
Total Lines | 106 |
Code Lines | 63 |
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.provisionList() 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 |
||
48 | def provisionList( |
||
49 | items, database_name, overwrite=False, clear=False, skip_user_check=False |
||
50 | ): |
||
51 | """Provisions a list of items according to their schema |
||
52 | |||
53 | :param items: A list of provisionable items. |
||
54 | :param database_name: |
||
55 | :param overwrite: Causes existing items to be overwritten |
||
56 | :param clear: Clears the collection first (Danger!) |
||
57 | :param skip_user_check: Skips checking if a system user is existing already |
||
58 | (for user provisioning) |
||
59 | :return: |
||
60 | """ |
||
61 | |||
62 | log("Provisioning", items, database_name, lvl=debug) |
||
63 | |||
64 | def get_system_user(): |
||
65 | """Retrieves the node local system user""" |
||
66 | |||
67 | user = objectmodels["user"].find_one({"name": "System"}) |
||
68 | |||
69 | try: |
||
70 | log("System user uuid: ", user.uuid, lvl=verbose) |
||
71 | return user.uuid |
||
72 | except AttributeError as system_user_error: |
||
73 | log("No system user found:", system_user_error, lvl=warn) |
||
74 | log( |
||
75 | "Please install the user provision to setup a system user or " |
||
76 | "check your database configuration", |
||
77 | lvl=error, |
||
78 | ) |
||
79 | return False |
||
80 | |||
81 | # TODO: Do not check this on specific objects but on the model (i.e. once) |
||
82 | def needs_owner(obj): |
||
83 | """Determines whether a basic object has an ownership field""" |
||
84 | for privilege in obj._fields.get("perms", None): |
||
85 | if "owner" in obj._fields["perms"][privilege]: |
||
86 | return True |
||
87 | |||
88 | return False |
||
89 | |||
90 | import pymongo |
||
91 | from isomer.database import objectmodels, dbhost, dbport, dbname |
||
92 | |||
93 | database_object = objectmodels[database_name] |
||
94 | |||
95 | log(dbhost, dbname) |
||
96 | # TODO: Fix this to make use of the dbhost |
||
97 | |||
98 | client = pymongo.MongoClient(dbhost, dbport) |
||
99 | db = client[dbname] |
||
100 | |||
101 | if not skip_user_check: |
||
102 | system_user = get_system_user() |
||
103 | |||
104 | if not system_user: |
||
105 | return |
||
106 | else: |
||
107 | # TODO: Evaluate what to do instead of using a hardcoded UUID |
||
108 | # This is usually only here for provisioning the system user |
||
109 | # One way to avoid this, is to create (instead of provision) |
||
110 | # this one upon system installation. |
||
111 | system_user = "0ba87daa-d315-462e-9f2e-6091d768fd36" |
||
112 | |||
113 | col_name = database_object.collection_name() |
||
114 | |||
115 | if clear is True: |
||
116 | log("Clearing collection for", col_name, lvl=warn) |
||
117 | db.drop_collection(col_name) |
||
118 | counter = 0 |
||
119 | |||
120 | for no, item in enumerate(items): |
||
121 | new_object = None |
||
122 | item_uuid = item["uuid"] |
||
123 | log("Validating object (%i/%i):" % (no + 1, len(items)), item_uuid, lvl=debug) |
||
124 | |||
125 | if database_object.count({"uuid": item_uuid}) > 0: |
||
126 | log("Object already present", lvl=warn) |
||
127 | if overwrite is False: |
||
128 | log("Not updating item", item, lvl=warn) |
||
129 | else: |
||
130 | log("Overwriting item: ", item_uuid, lvl=warn) |
||
131 | new_object = database_object.find_one({"uuid": item_uuid}) |
||
132 | new_object._fields.update(item) |
||
133 | else: |
||
134 | new_object = database_object(item) |
||
135 | |||
136 | if new_object is not None: |
||
137 | try: |
||
138 | if needs_owner(new_object): |
||
139 | if not hasattr(new_object, "owner"): |
||
140 | log("Adding system owner to object.", lvl=verbose) |
||
141 | new_object.owner = system_user |
||
142 | except Exception as e: |
||
143 | log("Error during ownership test:", e, type(e), exc=True, lvl=error) |
||
144 | try: |
||
145 | new_object.validate() |
||
146 | new_object.save() |
||
147 | counter += 1 |
||
148 | except ValidationError as e: |
||
149 | raise ValidationError( |
||
150 | "Could not provision object: " + str(item_uuid), e |
||
151 | ) |
||
152 | |||
153 | log("Provisioned %i out of %i items successfully." % (counter, len(items))) |
||
154 | |||
256 |