| Conditions | 9 |
| Total Lines | 59 |
| Code Lines | 38 |
| 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 |
||
| 107 | def main( |
||
| 108 | name: str, |
||
| 109 | daemon_class: Type[OSPDaemon], |
||
| 110 | parser: Optional[ParserType] = None, |
||
| 111 | ): |
||
| 112 | """ OSPD Main function. """ |
||
| 113 | |||
| 114 | if not parser: |
||
| 115 | parser = create_parser(name) |
||
| 116 | args = parser.parse_arguments() |
||
| 117 | |||
| 118 | if args.version: |
||
| 119 | args.foreground = True |
||
| 120 | |||
| 121 | init_logging( |
||
| 122 | name, args.log_level, log_file=args.log_file, foreground=args.foreground |
||
| 123 | ) |
||
| 124 | |||
| 125 | if args.port == 0: |
||
| 126 | server = UnixSocketServer( |
||
| 127 | args.unix_socket, args.socket_mode, args.stream_timeout, |
||
| 128 | ) |
||
| 129 | else: |
||
| 130 | server = TlsServer( |
||
| 131 | args.address, |
||
| 132 | args.port, |
||
| 133 | args.cert_file, |
||
| 134 | args.key_file, |
||
| 135 | args.ca_file, |
||
| 136 | args.stream_timeout, |
||
| 137 | ) |
||
| 138 | |||
| 139 | daemon = daemon_class(**vars(args)) |
||
| 140 | |||
| 141 | if args.version: |
||
| 142 | print_version(daemon) |
||
| 143 | sys.exit() |
||
| 144 | |||
| 145 | if args.list_commands: |
||
| 146 | print(daemon.get_help_text()) |
||
| 147 | |||
| 148 | if not args.foreground: |
||
| 149 | go_to_background() |
||
| 150 | |||
| 151 | if not create_pid(args.pid_file): |
||
| 152 | sys.exit() |
||
| 153 | |||
| 154 | # Set signal handler and cleanup |
||
| 155 | atexit.register(remove_pidfile, pidfile=args.pid_file) |
||
| 156 | signal.signal(signal.SIGTERM, partial(remove_pidfile, args.pid_file)) |
||
| 157 | |||
| 158 | if not daemon.check(): |
||
| 159 | return 1 |
||
| 160 | |||
| 161 | daemon.init() |
||
| 162 | |||
| 163 | daemon.run(server) |
||
| 164 | |||
| 165 | return 0 |
||
| 166 |