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