Conditions | 10 |
Total Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 10 |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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 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 -*- |
||
14 | 1 | def main(args=None): |
|
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.', |
||
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: |
|
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]) |
|
86 | 1 | except TypeError: |
|
87 | 1 | print (key + ' = ' + ' '.join(data[key])) |
|
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: |
|
102 | 1 | print("Error Writing version config file : "+str(exc)) |
|
103 | |||
104 | 1 | return 1 |
|
105 | |||
108 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.