main()   F
last analyzed

Complexity

Conditions 14

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 3
Metric Value
cc 14
dl 0
loc 43
rs 2.7581
c 7
b 0
f 3

How to fix   Complexity   

Complexity

Complex classes like main() 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
"""
2
Manage your Anaconda repository channels.
3
"""
4
5
from __future__ import unicode_literals, print_function
6
7
from binstar_client.utils import get_server_api
8
import functools
9
import logging
10
import argparse
11
12
logger = logging.getLogger('binstar.channel')
13
14
15
def main(args, name, deprecated=False):
16
    aserver_api = get_server_api(args.token, args.site)
17
18
    if args.organization:
19
        owner = args.organization
20
    else:
21
        current_user = aserver_api.user()
22
        owner = current_user['login']
23
24
    if deprecated:
25
        logger.warning('channel command is deprecated in favor of label')
26
27
    if args.copy:
28
        aserver_api.copy_channel(args.copy[0], owner, args.copy[1])
29
        logger.info("Copied {} {} to {}".format(name, *tuple(args.copy)))
30
    elif args.remove:
31
        aserver_api.remove_channel(args.remove, owner)
32
        logger.info("Removed {} {}".format(name, args.remove))
33
    elif args.list:
34
        logger.info('{}s'.format(name.title()))
35
        for channel, info in aserver_api.list_channels(owner).items():
36
            if isinstance(info, int):  # OLD API
37
                logger.info((' + %s ' % channel))
38
            else:
39
                logger.info((' + %s ' % channel) + ('[locked]' if info['is_locked'] else ''))
40
41
    elif args.show:
42
        info = aserver_api.show_channel(args.show, owner)
43
        logger.info('{} {} {}'.format(
44
            name.title(),
45
            args.show,
46
            ('[locked]' if info['is_locked'] else '')
47
        ))
48
        for f in info['files']:
49
            logger.info('  + %(full_name)s' % f)
50
    elif args.lock:
51
        aserver_api.lock_channel(args.lock, owner)
52
        logger.info("{} {} is now locked".format(name.title(), args.lock))
53
    elif args.unlock:
54
        aserver_api.unlock_channel(args.unlock, owner)
55
        logger.info("{} {} is now unlocked".format(name.title(), args.unlock))
56
    else:
57
        raise NotImplementedError()
58
59
60
def _add_parser(subparsers, name, deprecated=False):
61
    deprecated_warn = ""
62
    if deprecated:
63
        deprecated_warn = "[DEPRECATED in favor of label] \n"
64
65
    subparser = subparsers.add_parser(
66
        name,
67
        help='{}Manage your Anaconda repository {}s'.format(deprecated_warn, name),
68
        formatter_class=argparse.RawDescriptionHelpFormatter,
69
        description=__doc__)
70
71
    subparser.add_argument('-o', '--organization',
72
                           help="Manage an organizations {}s".format(name))
73
74
    group = subparser.add_mutually_exclusive_group(required=True)
75
76
    group.add_argument('--copy', nargs=2, metavar=name.upper())
77
    group.add_argument(
78
        '--list',
79
        action='store_true',
80
        help="{}list all {}s for a user".format(deprecated_warn, name)
81
    )
82
    group.add_argument(
83
        '--show',
84
        metavar=name.upper(),
85
        help="{}Show all of the files in a {}".format(deprecated_warn, name)
86
    )
87
    group.add_argument(
88
        '--lock',
89
        metavar=name.upper(),
90
        help="{}Lock a {}".format(deprecated_warn, name))
91
    group.add_argument(
92
        '--unlock',
93
        metavar=name.upper(),
94
        help="{}Unlock a {}".format(deprecated_warn, name)
95
    )
96
    group.add_argument(
97
        '--remove',
98
        metavar=name.upper(),
99
        help="{}Remove a {}".format(deprecated_warn, name)
100
    )
101
    subparser.set_defaults(main=functools.partial(main, name=name, deprecated=deprecated))
102
103
def add_parser(subparsers):
104
    _add_parser(subparsers, name="label")
105
    _add_parser(subparsers, name="channel", deprecated=True)
106