Conditions | 17 |
Total Lines | 81 |
Code Lines | 62 |
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 | """ |
||
71 | @resmgr_cli.command('download') |
||
72 | @click.option('-n', '--any-url', default='', help='URL of unregistered resource to download/copy from') |
||
73 | @click.option('-D', '--no-dynamic', default=False, is_flag=True, |
||
74 | help="Whether to skip looking into each processor's --dump-{json,module-dir} for module-level resources") |
||
75 | @click.option('-t', '--resource-type', type=click.Choice(RESOURCE_TYPES), default='file', |
||
76 | help='Type of resource',) |
||
77 | @click.option('-P', '--path-in-archive', default='.', help='Path to extract in case of archive type') |
||
78 | @click.option('-a', '--allow-uninstalled', is_flag=True, |
||
79 | help="Allow installing resources for uninstalled processors",) |
||
80 | @click.option('-o', '--overwrite', help='Overwrite existing resources', is_flag=True) |
||
81 | @click.option('-l', '--location', type=click.Choice(RESOURCE_LOCATIONS), default='data', |
||
82 | help="Where to store resources - defaults to first location in processor's 'resource_locations' " |
||
83 | "list or finally 'data'") |
||
84 | @click.argument('executable', required=True) |
||
85 | @click.argument('name', required=False) |
||
86 | def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstalled, overwrite, location, executable, |
||
87 | name): |
||
88 | """ |
||
89 | Download resource NAME for processor EXECUTABLE. |
||
90 | |||
91 | NAME is the name of the resource made available by downloading or copying. |
||
92 | |||
93 | If NAME is '*' (asterisk), then download all known registered resources for this processor. |
||
94 | |||
95 | If ``--any-url=URL`` or ``-n URL`` is given, then URL is accepted regardless of registered resources for ``NAME``. |
||
96 | (This can be used for unknown resources or for replacing registered resources.) |
||
97 | |||
98 | If ``--resource-type`` is set to `archive`, then that archive gets unpacked after download, |
||
99 | and its ``--path-in-archive`` will subsequently be renamed to NAME. |
||
100 | """ |
||
101 | log = getLogger('ocrd.cli.resmgr') |
||
102 | resmgr = OcrdResourceManager() |
||
103 | if executable != '*' and not name: |
||
104 | log.error(f"Unless EXECUTABLE ('{executable}') is the '*' wildcard, NAME is required") |
||
105 | sys.exit(1) |
||
106 | elif executable == '*': |
||
107 | executable = None |
||
108 | if name == '*': |
||
109 | name = None |
||
110 | if executable and not which(executable): |
||
111 | if not allow_uninstalled: |
||
112 | log.error(f"Executable '{executable}' is not installed. " |
||
113 | f"To download resources anyway, use the -a/--allow-uninstalled flag") |
||
114 | sys.exit(1) |
||
115 | else: |
||
116 | log.info(f"Executable '{executable}' is not installed, but downloading resources anyway") |
||
117 | reslist = resmgr.list_available(executable=executable, dynamic=not no_dynamic, name=name) |
||
118 | if not any(r[1] for r in reslist): |
||
119 | log.info(f"No resources {name} found in registry for executable {executable}") |
||
120 | if executable and name: |
||
121 | reslist = [(executable, [{ |
||
122 | 'url': any_url or '???', |
||
123 | 'name': name, |
||
124 | 'type': resource_type, |
||
125 | 'path_in_archive': path_in_archive}] |
||
126 | )] |
||
127 | for this_executable, this_reslist in reslist: |
||
128 | resource_locations = get_ocrd_tool_json(this_executable)['resource_locations'] |
||
129 | if not location: |
||
130 | location = resource_locations[0] |
||
131 | elif location not in resource_locations: |
||
132 | log.warning(f"The selected --location {location} is not in the {this_executable}'s resource search path, " |
||
133 | f"refusing to install to invalid location. Instead installing to: {resource_locations[0]}") |
||
134 | res_dest_dir = resmgr.build_resource_dest_dir(location=location, executable=this_executable) |
||
135 | for res_dict in this_reslist: |
||
136 | try: |
||
137 | fpath = resmgr.handle_resource( |
||
138 | res_dict=res_dict, |
||
139 | executable=this_executable, |
||
140 | dest_dir=res_dest_dir, |
||
141 | any_url=any_url, |
||
142 | overwrite=overwrite, |
||
143 | resource_type=resource_type, |
||
144 | path_in_archive=path_in_archive |
||
145 | ) |
||
146 | if not fpath: |
||
147 | continue |
||
148 | except FileExistsError as exc: |
||
149 | log.info(str(exc)) |
||
150 | usage = res_dict.get('parameter_usage', 'as-is') |
||
151 | log.info(f"Use in parameters as '{resmgr.parameter_usage(res_dict['name'], usage)}'") |
||
152 | |||
188 |