Conditions | 12 |
Total Lines | 101 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Tests | 2 |
CRAP Score | 138.7263 |
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 tabpy.tabpy_server.handlers.management_handler.ManagementHandler._add_or_update_endpoint() 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 | 1 | import logging |
|
45 | 1 | @gen.coroutine |
|
46 | 1 | def _add_or_update_endpoint(self, action, name, version, request_data): |
|
47 | """ |
||
48 | Add or update an endpoint |
||
49 | """ |
||
50 | self.logger.log(logging.DEBUG, f"Adding/updating model {name}...") |
||
51 | |||
52 | if not isinstance(name, str): |
||
53 | msg = "Endpoint name must be a string" |
||
54 | self.logger.log(logging.CRITICAL, msg) |
||
55 | raise TypeError(msg) |
||
56 | |||
57 | name_checker = _compile(r"^[a-zA-Z0-9-_\s]+$") |
||
58 | if not name_checker.match(name): |
||
59 | raise gen.Return( |
||
60 | "endpoint name can only contain: a-z, A-Z, 0-9," |
||
61 | " underscore, hyphens and spaces." |
||
62 | ) |
||
63 | |||
64 | if self.settings.get("add_or_updating_endpoint"): |
||
65 | msg = ( |
||
66 | "Another endpoint update is already in progress" |
||
67 | ", please wait a while and try again" |
||
68 | ) |
||
69 | self.logger.log(logging.CRITICAL, msg) |
||
70 | raise RuntimeError(msg) |
||
71 | |||
72 | self.settings["add_or_updating_endpoint"] = random_uuid() |
||
73 | try: |
||
74 | docstring = None |
||
75 | if "docstring" in request_data: |
||
76 | docstring = str( |
||
77 | bytes(request_data["docstring"], "utf-8").decode("unicode_escape") |
||
78 | ) |
||
79 | |||
80 | description = request_data.get("description", None) |
||
81 | endpoint_type = request_data.get("type", None) |
||
82 | methods = request_data.get("methods", []) |
||
83 | dependencies = request_data.get("dependencies", None) |
||
84 | target = request_data.get("target", None) |
||
85 | schema = request_data.get("schema", None) |
||
86 | src_path = request_data.get("src_path", None) |
||
87 | is_public = request_data.get("is_public", None) |
||
88 | target_path = get_query_object_path( |
||
89 | self.settings[SettingsParameters.StateFilePath], name, version |
||
90 | ) |
||
91 | |||
92 | path_checker = _compile(r"^[\\\:a-zA-Z0-9-_~\s/\.\(\)]+$") |
||
93 | # copy from staging |
||
94 | if src_path: |
||
95 | if not isinstance(src_path, str): |
||
96 | raise gen.Return("src_path must be a string.") |
||
97 | if not path_checker.match(src_path): |
||
98 | raise gen.Return(f"Invalid source path for endpoint {name}") |
||
99 | |||
100 | yield self._copy_po_future(src_path, target_path) |
||
101 | elif endpoint_type != "alias": |
||
102 | raise gen.Return("src_path is required to add/update an endpoint.") |
||
103 | else: |
||
104 | # alias special logic: |
||
105 | if not target: |
||
106 | raise gen.Return("Target is required for alias endpoint.") |
||
107 | dependencies = [target] |
||
108 | |||
109 | # update local config |
||
110 | try: |
||
111 | if action == "add": |
||
112 | self.tabpy_state.add_endpoint( |
||
113 | name=name, |
||
114 | description=description, |
||
115 | docstring=docstring, |
||
116 | endpoint_type=endpoint_type, |
||
117 | methods=methods, |
||
118 | dependencies=dependencies, |
||
119 | target=target, |
||
120 | schema=schema, |
||
121 | is_public=is_public, |
||
122 | ) |
||
123 | else: |
||
124 | self.tabpy_state.update_endpoint( |
||
125 | name=name, |
||
126 | description=description, |
||
127 | docstring=docstring, |
||
128 | endpoint_type=endpoint_type, |
||
129 | methods=methods, |
||
130 | dependencies=dependencies, |
||
131 | target=target, |
||
132 | schema=schema, |
||
133 | version=version, |
||
134 | is_public=is_public, |
||
135 | ) |
||
136 | |||
137 | except Exception as e: |
||
138 | raise gen.Return(f"Error when changing TabPy state: {e}") |
||
139 | |||
140 | on_state_change( |
||
141 | self.settings, self.tabpy_state, self.python_service, self.logger |
||
142 | ) |
||
143 | |||
144 | finally: |
||
145 | self.settings["add_or_updating_endpoint"] = None |
||
146 | |||
154 |