Conditions | 8 |
Total Lines | 63 |
Code Lines | 46 |
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 | # encoding: utf-8 |
||
23 | def main(): |
||
24 | cli_named_pipe = os.environ.get('exabgp_cli_pipe', '') |
||
25 | if cli_named_pipe: |
||
26 | from exabgp.application.pipe import main |
||
27 | |||
28 | main(cli_named_pipe) |
||
29 | sys.exit(0) |
||
30 | |||
31 | # compatibility with exabgp 4.x |
||
32 | if len(sys.argv) > 1 and not ('-h' in sys.argv or '--help' in sys.argv): |
||
33 | if sys.argv[1] not in ('version', 'cli', 'healthcheck', 'decode', 'server', 'env', 'validate'): |
||
34 | sys.argv = sys.argv[0:1] + ['server'] + sys.argv[1:] |
||
35 | |||
36 | formatter = argparse.RawDescriptionHelpFormatter |
||
37 | parser = argparse.ArgumentParser(description='The BGP swiss army knife of networking') |
||
38 | |||
39 | subparsers = parser.add_subparsers() |
||
40 | |||
41 | sub = subparsers.add_parser('version', help='report exabgp version', description=version.__doc__) |
||
42 | sub.set_defaults(func=version.cmdline) |
||
43 | version.setargs(sub) |
||
44 | |||
45 | sub = subparsers.add_parser('cli', help='control a running exabgp server instance', description=cli.__doc__) |
||
46 | sub.set_defaults(func=cli.cmdline) |
||
47 | cli.setargs(sub) |
||
48 | |||
49 | sub = subparsers.add_parser( |
||
50 | 'healthcheck', |
||
51 | help='monitor services and announce/withdraw routes', |
||
52 | description=healthcheck.__doc__, |
||
53 | formatter_class=formatter, |
||
54 | ) |
||
55 | sub.set_defaults(func=healthcheck.cmdline) |
||
56 | healthcheck.setargs(sub) |
||
57 | |||
58 | sub = subparsers.add_parser('env', help='show exabgp configuration information', description=environ.__doc__) |
||
59 | sub.set_defaults(func=environ.cmdline) |
||
60 | environ.setargs(sub) |
||
61 | |||
62 | sub = subparsers.add_parser('decode', help='decode hex-encoded bgp packets', description=decode.__doc__) |
||
63 | sub.set_defaults(func=decode.cmdline) |
||
64 | decode.setargs(sub) |
||
65 | |||
66 | sub = subparsers.add_parser('server', help='start exabgp', description=server.__doc__) |
||
67 | sub.set_defaults(func=server.cmdline) |
||
68 | server.setargs(sub) |
||
69 | |||
70 | sub = subparsers.add_parser('validate', help='validate configuration', description=validate.__doc__) |
||
71 | sub.set_defaults(func=validate.cmdline) |
||
72 | validate.setargs(sub) |
||
73 | |||
74 | try: |
||
75 | cmdarg = parser.parse_args() |
||
76 | except Exception as exc: |
||
77 | sys.exit(exc.args[-1]) |
||
78 | |||
79 | options = vars(cmdarg) |
||
80 | |||
81 | if 'func' in options: |
||
82 | cmdarg.func(cmdarg) |
||
83 | else: |
||
84 | parser.print_help() |
||
85 | environ.default() |
||
86 | |||
90 |