| Conditions | 12 |
| Total Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like process_labels() 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 | # -*- coding: utf-8 -*- |
||
| 19 | def process_labels(username, password, token, action, repo, filename): |
||
| 20 | """Get or update labels on a given repository.""" |
||
| 21 | # Separator |
||
| 22 | s = ';' |
||
| 23 | |||
| 24 | # Instantiate Github API |
||
| 25 | gh = GitHubRepo( |
||
| 26 | username=username, |
||
| 27 | password=password, |
||
| 28 | token=token, |
||
| 29 | repo=repo, ) |
||
| 30 | |||
| 31 | labels = gh.labels() |
||
| 32 | |||
| 33 | if action == 'get': |
||
| 34 | print('Getting labels from {0}\n'.format(repo)) |
||
| 35 | labels = sorted((l.name, l.color) for l in labels) |
||
| 36 | data = ''.join('{0}{1}{2}\n'.format(l, s, c) for (l, c) in labels) |
||
| 37 | |||
| 38 | with open(filename, 'wt') as f: |
||
| 39 | f.write(data[:-1]) |
||
| 40 | |||
| 41 | elif action == 'update': |
||
| 42 | print('Updating labels on {0}\n'.format(repo)) |
||
| 43 | with open(filename, 'r') as f: |
||
| 44 | data = f.read() |
||
| 45 | |||
| 46 | lines = [line for line in data.split('\n') if line] |
||
| 47 | labels = [] |
||
| 48 | for line in lines: |
||
| 49 | parts = [p for p in line.strip().split(s)] |
||
| 50 | if len(parts) == 3: |
||
| 51 | old_name, new_name, color = parts |
||
| 52 | else: |
||
| 53 | new_name, color = parts |
||
| 54 | old_name = new_name |
||
| 55 | |||
| 56 | label_dict = { |
||
| 57 | 'new_name': new_name, |
||
| 58 | 'old_name': old_name, |
||
| 59 | 'color': color |
||
| 60 | } |
||
| 61 | labels.append(label_dict) |
||
| 62 | |||
| 63 | gh.set_labels(labels) |
||
| 64 |