Conditions | 15 |
Total Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 2 |
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, about, 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 | about = recipe.pop('about', {}) |
||
95 | elif info.name == 'info/about.json': |
||
96 | # recipe.json is deprecated and only packages build with older |
||
97 | # versions of conda-build contain that file. |
||
98 | about = tar.extractfile(info) |
||
99 | about = json.loads(about.read().decode()) |
||
100 | elif info.name == 'info/has_prefix': |
||
101 | has_prefix = True |
||
102 | if index is not None and about != {}: |
||
103 | break |
||
104 | else: |
||
105 | if index is None: |
||
106 | raise TypeError("info/index.json required in conda package") |
||
107 | |||
108 | # Load icon defined in the index.json and file exists inside info folder |
||
109 | fileobj.seek(0) |
||
110 | icon_b64 = None |
||
111 | icon_path = index.get('icon') |
||
112 | if icon_path: |
||
113 | tar = tarfile.open(filename, fileobj=fileobj, mode="r|bz2") |
||
114 | for info in tar: |
||
115 | if info.name == 'info/{0}'.format(icon_path): |
||
116 | icon_data = tar.extractfile(info).read() |
||
117 | f, temp_path = tempfile.mkstemp() |
||
118 | with open(temp_path, 'wb') as f: |
||
119 | f.write(icon_data) |
||
120 | icon_b64 = data_uri_from(temp_path) |
||
121 | break |
||
122 | |||
123 | subdir = get_subdir(index) |
||
124 | |||
125 | machine = index['arch'] |
||
126 | operatingsystem = os_map.get(index['platform'], index['platform']) |
||
127 | |||
128 | package_data = { |
||
129 | 'name': index.pop('name'), |
||
130 | # TODO: this info should be removed and moved to release |
||
131 | 'summary': about.get('summary', ''), |
||
132 | 'description': about.get('description', ''), |
||
133 | 'license': about.get('license'), |
||
134 | 'license_url': about.get('license_url'), |
||
135 | 'license_family': about.get('license_family'), |
||
136 | 'dev_url': about.get('dev_url'), |
||
137 | 'doc_url': about.get('doc_url'), |
||
138 | 'home': about.get('home'), |
||
139 | 'source_git_url': about.get('source_git_url'), |
||
140 | } |
||
141 | release_data = { |
||
142 | 'version': index.pop('version'), |
||
143 | 'home_page': about.get('home'), |
||
144 | 'description': about.get('description', ''), |
||
145 | 'summary': about.get('summary', ''), |
||
146 | 'dev_url': about.get('dev_url'), |
||
147 | 'doc_url': about.get('doc_url'), |
||
148 | 'home': about.get('home'), |
||
149 | 'source_git_url': about.get('source_git_url'), |
||
150 | 'source_git_tag': about.get('source_git_tag'), |
||
151 | 'icon': icon_b64, |
||
152 | 'license': about.get('license'), |
||
153 | 'license_url': about.get('license_url'), |
||
154 | 'license_family': about.get('license_family'), |
||
155 | } |
||
156 | file_data = { |
||
157 | 'basename': '%s/%s' % (subdir, path.basename(filename)), |
||
158 | 'attrs': { |
||
159 | 'operatingsystem': operatingsystem, |
||
160 | 'machine': machine, |
||
161 | 'target-triplet': '%s-any-%s' % (machine, operatingsystem), |
||
162 | 'has_prefix': has_prefix |
||
163 | } |
||
164 | } |
||
165 | |||
166 | file_data['attrs'].update(index) |
||
167 | conda_depends = index.get('depends', index.get('requires', [])) |
||
168 | file_data['dependencies'] = transform_conda_deps(conda_depends) |
||
169 | |||
170 | return package_data, release_data, file_data |
||
171 | |||
185 |