| Conditions | 12 |
| Total Lines | 95 |
| Code Lines | 65 |
| 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 | connection = create_connection(**vars(args)) |
||
| 108 | |||
| 109 | transform = EtreeCheckCommandTransform() |
||
| 110 | |||
| 111 | global_vars = { |
||
| 112 | 'help': Help(), |
||
| 113 | '__version__': __version__, |
||
| 114 | '__api_version__': __api_version__, |
||
| 115 | } |
||
| 116 | |||
| 117 | username = None |
||
| 118 | password = None |
||
| 119 | |||
| 120 | if args.protocol == PROTOCOL_OSP: |
||
| 121 | protocol_class = Osp |
||
| 122 | name = 'osp' |
||
| 123 | else: |
||
| 124 | protocol_class = Gmp |
||
| 125 | name = 'gmp' |
||
| 126 | |||
| 127 | with protocol_class(connection, transform=transform) as protocol: |
||
| 128 | global_vars[name] = protocol |
||
| 129 | global_vars['__name__'] = '__{}__'.format(name) |
||
| 130 | |||
| 131 | if args.protocol == PROTOCOL_GMP: |
||
| 132 | if args.gmp_username: |
||
| 133 | (username, password) = authenticate( |
||
| 134 | protocol, |
||
| 135 | username=args.gmp_username, |
||
| 136 | password=args.gmp_password, |
||
| 137 | ) |
||
| 138 | |||
| 139 | shell_args = Namespace(username=username, password=password) |
||
| 140 | |||
| 141 | global_vars['args'] = shell_args |
||
| 142 | |||
| 143 | with_script = args.scriptname and len(args.scriptname) > 0 |
||
| 144 | |||
| 145 | if with_script: |
||
| 146 | argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
||
| 147 | shell_args.argv = argv |
||
| 148 | # for backwards compatibility we add script here |
||
| 149 | shell_args.script = argv |
||
| 150 | |||
| 151 | no_script_no_interactive = not args.interactive and not with_script |
||
| 152 | script_and_interactive = args.interactive and with_script |
||
| 153 | only_interactive = not with_script and args.interactive |
||
| 154 | only_script = not args.interactive and with_script |
||
| 155 | |||
| 156 | if only_interactive or no_script_no_interactive: |
||
| 157 | enter_interactive_mode(global_vars) |
||
| 158 | |||
| 159 | if script_and_interactive or only_script: |
||
| 160 | if only_script: |
||
| 161 | print( |
||
| 162 | 'Using gvm-pyshell for running scripts only is deprecated. ' |
||
| 163 | 'Please use gvm-script instead', |
||
| 164 | file=sys.stderr, |
||
| 165 | ) |
||
| 166 | |||
| 167 | script_name = args.scriptname |
||
| 168 | run_script(script_name, global_vars) |
||
| 169 | |||
| 170 | if not only_script: |
||
| 171 | enter_interactive_mode(global_vars) |
||
| 172 | |||
| 184 |