Conditions | 10 |
Total Lines | 62 |
Code Lines | 39 |
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 db_sync_tool.remote.client.get_jump_host_channel() 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 | #!/usr/bin/env python3 |
||
127 | def get_jump_host_channel(client): |
||
128 | """ |
||
129 | Provide an optional transport channel for a SSH jump host client |
||
130 | https://gist.github.com/tintoy/443c42ea3865680cd624039c4bb46219 |
||
131 | :param client: |
||
132 | :return: |
||
133 | """ |
||
134 | _jump_host_channel = None |
||
135 | if 'jump_host' in system.config[client]: |
||
136 | # prepare jump host config |
||
137 | _jump_host_client = paramiko.SSHClient() |
||
138 | _jump_host_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
||
139 | |||
140 | _jump_host_host = system.config[client]['jump_host']['host'] |
||
141 | _jump_host_user = system.config[client]['jump_host']['user'] if 'user' in system.config[client]['jump_host'] else system.config[client]['user'] |
||
142 | |||
143 | if 'ssh_key' in system.config[client]['jump_host']: |
||
144 | _jump_host_ssh_key = system.config[client]['jump_host']['ssh_key'] |
||
145 | elif 'ssh_key' in system.config[client]: |
||
146 | _jump_host_ssh_key = system.config[client]['ssh_key'] |
||
147 | else: |
||
148 | _jump_host_ssh_key = None |
||
149 | |||
150 | if 'port' in system.config[client]['jump_host']: |
||
151 | _jump_host_port = system.config[client]['jump_host']['port'] |
||
152 | elif 'port' in system.config[client]: |
||
153 | _jump_host_port = system.config[client]['port'] |
||
154 | else: |
||
155 | _jump_host_port = 22 |
||
156 | |||
157 | # connect to the jump host |
||
158 | _jump_host_client.connect( |
||
159 | hostname=_jump_host_host, |
||
160 | username=_jump_host_user, |
||
161 | key_filename=_jump_host_ssh_key, |
||
162 | password=system.config[client]['jump_host']['password'] if 'password' in system.config[client]['jump_host'] else None, |
||
163 | port=_jump_host_port, |
||
164 | compress=True, |
||
165 | timeout=default_timeout |
||
166 | ) |
||
167 | |||
168 | global additional_ssh_clients |
||
169 | additional_ssh_clients.append(_jump_host_client) |
||
170 | |||
171 | # open the necessary channel |
||
172 | _jump_host_transport = _jump_host_client.get_transport() |
||
173 | _jump_host_channel = _jump_host_transport.open_channel( |
||
174 | 'direct-tcpip', |
||
175 | dest_addr=(system.config[client]['host'], 22), |
||
176 | src_addr=(system.config[client]['jump_host']['private'] if 'private' in system.config[client]['jump_host'] else system.config[client]['jump_host']['host'], 22) |
||
177 | ) |
||
178 | |||
179 | # print information |
||
180 | _destination_client = helper.get_ssh_host_name(client, minimal=True) |
||
181 | _jump_host_name = system.config[client]['jump_host']['name'] if 'name' in system.config[client]['jump_host'] else _jump_host_host |
||
182 | output.message( |
||
183 | output.host_to_subject(client), |
||
184 | f'Initialize remote SSH jump host {output.CliFormat.BLACK}local ➔ {output.CliFormat.BOLD}{_jump_host_name}{output.CliFormat.ENDC}{output.CliFormat.BLACK} ➔ {_destination_client}{output.CliFormat.ENDC}', |
||
185 | True |
||
186 | ) |
||
187 | |||
188 | return _jump_host_channel |
||
189 | |||
190 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.