| Conditions | 11 |
| Total Lines | 79 |
| Code Lines | 56 |
| 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 -*- |
||
| 94 | def main(): |
||
| 95 | parser = create_parser( |
||
| 96 | description=HELP_TEXT, logfilename='gvm-pyshell.log') |
||
| 97 | |||
| 98 | parser.add_protocol_argument() |
||
| 99 | |||
| 100 | parser.add_argument( |
||
| 101 | '-i', '--interactive', action='store_true', default=False, |
||
| 102 | help='Start an interactive Python shell') |
||
| 103 | |||
| 104 | parser.add_argument( |
||
| 105 | 'scriptname', nargs='?', metavar="SCRIPT", |
||
| 106 | help='Path to script to be preloaded (example: myscript.gmp)') |
||
| 107 | parser.add_argument( |
||
| 108 | 'scriptargs', nargs='*', metavar="ARG", |
||
| 109 | help='Arguments for preloaded script') |
||
| 110 | |||
| 111 | args = parser.parse_args() |
||
| 112 | |||
| 113 | if 'socket' in args.connection_type and args.sockpath: |
||
| 114 | print('The --sockpath parameter has been deprecated. Please use ' |
||
| 115 | '--socketpath instead', file=sys.stderr) |
||
| 116 | |||
| 117 | connection = create_connection(**vars(args)) |
||
| 118 | |||
| 119 | transform = EtreeCheckCommandTransform() |
||
| 120 | |||
| 121 | global_vars = { |
||
| 122 | 'help': Help(), |
||
| 123 | '__version__': __version__, |
||
| 124 | '__api_version__': __api_version__, |
||
| 125 | } |
||
| 126 | |||
| 127 | username = None |
||
| 128 | password = None |
||
| 129 | |||
| 130 | if args.protocol == PROTOCOL_OSP: |
||
| 131 | protocol = Osp(connection, transform=transform) |
||
| 132 | global_vars['osp'] = protocol |
||
| 133 | global_vars['__name__'] = '__osp__' |
||
| 134 | else: |
||
| 135 | protocol = Gmp(connection, transform=transform) |
||
| 136 | global_vars['gmp'] = protocol |
||
| 137 | global_vars['__name__'] = '__gmp__' |
||
| 138 | |||
| 139 | if args.gmp_username: |
||
| 140 | (username, password) = authenticate( |
||
| 141 | protocol, username=args.gmp_username, |
||
| 142 | password=args.gmp_password) |
||
| 143 | |||
| 144 | shell_args = Arguments( |
||
| 145 | username=username, password=password) |
||
| 146 | |||
| 147 | global_vars['args'] = shell_args |
||
| 148 | |||
| 149 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
| 150 | |||
| 151 | if with_script: |
||
| 152 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
| 153 | shell_args.argv = argv |
||
| 154 | # for backwards compatibility we add script here |
||
| 155 | shell_args.script = argv |
||
| 156 | |||
| 157 | no_script_no_interactive = not args.interactive and not with_script |
||
| 158 | script_and_interactive = args.interactive and with_script |
||
| 159 | only_interactive = not with_script and args.interactive |
||
| 160 | only_script = not args.interactive and with_script |
||
| 161 | |||
| 162 | if only_interactive or no_script_no_interactive: |
||
| 163 | enter_interactive_mode(global_vars) |
||
| 164 | |||
| 165 | if script_and_interactive or only_script: |
||
| 166 | script_name = args.scriptname |
||
| 167 | load(script_name, global_vars) |
||
| 168 | |||
| 169 | if not only_script: |
||
| 170 | enter_interactive_mode(global_vars) |
||
| 171 | |||
| 172 | protocol.disconnect() |
||
| 173 | |||
| 201 |