Completed
Pull Request — master (#59)
by Gonzalo
57s
created

process_labels()   F

Complexity

Conditions 12

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
dl 0
loc 45
rs 2.7855

How to fix   Complexity   

Complexity

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 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) The Spyder Development Team
4
#
5
# Licensed under the terms of the MIT License
6
# (See LICENSE.txt for details)
7
# -----------------------------------------------------------------------------
8
"""Get/Create github labels from a text file."""
9
10
# yapf: disable
11
12
# Local imports
13
from loghub.core.repo import GitHubRepo
14
15
16
# yapf: enable
17
18
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