Conditions | 12 |
Total Lines | 96 |
Code Lines | 66 |
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 -*- |
||
79 | def main(): |
||
80 | do_not_run_as_root() |
||
81 | |||
82 | parser = create_parser(description=HELP_TEXT, logfilename='gvm-pyshell.log') |
||
83 | |||
84 | parser.add_protocol_argument() |
||
85 | |||
86 | parser.add_argument( |
||
87 | '-i', |
||
88 | '--interactive', |
||
89 | action='store_true', |
||
90 | default=False, |
||
91 | help='Start an interactive Python shell', |
||
92 | ) |
||
93 | |||
94 | parser.add_argument( |
||
95 | 'scriptname', |
||
96 | nargs='?', |
||
97 | metavar="SCRIPT", |
||
98 | help='Path to script to be preloaded (example: myscript.gmp.py)', |
||
99 | ) |
||
100 | parser.add_argument( |
||
101 | 'scriptargs', |
||
102 | nargs='*', |
||
103 | metavar="ARG", |
||
104 | help='Arguments for preloaded script', |
||
105 | ) |
||
106 | |||
107 | args = parser.parse_args() |
||
108 | |||
109 | connection = create_connection(**vars(args)) |
||
110 | |||
111 | transform = EtreeCheckCommandTransform() |
||
112 | |||
113 | global_vars = { |
||
114 | 'help': Help(), |
||
115 | 'pretty_print': pretty_print, |
||
116 | '__version__': __version__, |
||
117 | '__api_version__': __api_version__, |
||
118 | } |
||
119 | |||
120 | username = None |
||
121 | password = None |
||
122 | |||
123 | if args.protocol == PROTOCOL_OSP: |
||
124 | protocol_class = Osp |
||
125 | name = 'osp' |
||
126 | else: |
||
127 | protocol_class = Gmp |
||
128 | name = 'gmp' |
||
129 | |||
130 | with protocol_class(connection, transform=transform) as protocol: |
||
131 | global_vars[name] = protocol |
||
132 | global_vars['__name__'] = '__{}__'.format(name) |
||
133 | |||
134 | if args.protocol == PROTOCOL_GMP: |
||
135 | if args.gmp_username: |
||
136 | (username, password) = authenticate( |
||
137 | protocol, |
||
138 | username=args.gmp_username, |
||
139 | password=args.gmp_password, |
||
140 | ) |
||
141 | |||
142 | shell_args = Namespace(username=username, password=password) |
||
143 | |||
144 | global_vars['args'] = shell_args |
||
145 | |||
146 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
147 | |||
148 | if with_script: |
||
149 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
150 | shell_args.argv = argv |
||
151 | # for backwards compatibility we add script here |
||
152 | shell_args.script = argv |
||
153 | |||
154 | no_script_no_interactive = not args.interactive and not with_script |
||
155 | script_and_interactive = args.interactive and with_script |
||
156 | only_interactive = not with_script and args.interactive |
||
157 | only_script = not args.interactive and with_script |
||
158 | |||
159 | if only_interactive or no_script_no_interactive: |
||
160 | enter_interactive_mode(global_vars) |
||
161 | |||
162 | if script_and_interactive or only_script: |
||
163 | if only_script: |
||
164 | print( |
||
165 | 'Using gvm-pyshell for running scripts only is deprecated. ' |
||
166 | 'Please use gvm-script instead', |
||
167 | file=sys.stderr, |
||
168 | ) |
||
169 | |||
170 | script_name = args.scriptname |
||
171 | run_script(script_name, global_vars) |
||
172 | |||
173 | if not only_script: |
||
174 | enter_interactive_mode(global_vars) |
||
175 | |||
187 |