| Conditions | 30 |
| Total Lines | 120 |
| Code Lines | 67 |
| Lines | 35 |
| Ratio | 29.17 % |
| 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 gvm.protocols.gmpv8.Gmp.create_credential() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | # -*- coding: utf-8 -*- |
||
| 59 | def create_credential(self, name, credential_type, *, comment=None, |
||
|
|
|||
| 60 | allow_insecure=False, certificate=None, |
||
| 61 | key_phrase=None, private_key=None, |
||
| 62 | login=None, password=None, auth_algorithm=None, |
||
| 63 | community=None, privacy_algorithm=None, |
||
| 64 | privacy_password=None, public_key=None): |
||
| 65 | """Create a new credential |
||
| 66 | |||
| 67 | Arguments: |
||
| 68 | name (str): Name of the new credential |
||
| 69 | credential_type (str): The credential type. One of 'cc', 'snmp', |
||
| 70 | 'up', 'usk', 'smime', 'pgp' |
||
| 71 | comment (str, optional): Comment for the credential |
||
| 72 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 73 | the credential |
||
| 74 | certificate (str, optional): Certificate for the credential |
||
| 75 | key_phrase (str, optional): Key passphrase for the private key |
||
| 76 | private_key (str, optional): Private key to use for login |
||
| 77 | login (str, optional): Username for the credential |
||
| 78 | password (str, optional): Password for the credential |
||
| 79 | community (str, optional): The SNMP community |
||
| 80 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 81 | either aes or des. |
||
| 82 | privacy_password (str, optional): The SNMP privacy password |
||
| 83 | public_key: (str, optional): PGP public key in *armor* plain text |
||
| 84 | format |
||
| 85 | |||
| 86 | Returns: |
||
| 87 | The response. See :py:meth:`send_command` for details. |
||
| 88 | """ |
||
| 89 | if not name: |
||
| 90 | raise RequiredArgument('create_credential requires name argument') |
||
| 91 | |||
| 92 | if credential_type not in CREDENTIAL_TYPES: |
||
| 93 | raise InvalidArgument( |
||
| 94 | 'create_credential requires type to be either cc, snmp, up,' |
||
| 95 | 'smime, pgp or usk') |
||
| 96 | |||
| 97 | cmd = XmlCommand('create_credential') |
||
| 98 | cmd.add_element('name', name) |
||
| 99 | |||
| 100 | cmd.add_element('type', credential_type) |
||
| 101 | |||
| 102 | if comment: |
||
| 103 | cmd.add_element('comment', comment) |
||
| 104 | |||
| 105 | if allow_insecure: |
||
| 106 | cmd.add_element('allow_insecure', '1') |
||
| 107 | |||
| 108 | |||
| 109 | if credential_type == 'cc' or credential_type == 'smime': |
||
| 110 | if not certificate: |
||
| 111 | raise RequiredArgument( |
||
| 112 | 'create_credential requires certificate argument for ' |
||
| 113 | 'credential_type {0}'.format(credential_type)) |
||
| 114 | |||
| 115 | cmd.add_element('certificate', certificate) |
||
| 116 | |||
| 117 | if (credential_type == 'up' or credential_type == 'usk' or \ |
||
| 118 | credential_type == 'snmp'): |
||
| 119 | if not login: |
||
| 120 | raise RequiredArgument( |
||
| 121 | 'create_credential requires login argument for ' |
||
| 122 | 'credential_type {0}'.format(credential_type)) |
||
| 123 | |||
| 124 | cmd.add_element('login', login) |
||
| 125 | |||
| 126 | if (credential_type == 'up' or credential_type == 'snmp') and password: |
||
| 127 | cmd.add_element('password', password) |
||
| 128 | |||
| 129 | View Code Duplication | if credential_type == 'usk': |
|
| 130 | if not private_key: |
||
| 131 | raise RequiredArgument( |
||
| 132 | 'create_credential requires certificate argument for ' |
||
| 133 | 'credential_type usk') |
||
| 134 | |||
| 135 | _xmlkey = cmd.add_element('key') |
||
| 136 | _xmlkey.add_element('private', private_key) |
||
| 137 | |||
| 138 | if key_phrase: |
||
| 139 | _xmlkey.add_element('phrase', key_phrase) |
||
| 140 | |||
| 141 | if credential_type == 'cc' and private_key: |
||
| 142 | _xmlkey = cmd.add_element('key') |
||
| 143 | _xmlkey.add_element('private', private_key) |
||
| 144 | |||
| 145 | View Code Duplication | if credential_type == 'snmp': |
|
| 146 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 147 | raise InvalidArgument( |
||
| 148 | 'create_credential requires auth_algorithm to be either ' |
||
| 149 | 'md5 or sha1') |
||
| 150 | |||
| 151 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 152 | |||
| 153 | if community: |
||
| 154 | cmd.add_element('community', community) |
||
| 155 | |||
| 156 | if privacy_algorithm is not None or privacy_password: |
||
| 157 | _xmlprivacy = cmd.add_element('privacy') |
||
| 158 | |||
| 159 | if privacy_algorithm is not None: |
||
| 160 | if privacy_algorithm not in ('aes', 'des'): |
||
| 161 | raise InvalidArgument( |
||
| 162 | 'create_credential requires algorithm to be either ' |
||
| 163 | 'aes or des') |
||
| 164 | |||
| 165 | _xmlprivacy.add_element('algorithm', privacy_algorithm) |
||
| 166 | |||
| 167 | if privacy_password: |
||
| 168 | _xmlprivacy.add_element('password', privacy_password) |
||
| 169 | |||
| 170 | if credential_type == 'pgp': |
||
| 171 | if not public_key: |
||
| 172 | raise RequiredArgument( |
||
| 173 | 'Creating a pgp credential requires a public_key argument') |
||
| 174 | |||
| 175 | _xmlkey = cmd.add_element('key') |
||
| 176 | _xmlkey.add_element('public', public_key) |
||
| 177 | |||
| 178 | return self._send_xml_command(cmd) |
||
| 179 | |||
| 277 |