Conditions | 12 |
Total Lines | 152 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 parse_arguments() 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 -*- |
||
30 | def parse_arguments(skip=False): |
||
31 | """Main script.""" |
||
32 | # Cli options |
||
33 | parser = argparse.ArgumentParser( |
||
34 | description='Script to print the list of issues and pull requests ' |
||
35 | 'closed in a given milestone') |
||
36 | parser.add_argument( |
||
37 | 'repository', |
||
38 | help="Repository name to generate the Changelog for, in the form " |
||
39 | "user/repo or org/repo (e.g. spyder-ide/spyder)") |
||
40 | parser.add_argument( |
||
41 | '-m', |
||
42 | '--milestone', |
||
43 | action="store", |
||
44 | dest="milestone", |
||
45 | default='', |
||
46 | help="Github milestone to get issues and pull requests for") |
||
47 | parser.add_argument( |
||
48 | '-ilg', |
||
49 | '--issue-label-group', |
||
50 | action="append", |
||
51 | nargs='+', |
||
52 | dest="issue_label_groups", |
||
53 | help="Groups the generated issues by the specified label. This option" |
||
54 | "Takes 1 or 2 arguments, where the first one is the label to " |
||
55 | "match and the second one is the label to print on the final" |
||
56 | "output") |
||
57 | parser.add_argument( |
||
58 | '-ilr', |
||
59 | '--issue-label-regex', |
||
60 | action="store", |
||
61 | dest="issue_label_regex", |
||
62 | default='', |
||
63 | help="Label issue filter using a regular expression filter") |
||
64 | parser.add_argument( |
||
65 | '-plr', |
||
66 | '--pr-label-regex', |
||
67 | action="store", |
||
68 | dest="pr_label_regex", |
||
69 | default='', |
||
70 | help="Label pull requets filter using a regular expression filter") |
||
71 | parser.add_argument( |
||
72 | '-st', |
||
73 | '--since-tag', |
||
74 | action="store", |
||
75 | dest="since_tag", |
||
76 | default='', |
||
77 | help="Github issues and pull requests since tag") |
||
78 | parser.add_argument( |
||
79 | '-ut', |
||
80 | '--until-tag', |
||
81 | action="store", |
||
82 | dest="until_tag", |
||
83 | default='', |
||
84 | help="Github issues and pull requests until tag") |
||
85 | parser.add_argument( |
||
86 | '-b', |
||
87 | '--branch', |
||
88 | action="store", |
||
89 | dest="branch", |
||
90 | default='', |
||
91 | help="Github base branch for merged PRs") |
||
92 | parser.add_argument( |
||
93 | '-f', |
||
94 | '--format', |
||
95 | action="store", |
||
96 | dest="output_format", |
||
97 | default='changelog', |
||
98 | help="Format for print, either 'changelog' (for " |
||
99 | "Changelog.md file) or 'release' (for the Github " |
||
100 | "Releases page). Default is 'changelog'. The " |
||
101 | "'release' option doesn't generate Markdown " |
||
102 | "hyperlinks.") |
||
103 | parser.add_argument( |
||
104 | '--template', |
||
105 | action="store", |
||
106 | dest="template", |
||
107 | default='', |
||
108 | help="Use a custom Jinja2 template file ") |
||
109 | parser.add_argument( |
||
110 | '-u', |
||
111 | '--user', |
||
112 | action="store", |
||
113 | dest="user", |
||
114 | default='', |
||
115 | help="Github user name") |
||
116 | parser.add_argument( |
||
117 | '-p', |
||
118 | '--password', |
||
119 | action="store", |
||
120 | dest="password", |
||
121 | default='', |
||
122 | help="Github user password") |
||
123 | parser.add_argument( |
||
124 | '-t', |
||
125 | '--token', |
||
126 | action="store", |
||
127 | dest="token", |
||
128 | default='', |
||
129 | help="Github access token") |
||
130 | options = parser.parse_args() |
||
131 | |||
132 | username = options.user |
||
133 | password = options.password |
||
134 | milestone = options.milestone |
||
135 | issue_label_groups = options.issue_label_groups |
||
136 | |||
137 | if username and not password: |
||
138 | password = getpass.getpass() |
||
139 | |||
140 | # Check if repo given |
||
141 | if not options.repository: |
||
142 | print('LOGHUB: Please define a repository name to this script. ' |
||
143 | 'See its help') |
||
144 | sys.exit(1) |
||
145 | |||
146 | # Check if milestone or tag given |
||
147 | if not milestone and not options.since_tag: |
||
148 | print('\nLOGHUB: Querying all issues\n') |
||
149 | elif milestone: |
||
150 | print('\nLOGHUB: Querying issues for milestone {0}' |
||
151 | '\n'.format(milestone)) |
||
152 | |||
153 | new_issue_label_groups = [] |
||
154 | if issue_label_groups: |
||
155 | for item in issue_label_groups: |
||
156 | dic = {} |
||
157 | if len(item) == 1: |
||
158 | dic['label'] = item[0] |
||
159 | dic['name'] = item[0] |
||
160 | elif len(item) >= 2: |
||
161 | dic['label'] = item[0] |
||
162 | dic['name'] = item[1] |
||
163 | new_issue_label_groups.append(dic) |
||
164 | |||
165 | if not skip: |
||
166 | create_changelog( |
||
167 | repo=options.repository, |
||
168 | username=username, |
||
169 | password=password, |
||
170 | token=options.token, |
||
171 | milestone=milestone, |
||
172 | since_tag=options.since_tag, |
||
173 | until_tag=options.until_tag, |
||
174 | branch=options.branch, |
||
175 | issue_label_regex=options.issue_label_regex, |
||
176 | pr_label_regex=options.pr_label_regex, |
||
177 | output_format=options.output_format, |
||
178 | template_file=options.template, |
||
179 | issue_label_groups=new_issue_label_groups) |
||
180 | |||
181 | return options |
||
182 | |||
336 |