| Conditions | 14 |
| Total Lines | 81 |
| Code Lines | 58 |
| 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 send-targets.gmp.parse_send_xml_tree() 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 -*- |
||
| 92 | def parse_send_xml_tree(gmp, xml_tree): |
||
| 93 | credential_options = [ |
||
| 94 | 'ssh_credential', |
||
| 95 | 'smb_credential', |
||
| 96 | 'esxi_credential', |
||
| 97 | 'snmp_credential', |
||
| 98 | ] |
||
| 99 | counter = 1 |
||
| 100 | |||
| 101 | for target in xml_tree.xpath('target'): |
||
| 102 | keywords = {} # {'make_unique': True} |
||
| 103 | |||
| 104 | keywords['name'] = target.find('name').text |
||
| 105 | |||
| 106 | keywords['hosts'] = target.find('hosts').text.split(',') |
||
| 107 | |||
| 108 | exclude_hosts = target.find('exclude_hosts').text |
||
| 109 | if exclude_hosts is not None: |
||
| 110 | keywords['exclude_hosts'] = exclude_hosts.split(',') |
||
| 111 | |||
| 112 | comment = target.find('comment').text |
||
| 113 | if comment is not None: |
||
| 114 | keywords['comment'] = comment |
||
| 115 | |||
| 116 | credentials = gmp.get_credentials()[0].xpath("//credential/@id") |
||
| 117 | |||
| 118 | for credential in credential_options: |
||
| 119 | cred_id = target.find(credential).xpath('@id')[0] |
||
| 120 | if cred_id == '': |
||
| 121 | continue |
||
| 122 | if cred_id not in credentials: |
||
| 123 | response = yes_or_no( |
||
| 124 | "\nThe credential '{}' for 'target {}' could not be " |
||
| 125 | "located...\nWould you like to continue?".format( |
||
| 126 | credential, counter |
||
| 127 | ) |
||
| 128 | ) |
||
| 129 | |||
| 130 | if response is False: |
||
| 131 | print("Terminating...\n") |
||
| 132 | quit() |
||
| 133 | else: |
||
| 134 | continue |
||
| 135 | |||
| 136 | temp_dict = {} |
||
| 137 | temp_dict['id'] = cred_id |
||
| 138 | elem_path = target.find(credential) |
||
| 139 | if elem_path.find('port').text is not None: |
||
| 140 | temp_dict['port'] = elem_path.find('port').text |
||
| 141 | |||
| 142 | keywords[credential] = temp_dict |
||
| 143 | |||
| 144 | alive_test = alive_test_convert(target.find('alive_tests').text) |
||
| 145 | print(alive_test) |
||
| 146 | |||
| 147 | if alive_test is not None: |
||
| 148 | keywords['alive_test'] = alive_test |
||
| 149 | print(keywords['alive_test']) |
||
| 150 | |||
| 151 | reverse_lookup_only = target.find('reverse_lookup_only').text |
||
| 152 | if reverse_lookup_only == '1': |
||
| 153 | keywords['reverse_lookup_only'] = 1 |
||
| 154 | |||
| 155 | reverse_lookup_unify = target.find('reverse_lookup_unify').text |
||
| 156 | if reverse_lookup_unify == '1': |
||
| 157 | keywords['reverse_lookup_unify'] = 1 |
||
| 158 | |||
| 159 | port_range = target.find('port_range') |
||
| 160 | if port_range is not None: |
||
| 161 | keywords['port_range'] = port_range.text |
||
| 162 | |||
| 163 | if target.xpath('port_list/@id') is not None: |
||
| 164 | port_list = {} |
||
| 165 | port_list = target.xpath('port_list/@id')[0] |
||
| 166 | keywords['port_list_id'] = port_list |
||
| 167 | |||
| 168 | print(keywords) |
||
| 169 | |||
| 170 | gmp.create_target(**keywords) |
||
| 171 | |||
| 172 | counter += 1 |
||
| 173 | |||
| 192 |