Completed
Push — master ( 790092...745d04 )
by Charles
02:04
created

main()   F

Complexity

Conditions 10

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
dl 0
loc 91
ccs 32
cts 32
cp 1
crap 10
rs 3.3962
c 3
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3 1
import os
4 1
import sys
5 1
import argparse
6 1
import git_app_version.version
7 1
from git_app_version.git import Git
8 1
from git_app_version.dumper import FileDumper
9
10 1
__version__ = git_app_version.version.__version__
11 1
__DESCRIPTION__ = 'Get Git commit informations and store them in a INI/XML/YAML/JSON file.'
12
13
14 1
def main(args=None):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
15 1
    parser = argparse.ArgumentParser(
16
        prog='git-app-version',
17
        description=__DESCRIPTION__)
18 1
    parser.add_argument(
19
        '-V',
20
        '--version',
21
        action='version',
22
        version='%(prog)s ' +
23
        __version__,
24
        help='display tool version')
25 1
    parser.add_argument('-v', '--verbose', action='count',
26
                        help='increase verbosity : use -v or -vv')
27 1
    parser.add_argument(
28
        '-q',
29
        '--quiet',
30
        action='store_true',
31
        help='silent mode')
32 1
    parser.add_argument(
33
        'repository',
34
        nargs='?',
35
        metavar='path',
36
        type=str,
37
        help='git repository path. Default is the current directory.',
38
        default=os.getcwd())
39 1
    parser.add_argument(
40
        'commit',
41
        nargs='?',
42
        type=str,
43
        help='git commit to check. Default is HEAD.',
44
        default='HEAD')
45 1
    parser.add_argument(
46
        '-o',
47
        '--output',
48
        metavar='path',
49
        type=str,
50
        help='output file path (without extension). Default is \'<repository-path>/version\'.',
51
        default='version')
52 1
    parser.add_argument(
53
        '-f',
54
        '--format',
55
        metavar='format',
56
        type=str,
57
        help='output file format and extension (ini/xml/yml/json). Default is json.',
58
        default='json')
59 1
    parser.add_argument(
60
        '-n',
61
        '--namespace',
62
        metavar='namespace',
63
        type=str,
64
        help='namespace like notation in version file, use dot separator to segment namespaces e.g.: \'foo.bar.git\'. Default is \'app_version\' for XML and INI and no namespace for JSON and YAML.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (198/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
65
        default='')
66
67 1
    options = parser.parse_args(sys.argv[1:] if args is None else args)
68
69 1
    try:
70 1
        vcs = Git()
71
72 1
        if not vcs.is_git_repo(options.repository):
73 1
            raise Exception(
74
                'The directory \'' +
75
                options.repository +
76
                '\' is not a git repository.')
77
78 1
        data = vcs.get_infos(commit=options.commit, cwd=options.repository)
79
80 1
        if not options.quiet and options.verbose is not None and options.verbose  >= 1:
0 ignored issues
show
Coding Style introduced by
Exactly one space required before comparison
if not options.quiet and options.verbose is not None and options.verbose >= 1:
^^
Loading history...
81 1
            print('Git commit :')
82 1
            keys = sorted(data.keys())
83 1
            for key in keys:
84 1
                try:
85 1
                    print (key + ' = ' + data[key])
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
print (key + ' = ' + data[key])
^
Loading history...
86 1
                except TypeError:
87 1
                    print (key + ' = ' + ' '.join(data[key]))
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
print (key + ' = ' + ' '.join(data[key]))
^
Loading history...
88
89 1
        dumper = FileDumper()
90 1
        dest = dumper.dump(
91
            data=data,
92
            fileformat=options.format,
93
            target=options.output,
94
            cwd=options.repository,
95
            namespace=options.namespace)
96 1
        if not options.quiet:
97 1
            print("Git commit informations stored in "+dest)
98
99 1
        return 0
100
101 1
    except Exception as exc:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
102 1
        print("Error Writing version config file : "+str(exc))
103
104 1
        return 1
105
106 1
if __name__ == '__main__':
107
    sys.exit(main())
108