Conditions | 21 |
Total Lines | 165 |
Code Lines | 119 |
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 backend.tracim_backend.lib.webdav.dav_provider.Provider.getResourceInst() 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 | # coding: utf8 |
||
62 | def getResourceInst(self, path: str, environ: dict): |
||
63 | """ |
||
64 | Called by wsgidav whenever a request is called to get the _DAVResource corresponding to the path |
||
65 | """ |
||
66 | user = environ['tracim_user'] |
||
67 | session = environ['tracim_dbsession'] |
||
68 | if not self.exists(path, environ): |
||
69 | return None |
||
70 | path = normpath(path) |
||
71 | root_path = environ['http_authenticator.realm'] |
||
72 | |||
73 | # If the requested path is the root, then we return a RootResource resource |
||
74 | if path == root_path: |
||
75 | return resources.RootResource( |
||
76 | path=path, |
||
77 | environ=environ, |
||
78 | user=user, |
||
79 | session=session |
||
80 | ) |
||
81 | |||
82 | workspace_api = WorkspaceApi( |
||
83 | current_user=user, |
||
84 | session=session, |
||
85 | config=self.app_config, |
||
86 | ) |
||
87 | workspace = self.get_workspace_from_path(path, workspace_api) |
||
88 | |||
89 | # If the request path is in the form root/name, then we return a WorkspaceResource resource |
||
90 | parent_path = dirname(path) |
||
91 | if parent_path == root_path: |
||
92 | if not workspace: |
||
93 | return None |
||
94 | return resources.WorkspaceResource( |
||
95 | path=path, |
||
96 | environ=environ, |
||
97 | workspace=workspace, |
||
98 | user=user, |
||
99 | session=session, |
||
100 | ) |
||
101 | |||
102 | # And now we'll work on the path to establish which type or resource is requested |
||
103 | |||
104 | content_api = ContentApi( |
||
105 | current_user=user, |
||
106 | session=session, |
||
107 | config=self.app_config, |
||
108 | show_archived=False, # self._show_archive, |
||
109 | show_deleted=False, # self._show_delete |
||
110 | ) |
||
111 | |||
112 | content = self.get_content_from_path( |
||
113 | path=path, |
||
114 | content_api=content_api, |
||
115 | workspace=workspace |
||
116 | ) |
||
117 | |||
118 | |||
119 | # Easy cases : path either end with /.deleted, /.archived or /.history, then we return corresponding resources |
||
120 | if path.endswith(SpecialFolderExtension.Archived) and self._show_archive: |
||
121 | return resources.ArchivedFolderResource( |
||
122 | path=path, |
||
123 | environ=environ, |
||
124 | workspace=workspace, |
||
125 | user=user, |
||
126 | content=content, |
||
127 | session=session, |
||
128 | ) |
||
129 | |||
130 | if path.endswith(SpecialFolderExtension.Deleted) and self._show_delete: |
||
131 | return resources.DeletedFolderResource( |
||
132 | path=path, |
||
133 | environ=environ, |
||
134 | workspace=workspace, |
||
135 | user=user, |
||
136 | content=content, |
||
137 | session=session, |
||
138 | ) |
||
139 | |||
140 | if path.endswith(SpecialFolderExtension.History) and self._show_history: |
||
141 | is_deleted_folder = re.search(r'/\.deleted/\.history$', path) is not None |
||
142 | is_archived_folder = re.search(r'/\.archived/\.history$', path) is not None |
||
143 | |||
144 | type = HistoryType.Deleted if is_deleted_folder \ |
||
145 | else HistoryType.Archived if is_archived_folder \ |
||
146 | else HistoryType.Standard |
||
147 | |||
148 | return resources.HistoryFolderResource( |
||
149 | path=path, |
||
150 | environ=environ, |
||
151 | workspace=workspace, |
||
152 | user=user, |
||
153 | content=content, |
||
154 | session=session, |
||
155 | type=type |
||
156 | ) |
||
157 | |||
158 | # Now that's more complicated, we're trying to find out if the path end with /.history/file_name |
||
159 | is_history_file_folder = re.search(r'/\.history/([^/]+)$', path) is not None |
||
160 | |||
161 | if is_history_file_folder and self._show_history: |
||
162 | return resources.HistoryFileFolderResource( |
||
163 | path=path, |
||
164 | environ=environ, |
||
165 | user=user, |
||
166 | content=content, |
||
167 | session=session, |
||
168 | ) |
||
169 | # And here next step : |
||
170 | is_history_file = re.search(r'/\.history/[^/]+/\((\d+) - [a-zA-Z]+\) .+', path) is not None |
||
171 | |||
172 | if self._show_history and is_history_file: |
||
173 | |||
174 | revision_id = re.search(r'/\.history/[^/]+/\((\d+) - [a-zA-Z]+\) ([^/].+)$', path).group(1) |
||
175 | |||
176 | content_revision = content_api.get_one_revision(revision_id) |
||
177 | content = self.get_content_from_revision(content_revision, content_api) |
||
178 | |||
179 | if content.type == content_type_list.File.slug: |
||
180 | return resources.HistoryFileResource( |
||
181 | path=path, |
||
182 | environ=environ, |
||
183 | user=user, |
||
184 | content=content, |
||
185 | content_revision=content_revision, |
||
186 | session=session, |
||
187 | ) |
||
188 | else: |
||
189 | return resources.HistoryOtherFile( |
||
190 | path=path, |
||
191 | environ=environ, |
||
192 | user=user, |
||
193 | content=content, |
||
194 | content_revision=content_revision, |
||
195 | session=session, |
||
196 | ) |
||
197 | |||
198 | # And if we're still going, the client is asking for a standard Folder/File/Page/Thread so we check the type7 |
||
199 | # and return the corresponding resource |
||
200 | |||
201 | if content is None: |
||
202 | return None |
||
203 | if content.type == content_type_list.Folder.slug: |
||
204 | return resources.FolderResource( |
||
205 | path=path, |
||
206 | environ=environ, |
||
207 | workspace=content.workspace, |
||
208 | content=content, |
||
209 | session=session, |
||
210 | user=user, |
||
211 | ) |
||
212 | elif content.type == content_type_list.File.slug: |
||
213 | return resources.FileResource( |
||
214 | path=path, |
||
215 | environ=environ, |
||
216 | content=content, |
||
217 | session=session, |
||
218 | user=user |
||
219 | ) |
||
220 | else: |
||
221 | return resources.OtherFileResource( |
||
222 | path=path, |
||
223 | environ=environ, |
||
224 | content=content, |
||
225 | session=session, |
||
226 | user=user, |
||
227 | ) |
||
373 |