Conditions | 12 |
Total Lines | 86 |
Code Lines | 60 |
Lines | 13 |
Ratio | 15.12 % |
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 -*- |
||
71 | def main(): |
||
72 | parser = create_parser( |
||
73 | description=HELP_TEXT, logfilename='gvm-pyshell.log') |
||
74 | |||
75 | parser.add_protocol_argument() |
||
76 | |||
77 | parser.add_argument( |
||
78 | '-i', '--interactive', action='store_true', default=False, |
||
79 | help='Start an interactive Python shell') |
||
80 | |||
81 | parser.add_argument( |
||
82 | 'scriptname', nargs='?', metavar="SCRIPT", |
||
83 | help='Path to script to be preloaded (example: myscript.gmp)') |
||
84 | parser.add_argument( |
||
85 | 'scriptargs', nargs='*', metavar="ARG", |
||
86 | help='Arguments for preloaded script') |
||
87 | |||
88 | args = parser.parse_args() |
||
89 | |||
90 | if 'socket' in args.connection_type and args.sockpath: |
||
91 | print('The --sockpath parameter has been deprecated. Please use ' |
||
92 | '--socketpath instead', file=sys.stderr) |
||
93 | |||
94 | connection = create_connection(**vars(args)) |
||
95 | |||
96 | transform = EtreeCheckCommandTransform() |
||
97 | |||
98 | global_vars = { |
||
99 | 'help': Help(), |
||
100 | '__version__': __version__, |
||
101 | '__api_version__': __api_version__, |
||
102 | } |
||
103 | |||
104 | username = None |
||
105 | password = None |
||
106 | |||
107 | View Code Duplication | if args.protocol == PROTOCOL_OSP: |
|
|
|||
108 | protocol = Osp(connection, transform=transform) |
||
109 | global_vars['osp'] = protocol |
||
110 | global_vars['__name__'] = '__osp__' |
||
111 | else: |
||
112 | protocol = Gmp(connection, transform=transform) |
||
113 | global_vars['gmp'] = protocol |
||
114 | global_vars['__name__'] = '__gmp__' |
||
115 | |||
116 | if args.gmp_username: |
||
117 | (username, password) = authenticate( |
||
118 | protocol, username=args.gmp_username, |
||
119 | password=args.gmp_password) |
||
120 | |||
121 | shell_args = Namespace( |
||
122 | username=username, password=password) |
||
123 | |||
124 | global_vars['args'] = shell_args |
||
125 | |||
126 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
127 | |||
128 | if with_script: |
||
129 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
130 | shell_args.argv = argv |
||
131 | # for backwards compatibility we add script here |
||
132 | shell_args.script = argv |
||
133 | |||
134 | no_script_no_interactive = not args.interactive and not with_script |
||
135 | script_and_interactive = args.interactive and with_script |
||
136 | only_interactive = not with_script and args.interactive |
||
137 | only_script = not args.interactive and with_script |
||
138 | |||
139 | if only_interactive or no_script_no_interactive: |
||
140 | enter_interactive_mode(global_vars) |
||
141 | |||
142 | if script_and_interactive or only_script: |
||
143 | if only_script: |
||
144 | print( |
||
145 | 'Using gvm-pyshell for running scripts only is deprecated. ' |
||
146 | 'Please use gvm-script instead', |
||
147 | file=sys.stderr, |
||
148 | ) |
||
149 | |||
150 | script_name = args.scriptname |
||
151 | run_script(script_name, global_vars) |
||
152 | |||
153 | if not only_script: |
||
154 | enter_interactive_mode(global_vars) |
||
155 | |||
156 | protocol.disconnect() |
||
157 | |||
168 |