Conditions | 6 |
Total Lines | 70 |
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 | # -*- coding: utf-8 -*- |
||
50 | def main(): |
||
51 | do_not_run_as_root() |
||
52 | |||
53 | parser = create_parser(description=HELP_TEXT, logfilename='gvm-script.log') |
||
54 | |||
55 | parser.add_protocol_argument() |
||
56 | |||
57 | parser.add_argument( |
||
58 | 'scriptname', |
||
59 | metavar="SCRIPT", |
||
60 | help='Path to script to be executed (example: myscript.gmp.py)', |
||
61 | ) |
||
62 | parser.add_argument( |
||
63 | 'scriptargs', nargs='*', metavar="ARG", help='Arguments for the script' |
||
64 | ) |
||
65 | args, script_args = parser.parse_known_args() |
||
66 | |||
67 | connection = create_connection(**vars(args)) |
||
68 | |||
69 | transform = EtreeCheckCommandTransform() |
||
70 | |||
71 | global_vars = { |
||
72 | '__version__': __version__, |
||
73 | '__api_version__': __api_version__, |
||
74 | } |
||
75 | |||
76 | username = None |
||
77 | password = None |
||
78 | |||
79 | if args.protocol == PROTOCOL_OSP: |
||
80 | protocol_class = Osp |
||
81 | name = 'osp' |
||
82 | else: |
||
83 | protocol_class = Gmp |
||
84 | name = 'gmp' |
||
85 | |||
86 | try: |
||
87 | with protocol_class(connection, transform=transform) as protocol: |
||
88 | global_vars[name] = protocol |
||
89 | global_vars['__name__'] = '__{}__'.format(name) |
||
90 | |||
91 | if args.protocol == PROTOCOL_GMP: |
||
92 | if args.gmp_username: |
||
93 | (username, password) = authenticate( |
||
94 | protocol, |
||
95 | username=args.gmp_username, |
||
96 | password=args.gmp_password, |
||
97 | ) |
||
98 | |||
99 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
100 | |||
101 | shell_args = Namespace( |
||
102 | username=username, |
||
103 | password=password, |
||
104 | argv=argv, |
||
105 | # for backwards compatibility we add script here |
||
106 | script=argv, |
||
107 | # the unknown args, which are owned by the script. |
||
108 | script_args=script_args, |
||
109 | ) |
||
110 | |||
111 | global_vars['args'] = shell_args |
||
112 | |||
113 | run_script(args.scriptname, global_vars) |
||
114 | |||
115 | except Exception as e: # pylint: disable=broad-except |
||
116 | print(e, file=sys.stderr) |
||
117 | sys.exit(1) |
||
118 | |||
119 | sys.exit(0) |
||
120 | |||
124 |