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