Conditions | 25 |
Total Lines | 94 |
Code Lines | 76 |
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.cli.resmgr.download() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | """ |
||
66 | @resmgr_cli.command('download') |
||
67 | @click.option('-n', '--any-url', help='URL of unregistered resource to download/copy from', default='') |
||
68 | @click.option('-D', '--no-dynamic', is_flag=True, default=False, help="Whether to skip looking into each processor's --dump-json for module-level resources") |
||
69 | @click.option('-t', '--resource-type', help='Type of resource', type=click.Choice(['file', 'directory', 'archive']), default='file') |
||
70 | @click.option('-P', '--path-in-archive', help='Path to extract in case of archive type', default='.') |
||
71 | @click.option('-a', '--allow-uninstalled', help="Allow installing resources for uninstalled processors", is_flag=True) |
||
72 | @click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True) |
||
73 | @click.option('-l', '--location', help='Where to store resources', type=click.Choice(RESOURCE_LOCATIONS), default='data', show_default=True) |
||
74 | @click.argument('executable', required=True) |
||
75 | @click.argument('name', required=False) |
||
76 | def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstalled, overwrite, location, executable, name): |
||
77 | """ |
||
78 | Download resource NAME for processor EXECUTABLE. |
||
79 | |||
80 | NAME is the name of the resource made available by downloading or copying. |
||
81 | |||
82 | If NAME is '*' (asterisk), then download all known registered resources for this processor. |
||
83 | |||
84 | If ``--any-url=URL`` or ``-n URL`` is given, then URL is accepted regardless of registered resources for ``NAME``. |
||
85 | (This can be used for unknown resources or for replacing registered resources.) |
||
86 | |||
87 | If ``--resource-type`` is set to `archive`, then that archive gets unpacked after download, |
||
88 | and its ``--path-in-archive`` will subsequently be renamed to NAME. |
||
89 | """ |
||
90 | log = getLogger('ocrd.cli.resmgr') |
||
91 | resmgr = OcrdResourceManager() |
||
92 | basedir = resmgr.location_to_resource_dir(location) |
||
93 | if executable != '*' and not name: |
||
94 | log.error("Unless EXECUTABLE ('%s') is the '*' wildcard, NAME is required" % executable) |
||
95 | sys.exit(1) |
||
96 | elif executable == '*': |
||
97 | executable = None |
||
98 | if name == '*': |
||
99 | name = None |
||
100 | is_url = (any_url.startswith('https://') or any_url.startswith('http://')) if any_url else False |
||
101 | is_filename = Path(any_url).exists() if any_url else False |
||
102 | if executable and not which(executable): |
||
103 | if not allow_uninstalled: |
||
104 | log.error("Executable '%s' is not installed. " \ |
||
105 | "To download resources anyway, use the -a/--allow-uninstalled flag", executable) |
||
106 | sys.exit(1) |
||
107 | else: |
||
108 | log.info("Executable %s is not installed, but " \ |
||
109 | "downloading resources anyway", executable) |
||
110 | reslist = resmgr.list_available(executable=executable, dynamic=not no_dynamic) |
||
111 | if name: |
||
112 | reslist = [(executable, r) for _, rs in reslist for r in rs if r['name'] == name] |
||
113 | if not reslist: |
||
114 | log.info(f"No resources {name} found in registry for executable {executable}") |
||
115 | if executable and name: |
||
116 | reslist = [(executable, {'url': any_url or '???', 'name': name, |
||
117 | 'type': resource_type, |
||
118 | 'path_in_archive': path_in_archive})] |
||
119 | for executable, resdict in reslist: |
||
120 | if 'size' in resdict: |
||
121 | registered = "registered" |
||
122 | else: |
||
123 | registered = "unregistered" |
||
124 | if any_url: |
||
125 | resdict['url'] = any_url |
||
126 | if resdict['url'] == '???': |
||
127 | log.warning("Cannot download user resource %s", resdict['name']) |
||
128 | continue |
||
129 | if resdict['url'].startswith('https://') or resdict['url'].startswith('http://'): |
||
130 | log.info("Downloading %s resource '%s' (%s)", registered, resdict['name'], resdict['url']) |
||
131 | with requests.get(resdict['url'], stream=True) as r: |
||
132 | resdict['size'] = int(r.headers.get('content-length')) |
||
133 | else: |
||
134 | log.info("Copying %s resource '%s' (%s)", registered, resdict['name'], resdict['url']) |
||
135 | urlpath = Path(resdict['url']) |
||
136 | resdict['url'] = str(urlpath.resolve()) |
||
137 | if Path(urlpath).is_dir(): |
||
138 | resdict['size'] = directory_size(urlpath) |
||
139 | else: |
||
140 | resdict['size'] = urlpath.stat().st_size |
||
141 | with click.progressbar(length=resdict['size']) as bar: |
||
142 | fpath = resmgr.download( |
||
143 | executable, |
||
144 | resdict['url'], |
||
145 | name=resdict['name'], |
||
146 | resource_type=resdict.get('type', resource_type), |
||
147 | path_in_archive=resdict.get('path_in_archive', path_in_archive), |
||
148 | overwrite=overwrite, |
||
149 | size=resdict['size'], |
||
150 | no_subdir=location == 'cwd', |
||
151 | basedir=basedir, |
||
152 | progress_cb=lambda delta: bar.update(delta) |
||
|
|||
153 | ) |
||
154 | if registered == 'unregistered': |
||
155 | log.info("%s resource '%s' (%s) not a known resource, creating stub in %s'", executable, name, any_url, resmgr.user_list) |
||
156 | resmgr.add_to_user_database(executable, fpath, url=any_url) |
||
157 | resmgr.save_user_list() |
||
158 | log.info("Installed resource %s under %s", resdict['url'], fpath) |
||
159 | log.info("Use in parameters as '%s'", resmgr.parameter_usage(resdict['name'], usage=resdict.get('parameter_usage', 'as-is'))) |
||
160 | |||
196 |