| Conditions | 5 |
| Total Lines | 61 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
| 1 | # Copyright (C) 2019 Greenbone Networks GmbH |
||
| 54 | def main(name, klass): |
||
| 55 | """ OSPD Main function. """ |
||
| 56 | |||
| 57 | # Common args parser. |
||
| 58 | parser = create_args_parser(name) |
||
| 59 | |||
| 60 | # Common args |
||
| 61 | cargs = get_common_args(parser) |
||
| 62 | |||
| 63 | logging.getLogger().setLevel(cargs['log_level']) |
||
| 64 | |||
| 65 | wrapper = klass( |
||
| 66 | certfile=cargs['certfile'], |
||
| 67 | keyfile=cargs['keyfile'], |
||
| 68 | cafile=cargs['cafile'], |
||
| 69 | niceness=cargs['niceness'], |
||
| 70 | ) |
||
| 71 | |||
| 72 | if cargs['version']: |
||
| 73 | print_version(wrapper) |
||
| 74 | sys.exit() |
||
| 75 | |||
| 76 | if cargs['foreground']: |
||
| 77 | console = logging.StreamHandler() |
||
| 78 | console.setFormatter( |
||
| 79 | logging.Formatter( |
||
| 80 | '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format( |
||
| 81 | name |
||
| 82 | ) |
||
| 83 | ) |
||
| 84 | ) |
||
| 85 | logging.getLogger().addHandler(console) |
||
| 86 | elif cargs['log_file']: |
||
| 87 | logfile = WatchedFileHandler(cargs['log_file']) |
||
| 88 | logfile.setFormatter( |
||
| 89 | logging.Formatter( |
||
| 90 | '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format( |
||
| 91 | name |
||
| 92 | ) |
||
| 93 | ) |
||
| 94 | ) |
||
| 95 | logging.getLogger().addHandler(logfile) |
||
| 96 | go_to_background() |
||
| 97 | else: |
||
| 98 | syslog = SysLogHandler('/dev/log') |
||
| 99 | syslog.setFormatter( |
||
| 100 | logging.Formatter( |
||
| 101 | '{}: %(levelname)s: (%(name)s) %(message)s'.format(name) |
||
| 102 | ) |
||
| 103 | ) |
||
| 104 | logging.getLogger().addHandler(syslog) |
||
| 105 | # Duplicate syslog's file descriptor to stout/stderr. |
||
| 106 | syslog_fd = syslog.socket.fileno() |
||
| 107 | os.dup2(syslog_fd, 1) |
||
| 108 | os.dup2(syslog_fd, 2) |
||
| 109 | go_to_background() |
||
| 110 | |||
| 111 | if not wrapper.check(): |
||
| 112 | return 1 |
||
| 113 | |||
| 114 | return wrapper.run(cargs['address'], cargs['port'], cargs['unix_socket']) |
||
| 115 |
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.