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