| Conditions | 6 |
| Total Lines | 63 |
| 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:
| 1 | # -*- coding: utf-8 -*- |
||
| 38 | def get_config(gmp, nvt_oid): |
||
| 39 | # Choose from existing config, which to copy or create new config |
||
| 40 | res = gmp.get_configs() |
||
| 41 | |||
| 42 | config_ids = res.xpath('config/@id') |
||
| 43 | |||
| 44 | for i, conf in enumerate(res.xpath('config')): |
||
| 45 | config_id = conf.xpath('@id')[0] |
||
| 46 | name = conf.xpath('name/text()')[0] |
||
| 47 | print('\n({0}) {1}: ({2})'.format(i, name, config_id)) |
||
| 48 | |||
| 49 | while True: |
||
| 50 | chosen_config = input( |
||
| 51 | '\nChoose your config or create new one[0-{len} | n]: '.format( |
||
| 52 | len=len(config_ids) - 1 |
||
| 53 | ) |
||
| 54 | ) |
||
| 55 | |||
| 56 | if chosen_config == 'n': |
||
| 57 | chosen_copy_config = int( |
||
| 58 | input( |
||
| 59 | 'Which config to copy? [0-{len}]: '.format( |
||
| 60 | len=len(config_ids) - 1 |
||
| 61 | ) |
||
| 62 | ) |
||
| 63 | ) |
||
| 64 | config_name = input('Enter new Name for config: ') |
||
| 65 | |||
| 66 | copy_id = config_ids[chosen_copy_config] |
||
| 67 | |||
| 68 | res = gmp.clone_config(copy_id) |
||
| 69 | |||
| 70 | config_id = res.xpath('@id')[0] |
||
| 71 | |||
| 72 | # Modify the config with an nvt oid |
||
| 73 | if len(nvt_oid) is 0: |
||
| 74 | nvt_oid = input('NVT OID: ') |
||
| 75 | |||
| 76 | nvt = gmp.get_nvt(nvt_oid=nvt_oid) |
||
| 77 | family = nvt.xpath('nvt/family/text()')[0] |
||
| 78 | |||
| 79 | gmp.modify_config( |
||
| 80 | config_id, |
||
| 81 | 'nvt_selection', |
||
| 82 | name=config_name, |
||
| 83 | nvt_oids=[nvt_oid], |
||
| 84 | family=family, |
||
| 85 | ) |
||
| 86 | |||
| 87 | # This nvts must be present to work |
||
| 88 | family = 'Port scanners' |
||
| 89 | nvts = [ |
||
| 90 | '1.3.6.1.4.1.25623.1.0.14259', |
||
| 91 | '1.3.6.1.4.1.25623.1.0.100315', |
||
| 92 | ] |
||
| 93 | |||
| 94 | gmp.modify_config( |
||
| 95 | config_id, 'nvt_selection', nvt_oids=nvts, family=family |
||
| 96 | ) |
||
| 97 | return config_id |
||
| 98 | |||
| 99 | if 0 <= int(chosen_config) < len(config_ids): |
||
| 100 | return config_ids[int(chosen_config)] |
||
| 101 | |||
| 192 |