Conditions | 18 |
Total Lines | 67 |
Code Lines | 54 |
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.
1 | import sys |
||
64 | @resmgr_cli.command('download') |
||
65 | @click.option('-n', '--any-url', help='Allow downloading/copying unregistered resources', is_flag=True) |
||
66 | @click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True) |
||
67 | @click.option('-l', '--location', help='Where to store resources', type=click.Choice(RESOURCE_LOCATIONS), default=config.resource_location, show_default=True) |
||
68 | @click.argument('executable', required=True) |
||
69 | @click.argument('url_or_name', required=True) |
||
70 | def download(any_url, overwrite, location, executable, url_or_name): |
||
71 | """ |
||
72 | Download resource URL_OR_NAME for processor EXECUTABLE. |
||
73 | |||
74 | URL_OR_NAME can either be the ``name`` or ``url`` of a registered resource. |
||
75 | |||
76 | If URL_OR_NAME is '*' (asterisk), download all known resources for this processor |
||
77 | |||
78 | If ``--any-url`` is given, also accepts URL or filenames of non-registered resources for ``URL_OR_NAME``. |
||
79 | """ |
||
80 | log = getLogger('ocrd.cli.resmgr') |
||
81 | resmgr = OcrdResourceManager() |
||
82 | basedir = resmgr.location_to_resource_dir(location) |
||
83 | is_url = url_or_name.startswith('https://') or url_or_name.startswith('http://') |
||
84 | is_filename = Path(url_or_name).exists() |
||
85 | find_kwargs = {'executable': executable} |
||
86 | if url_or_name != '*': |
||
87 | find_kwargs['url' if is_url else 'name'] = url_or_name |
||
88 | reslist = resmgr.find_resources(**find_kwargs) |
||
89 | if not reslist: |
||
90 | log.info("No resources found in registry") |
||
91 | if any_url and (is_url or is_filename): |
||
92 | log.info("%s unregistered resource %s" % ("Downloading" if is_url else "Copying", url_or_name)) |
||
93 | if is_url: |
||
94 | with requests.get(url_or_name, stream=True) as r: |
||
95 | content_length = int(r.headers.get('content-length')) |
||
96 | else: |
||
97 | url_or_name = str(Path(url_or_name).resolve()) |
||
98 | content_length = Path(url_or_name).stat().st_size |
||
99 | with click.progressbar(length=content_length, label="Downloading" if is_url else "Copying") as bar: |
||
100 | fpath = resmgr.download( |
||
101 | executable, |
||
102 | url_or_name, |
||
103 | overwrite=overwrite, |
||
104 | basedir=basedir, |
||
105 | progress_cb=lambda delta: bar.update(delta)) |
||
106 | log.info("%s resource '%s' (%s) not a known resource, creating stub in %s'" % (executable, fpath.name, url_or_name, resmgr.user_list)) |
||
107 | resmgr.add_to_user_database(executable, fpath, url_or_name) |
||
108 | log.info("%s %s to %s" % ("Downloaded" if is_url else "Copied", url_or_name, fpath)) |
||
109 | log.info("Use in parameters as '%s'" % fpath.name) |
||
110 | else: |
||
111 | sys.exit(1) |
||
112 | else: |
||
113 | for _, resdict in reslist: |
||
114 | if resdict['url'] == '???': |
||
115 | log.info("Cannot download user resource %s" % (resdict['name'])), |
||
116 | continue |
||
117 | log.info("Downloading resource %s" % resdict) |
||
118 | with click.progressbar(length=resdict['size']) as bar: |
||
119 | fpath = resmgr.download( |
||
120 | executable, |
||
121 | resdict['url'], |
||
122 | name=resdict['name'], |
||
123 | resource_type=resdict['type'], |
||
124 | path_in_archive=resdict.get('path_in_archive', '.'), |
||
125 | overwrite=overwrite, |
||
126 | basedir=basedir, |
||
127 | progress_cb=lambda delta: bar.update(delta) |
||
128 | ) |
||
129 | log.info("Downloaded %s to %s" % (resdict['url'], fpath)) |
||
130 | log.info("Use in parameters as '%s'" % resmgr.parameter_usage(resdict['name'], usage=resdict['parameter_usage'])) |
||
131 | |||
132 |