Conditions | 14 |
Total Lines | 102 |
Code Lines | 69 |
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:
Complex classes like gvmtools.pyshell.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
77 | def main(): |
||
78 | do_not_run_as_root() |
||
79 | |||
80 | parser = create_parser(description=HELP_TEXT, logfilename='gvm-pyshell.log') |
||
81 | |||
82 | parser.add_protocol_argument() |
||
83 | |||
84 | parser.add_argument( |
||
85 | '-i', |
||
86 | '--interactive', |
||
87 | action='store_true', |
||
88 | default=False, |
||
89 | help='Start an interactive Python shell', |
||
90 | ) |
||
91 | |||
92 | parser.add_argument( |
||
93 | 'scriptname', |
||
94 | nargs='?', |
||
95 | metavar="SCRIPT", |
||
96 | help='Path to script to be preloaded (example: myscript.gmp)', |
||
97 | ) |
||
98 | parser.add_argument( |
||
99 | 'scriptargs', |
||
100 | nargs='*', |
||
101 | metavar="ARG", |
||
102 | help='Arguments for preloaded script', |
||
103 | ) |
||
104 | |||
105 | args = parser.parse_args() |
||
106 | |||
107 | if 'socket' in args.connection_type and args.sockpath: |
||
108 | print( |
||
109 | 'The --sockpath parameter has been deprecated. Please use ' |
||
110 | '--socketpath instead', |
||
111 | file=sys.stderr, |
||
112 | ) |
||
113 | |||
114 | connection = create_connection(**vars(args)) |
||
115 | |||
116 | transform = EtreeCheckCommandTransform() |
||
117 | |||
118 | global_vars = { |
||
119 | 'help': Help(), |
||
120 | '__version__': __version__, |
||
121 | '__api_version__': __api_version__, |
||
122 | } |
||
123 | |||
124 | username = None |
||
125 | password = None |
||
126 | |||
127 | if args.protocol == PROTOCOL_OSP: |
||
128 | protocol_class = Osp |
||
129 | name = 'osp' |
||
130 | else: |
||
131 | protocol_class = Gmp |
||
132 | name = 'gmp' |
||
133 | |||
134 | with protocol_class(connection, transform=transform) as protocol: |
||
135 | global_vars[name] = protocol |
||
136 | global_vars['__name__'] = '__{}__'.format(name) |
||
137 | |||
138 | if args.protocol == PROTOCOL_GMP: |
||
139 | if args.gmp_username: |
||
140 | (username, password) = authenticate( |
||
141 | protocol, |
||
142 | username=args.gmp_username, |
||
143 | password=args.gmp_password, |
||
144 | ) |
||
145 | |||
146 | shell_args = Namespace(username=username, password=password) |
||
147 | |||
148 | global_vars['args'] = shell_args |
||
149 | |||
150 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
151 | |||
152 | if with_script: |
||
153 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
154 | shell_args.argv = argv |
||
155 | # for backwards compatibility we add script here |
||
156 | shell_args.script = argv |
||
157 | |||
158 | no_script_no_interactive = not args.interactive and not with_script |
||
159 | script_and_interactive = args.interactive and with_script |
||
160 | only_interactive = not with_script and args.interactive |
||
161 | only_script = not args.interactive and with_script |
||
162 | |||
163 | if only_interactive or no_script_no_interactive: |
||
164 | enter_interactive_mode(global_vars) |
||
165 | |||
166 | if script_and_interactive or only_script: |
||
167 | if only_script: |
||
168 | print( |
||
169 | 'Using gvm-pyshell for running scripts only is deprecated. ' |
||
170 | 'Please use gvm-script instead', |
||
171 | file=sys.stderr, |
||
172 | ) |
||
173 | |||
174 | script_name = args.scriptname |
||
175 | run_script(script_name, global_vars) |
||
176 | |||
177 | if not only_script: |
||
178 | enter_interactive_mode(global_vars) |
||
179 | |||
191 |