| Conditions | 9 |
| Total Lines | 59 |
| 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 |
||
| 128 | def main( |
||
| 129 | name: str, |
||
| 130 | daemon_class: Type[OSPDaemon], |
||
| 131 | parser: Optional[ParserType] = None, |
||
| 132 | ): |
||
| 133 | """ OSPD Main function. """ |
||
| 134 | |||
| 135 | if not parser: |
||
| 136 | parser = create_parser(name) |
||
| 137 | args = parser.parse_arguments() |
||
| 138 | |||
| 139 | if args.version: |
||
| 140 | args.foreground = True |
||
| 141 | |||
| 142 | init_logging( |
||
| 143 | name, args.log_level, log_file=args.log_file, foreground=args.foreground |
||
| 144 | ) |
||
| 145 | |||
| 146 | if args.port == 0: |
||
| 147 | server = UnixSocketServer( |
||
| 148 | args.unix_socket, args.socket_mode, args.stream_timeout, |
||
| 149 | ) |
||
| 150 | else: |
||
| 151 | server = TlsServer( |
||
| 152 | args.address, |
||
| 153 | args.port, |
||
| 154 | args.cert_file, |
||
| 155 | args.key_file, |
||
| 156 | args.ca_file, |
||
| 157 | args.stream_timeout, |
||
| 158 | ) |
||
| 159 | |||
| 160 | daemon = daemon_class(**vars(args)) |
||
| 161 | |||
| 162 | if args.version: |
||
| 163 | print_version(daemon) |
||
| 164 | sys.exit() |
||
| 165 | |||
| 166 | if args.list_commands: |
||
| 167 | print(daemon.get_help_text()) |
||
| 168 | sys.exit() |
||
| 169 | |||
| 170 | if not args.foreground: |
||
| 171 | go_to_background() |
||
| 172 | |||
| 173 | if not create_pid(args.pid_file): |
||
| 174 | sys.exit() |
||
| 175 | |||
| 176 | # Set signal handler and cleanup |
||
| 177 | atexit.register(exit_cleanup, pidfile=args.pid_file, server=server) |
||
| 178 | signal.signal(signal.SIGTERM, partial(exit_cleanup, args.pid_file, server)) |
||
| 179 | |||
| 180 | if not daemon.check(): |
||
| 181 | return 1 |
||
| 182 | |||
| 183 | daemon.init(server) |
||
| 184 | daemon.run() |
||
| 185 | |||
| 186 | return 0 |
||
| 187 |