| Conditions | 15 |
| Total Lines | 79 |
| Code Lines | 57 |
| 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.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 -*- |
||
| 74 | def parse_send_xml_tree(gmp, xml_tree): |
||
| 75 | credential_options = [ |
||
| 76 | 'ssh_credential', |
||
| 77 | 'smb_credential', |
||
| 78 | 'esxi_credential', |
||
| 79 | 'snmp_credential', |
||
| 80 | ] |
||
| 81 | counter = 1 |
||
| 82 | |||
| 83 | for target in xml_tree.xpath('target'): |
||
| 84 | keywords = {} # {'make_unique': True} |
||
| 85 | |||
| 86 | keywords['name'] = target.find('name').text |
||
| 87 | |||
| 88 | keywords['hosts'] = target.find('hosts').text.split(',') |
||
| 89 | |||
| 90 | exclude_hosts = target.find('exclude_hosts').text |
||
| 91 | if exclude_hosts is not None: |
||
| 92 | keywords['exclude_hosts'] = exclude_hosts.split(',') |
||
| 93 | |||
| 94 | comment = target.find('comment').text |
||
| 95 | if comment is not None: |
||
| 96 | keywords['comment'] = comment |
||
| 97 | |||
| 98 | credentials = gmp.get_credentials()[0].xpath("//credential/@id") |
||
| 99 | |||
| 100 | for credential in credential_options: |
||
| 101 | cred_id = target.find(credential).xpath('@id')[0] |
||
| 102 | if cred_id == '': |
||
| 103 | continue |
||
| 104 | if cred_id not in credentials: |
||
| 105 | response = yes_or_no( |
||
| 106 | "\nThe credential '{}' for 'target {}' could not be " |
||
| 107 | "located...\nWould you like to continue?".format( |
||
| 108 | credential, counter |
||
| 109 | ) |
||
| 110 | ) |
||
| 111 | |||
| 112 | if response is False: |
||
| 113 | print("Terminating...\n") |
||
| 114 | quit() |
||
| 115 | else: |
||
| 116 | continue |
||
| 117 | |||
| 118 | key = '{}_id'.format(credential) |
||
| 119 | keywords[key] = cred_id |
||
| 120 | elem_path = target.find(credential) |
||
| 121 | port = elem_path.find('port') |
||
| 122 | if port is not None and port.text is not None: |
||
| 123 | port_key = '{}_port'.format(credential) |
||
| 124 | keywords[port_key] = elem_path.find('port').text |
||
| 125 | |||
| 126 | alive_test = get_alive_test_from_string(target.find('alive_tests').text) |
||
| 127 | |||
| 128 | if alive_test is not None: |
||
| 129 | keywords['alive_test'] = alive_test |
||
| 130 | |||
| 131 | reverse_lookup_only = target.find('reverse_lookup_only').text |
||
| 132 | if reverse_lookup_only == '1': |
||
| 133 | keywords['reverse_lookup_only'] = 1 |
||
| 134 | |||
| 135 | reverse_lookup_unify = target.find('reverse_lookup_unify').text |
||
| 136 | if reverse_lookup_unify == '1': |
||
| 137 | keywords['reverse_lookup_unify'] = 1 |
||
| 138 | |||
| 139 | port_range = target.find('port_range') |
||
| 140 | if port_range is not None: |
||
| 141 | keywords['port_range'] = port_range.text |
||
| 142 | |||
| 143 | if target.xpath('port_list/@id') is not None: |
||
| 144 | port_list = {} |
||
| 145 | port_list = target.xpath('port_list/@id')[0] |
||
| 146 | keywords['port_list_id'] = port_list |
||
| 147 | |||
| 148 | print(keywords) |
||
| 149 | |||
| 150 | gmp.create_target(**keywords) |
||
| 151 | |||
| 152 | counter += 1 |
||
| 153 | |||
| 172 |