| Conditions | 14 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 inspect_conda_package() 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 | from __future__ import print_function |
||
| 80 | def inspect_conda_package(filename, fileobj, *args, **kwargs): |
||
| 81 | |||
| 82 | index, recipe, has_prefix = None, {}, False |
||
| 83 | |||
| 84 | with tarfile.open(filename, fileobj=fileobj, mode="r|bz2") as tar: |
||
| 85 | for info in tar: |
||
| 86 | if info.name == 'info/index.json': |
||
| 87 | index = tar.extractfile(info) |
||
| 88 | index = json.loads(index.read().decode()) |
||
| 89 | elif info.name == 'info/recipe.json': |
||
| 90 | # recipe.index is deprecated and only packages built with older |
||
| 91 | # versions of conda-build contain that file. |
||
| 92 | recipe = tar.extractfile(info) |
||
| 93 | recipe = json.loads(recipe.read().decode()) |
||
| 94 | elif info.name == 'info/has_prefix': |
||
| 95 | has_prefix = True |
||
| 96 | if index is not None and recipe != {}: |
||
| 97 | break |
||
| 98 | else: |
||
| 99 | if index is None: |
||
| 100 | raise TypeError("info/index.json required in conda package") |
||
| 101 | |||
| 102 | # Load icon defined in the index.json and file exists inside info folder |
||
| 103 | fileobj.seek(0) |
||
| 104 | icon_b64 = None |
||
| 105 | icon_path = index.get('icon') |
||
| 106 | if icon_path: |
||
| 107 | tar = tarfile.open(filename, fileobj=fileobj, mode="r|bz2") |
||
| 108 | for info in tar: |
||
| 109 | if info.name == 'info/{0}'.format(icon_path): |
||
| 110 | icon_data = tar.extractfile(info).read() |
||
| 111 | f, temp_path = tempfile.mkstemp() |
||
| 112 | with open(temp_path, 'wb') as f: |
||
| 113 | f.write(icon_data) |
||
| 114 | icon_b64 = data_uri_from(temp_path) |
||
| 115 | break |
||
| 116 | |||
| 117 | about = recipe.pop('about', {}) |
||
| 118 | |||
| 119 | subdir = get_subdir(index) |
||
| 120 | |||
| 121 | machine = index['arch'] |
||
| 122 | operatingsystem = os_map.get(index['platform'], index['platform']) |
||
| 123 | |||
| 124 | package_data = { |
||
| 125 | 'name': index.pop('name'), |
||
| 126 | # TODO: this info should be removed and moved to release |
||
| 127 | 'summary': about.get('summary', ''), |
||
| 128 | 'license': about.get('license'), |
||
| 129 | } |
||
| 130 | release_data = { |
||
| 131 | 'version': index.pop('version'), |
||
| 132 | 'home_page': about.get('home'), |
||
| 133 | 'description': '', |
||
| 134 | # TODO: Add summary and license as per release attributes |
||
| 135 | # 'summary': about.get('summary', ''), |
||
| 136 | # 'license': about.get('license'), |
||
| 137 | 'icon': icon_b64, |
||
| 138 | } |
||
| 139 | file_data = { |
||
| 140 | 'basename': '%s/%s' % (subdir, path.basename(filename)), |
||
| 141 | 'attrs': { |
||
| 142 | 'operatingsystem': operatingsystem, |
||
| 143 | 'machine': machine, |
||
| 144 | 'target-triplet': '%s-any-%s' % (machine, operatingsystem), |
||
| 145 | 'has_prefix': has_prefix |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | file_data['attrs'].update(index) |
||
| 150 | conda_depends = index.get('depends', index.get('requires', [])) |
||
| 151 | file_data['dependencies'] = transform_conda_deps(conda_depends) |
||
| 152 | |||
| 153 | return package_data, release_data, file_data |
||
| 154 | |||
| 168 |