| Conditions | 14 |
| Total Lines | 62 |
| Code Lines | 30 |
| 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 ocrd.resolver.Resolver.workspace_from_url() 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 | import tempfile |
||
| 105 | def workspace_from_url(self, mets_url, dst_dir=None, clobber_mets=False, mets_basename=None, download=False, src_baseurl=None): |
||
| 106 | """ |
||
| 107 | Create a workspace from a METS by URL (i.e. clone it). |
||
| 108 | |||
| 109 | Sets the mets.xml file |
||
| 110 | |||
| 111 | Arguments: |
||
| 112 | mets_url (string): Source mets URL |
||
| 113 | dst_dir (string, None): Target directory for the workspace |
||
| 114 | clobber_mets (boolean, False): Whether to overwrite existing mets.xml. By default existing mets.xml will raise an exception. |
||
| 115 | download (boolean, False): Whether to download all the files |
||
| 116 | src_baseurl (string, None): Base URL for resolving relative file locations |
||
| 117 | |||
| 118 | Returns: |
||
| 119 | Workspace |
||
| 120 | """ |
||
| 121 | |||
| 122 | if mets_url is None: |
||
| 123 | raise ValueError("Must pass 'mets_url' workspace_from_url") |
||
| 124 | |||
| 125 | # if mets_url is a relative filename, make it absolute |
||
| 126 | if is_local_filename(mets_url) and not Path(mets_url).is_absolute(): |
||
| 127 | mets_url = str(Path(Path.cwd() / mets_url)) |
||
| 128 | |||
| 129 | # if mets_basename is not given, use the last URL segment of the mets_url |
||
| 130 | if mets_basename is None: |
||
| 131 | mets_basename = nth_url_segment(mets_url, -1) |
||
| 132 | |||
| 133 | # If src_baseurl wasn't given, determine from mets_url by removing last url segment |
||
| 134 | if not src_baseurl: |
||
| 135 | last_segment = nth_url_segment(mets_url) |
||
| 136 | src_baseurl = remove_non_path_from_url(remove_non_path_from_url(mets_url)[:-len(last_segment)]) |
||
| 137 | |||
| 138 | # resolve dst_dir |
||
| 139 | if not dst_dir: |
||
| 140 | if is_local_filename(mets_url): |
||
| 141 | log.debug("Deriving dst_dir %s from %s", Path(mets_url).parent, mets_url) |
||
| 142 | dst_dir = Path(mets_url).parent |
||
| 143 | else: |
||
| 144 | log.debug("Creating ephemeral workspace '%s' for METS @ <%s>", dst_dir, mets_url) |
||
| 145 | dst_dir = tempfile.mkdtemp(prefix=TMP_PREFIX) |
||
| 146 | # XXX Path.resolve is always strict in Python <= 3.5, so create dst_dir unless it exists consistently |
||
| 147 | if not Path(dst_dir).exists(): |
||
| 148 | Path(dst_dir).mkdir(parents=True, exist_ok=False) |
||
| 149 | dst_dir = str(Path(dst_dir).resolve()) |
||
| 150 | |||
| 151 | log.debug("workspace_from_url\nmets_basename='%s'\nmets_url='%s'\nsrc_baseurl='%s'\ndst_dir='%s'", |
||
| 152 | mets_basename, mets_url, src_baseurl, dst_dir) |
||
| 153 | |||
| 154 | self.download_to_directory(dst_dir, mets_url, basename=mets_basename, if_exists='overwrite' if clobber_mets else 'skip') |
||
| 155 | |||
| 156 | workspace = Workspace(self, dst_dir, mets_basename=mets_basename, baseurl=src_baseurl) |
||
| 157 | |||
| 158 | # XXX an empty dict is false-y but valid in this context |
||
| 159 | if download or download == {}: |
||
| 160 | if not isinstance(download, dict): |
||
| 161 | download = {} |
||
| 162 | mets_filter = OcrdMetsFilter(**download) |
||
| 163 | for f in mets_filter.find_files(workspace): |
||
| 164 | workspace.download_file(f) |
||
| 165 | |||
| 166 | return workspace |
||
| 167 | |||
| 183 |