| Conditions | 27 |
| Total Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 input_format_check() 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 | # |
||
| 165 | def input_format_check(args): |
||
| 166 | """Checks if the given arguments have a correct value |
||
| 167 | |||
| 168 | :param object args: Arguments object from argparser |
||
| 169 | """ |
||
| 170 | logmgr_flog() |
||
| 171 | |||
| 172 | if hasattr(args, 'status') and args.status is not None: |
||
| 173 | if args.status not in STATUSFLAGS: |
||
| 174 | print("Value of 'status' property is incorrect. " |
||
| 175 | "Expecting one of these values: " |
||
| 176 | "{}".format(", ".join(STATUSFLAGS))) |
||
| 177 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 178 | |||
| 179 | if hasattr(args, 'deadline') and args.deadline is not None: |
||
| 180 | match = re.match("^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$", args.deadline) |
||
| 181 | if match is None: |
||
| 182 | print("Value of 'deadline' is incorrect. " |
||
| 183 | "Use this date format: YYYY-MM-DD") |
||
| 184 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 185 | |||
| 186 | if hasattr(args, 'priority') and args.priority is not None: |
||
| 187 | errmsg = ("Value of 'priority' is incorrect. " |
||
| 188 | "Expecting a value between 1 and 10.") |
||
| 189 | |||
| 190 | if not args.priority.isnumeric(): |
||
| 191 | print(errmsg) |
||
| 192 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 193 | |||
| 194 | args.priority = int(args.priority) |
||
| 195 | if args.priority < 1 or args.priority > 10: |
||
| 196 | print(errmsg) |
||
| 197 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 198 | |||
| 199 | if hasattr(args, 'translation') and args.translation is not None: |
||
| 200 | values = ('yes', 'no') |
||
| 201 | if args.translation not in values: |
||
| 202 | print("Value of 'translation' is incorrect. " |
||
| 203 | "Expecting one of these values: yes or no") |
||
| 204 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 205 | |||
| 206 | if hasattr(args, 'languages') and args.languages is not None: |
||
| 207 | for i in args.languages.split(","): |
||
| 208 | if i not in LANGUAGES: |
||
| 209 | print("Value of 'languages' is incorrect. " |
||
| 210 | "Language code '{}' is not supported. " |
||
| 211 | "Type 'docmanager --langlist' to see " |
||
| 212 | "all supported language codes.".format(i)) |
||
| 213 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 214 | |||
| 215 | if hasattr(args, 'repository') and args.repository is not None: |
||
| 216 | request = None |
||
| 217 | try: |
||
| 218 | request = urllib.request.urlopen(args.repository) |
||
| 219 | except ValueError: |
||
| 220 | print("Value of 'repository' is incorrect. " |
||
| 221 | "The value is not a URL.") |
||
| 222 | sys.exit(ReturnCodes.E_WRONG_INPUT_FORMAT) |
||
| 223 | except urllib.error.URLError as err: |
||
| 224 | if hasattr(err, 'code') and err.code is not None: |
||
| 225 | if err.code is not 200: |
||
| 226 | log.warn("The remote server returns an error code " |
||
| 227 | "for this request: {} - Please double check if " |
||
| 228 | "the URL is correct. Nevertheless the URL will " |
||
| 229 | "be written into the " |
||
| 230 | "given files.".format(err.code)) |
||
| 231 | else: |
||
| 232 | log.warn("The given URL '{}' seems to be invalid or the " |
||
| 233 | "remote server is not online. Please double " |
||
| 234 | "check if the URL is correct. Nevertheless the " |
||
| 235 | "URL will be written into the " |
||
| 236 | "given files.".format(args.repository)) |
||
| 237 | |||
| 238 | if hasattr(request, 'close'): |
||
| 239 | request.close() |
||
| 240 | |||
| 281 |