Conditions | 22 |
Total Lines | 133 |
Code Lines | 79 |
Lines | 133 |
Ratio | 100 % |
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.gmpv208.entities.audits.AuditsMixin.create_audit() 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 -*- |
||
52 | View Code Duplication | def create_audit( |
|
|
|||
53 | self, |
||
54 | name: str, |
||
55 | policy_id: str, |
||
56 | target_id: str, |
||
57 | scanner_id: str, |
||
58 | *, |
||
59 | alterable: Optional[bool] = None, |
||
60 | hosts_ordering: Optional[HostsOrdering] = None, |
||
61 | schedule_id: Optional[str] = None, |
||
62 | alert_ids: Optional[List[str]] = None, |
||
63 | comment: Optional[str] = None, |
||
64 | schedule_periods: Optional[int] = None, |
||
65 | observers: Optional[List[str]] = None, |
||
66 | preferences: Optional[dict] = None, |
||
67 | ) -> Any: |
||
68 | """Create a new audit task |
||
69 | |||
70 | Arguments: |
||
71 | name: Name of the new audit |
||
72 | policy_id: UUID of policy to use by the audit |
||
73 | target_id: UUID of target to be scanned |
||
74 | scanner_id: UUID of scanner to use for scanning the target |
||
75 | comment: Comment for the audit |
||
76 | alterable: Whether the task should be alterable |
||
77 | alert_ids: List of UUIDs for alerts to be applied to the audit |
||
78 | hosts_ordering: The order hosts are scanned in |
||
79 | schedule_id: UUID of a schedule when the audit should be run. |
||
80 | schedule_periods: A limit to the number of times the audit will be |
||
81 | scheduled, or 0 for no limit |
||
82 | observers: List of names or ids of users which should be allowed to |
||
83 | observe this audit |
||
84 | preferences: Name/Value pairs of scanner preferences. |
||
85 | |||
86 | Returns: |
||
87 | The response. See :py:meth:`send_command` for details. |
||
88 | """ |
||
89 | |||
90 | if not name: |
||
91 | raise RequiredArgument( |
||
92 | function=self.create_audit.__name__, argument='name' |
||
93 | ) |
||
94 | if not policy_id: |
||
95 | raise RequiredArgument( |
||
96 | function=self.create_audit.__name__, argument='policy_id' |
||
97 | ) |
||
98 | |||
99 | if not target_id: |
||
100 | raise RequiredArgument( |
||
101 | function=self.create_audit.__name__, argument='target_id' |
||
102 | ) |
||
103 | |||
104 | if not scanner_id: |
||
105 | raise RequiredArgument( |
||
106 | function=self.create_audit.__name__, argument='scanner_id' |
||
107 | ) |
||
108 | |||
109 | # don't allow to create a container task with create_task |
||
110 | if target_id == '0': |
||
111 | raise InvalidArgument( |
||
112 | function=self.create_audit.__name__, argument='target_id' |
||
113 | ) |
||
114 | |||
115 | cmd = XmlCommand("create_task") |
||
116 | cmd.add_element("name", name) |
||
117 | cmd.add_element("usage_type", "audit") |
||
118 | cmd.add_element("config", attrs={"id": policy_id}) |
||
119 | cmd.add_element("target", attrs={"id": target_id}) |
||
120 | cmd.add_element("scanner", attrs={"id": scanner_id}) |
||
121 | |||
122 | if comment: |
||
123 | cmd.add_element("comment", comment) |
||
124 | |||
125 | if alterable is not None: |
||
126 | cmd.add_element("alterable", to_bool(alterable)) |
||
127 | |||
128 | if hosts_ordering: |
||
129 | if not isinstance(hosts_ordering, self.types.HostsOrdering): |
||
130 | raise InvalidArgumentType( |
||
131 | function=self.create_audit.__name__, |
||
132 | argument='hosts_ordering', |
||
133 | arg_type=HostsOrdering.__name__, |
||
134 | ) |
||
135 | cmd.add_element("hosts_ordering", hosts_ordering.value) |
||
136 | |||
137 | if alert_ids: |
||
138 | if is_list_like(alert_ids): |
||
139 | # parse all given alert id's |
||
140 | for alert in alert_ids: |
||
141 | cmd.add_element("alert", attrs={"id": str(alert)}) |
||
142 | |||
143 | if schedule_id: |
||
144 | cmd.add_element("schedule", attrs={"id": schedule_id}) |
||
145 | |||
146 | if schedule_periods is not None: |
||
147 | if ( |
||
148 | not isinstance(schedule_periods, Integral) |
||
149 | or schedule_periods < 0 |
||
150 | ): |
||
151 | raise InvalidArgument( |
||
152 | "schedule_periods must be an integer greater or equal " |
||
153 | "than 0" |
||
154 | ) |
||
155 | cmd.add_element("schedule_periods", str(schedule_periods)) |
||
156 | |||
157 | if observers is not None: |
||
158 | if not is_list_like(observers): |
||
159 | raise InvalidArgumentType( |
||
160 | function=self.create_audit.__name__, |
||
161 | argument='observers', |
||
162 | arg_type='list', |
||
163 | ) |
||
164 | |||
165 | # gvmd splits by comma and space |
||
166 | # gvmd tries to lookup each value as user name and afterwards as |
||
167 | # user id. So both user name and user id are possible |
||
168 | cmd.add_element("observers", to_comma_list(observers)) |
||
169 | |||
170 | if preferences is not None: |
||
171 | if not isinstance(preferences, Mapping): |
||
172 | raise InvalidArgumentType( |
||
173 | function=self.create_audit.__name__, |
||
174 | argument='preferences', |
||
175 | arg_type=Mapping.__name__, |
||
176 | ) |
||
177 | |||
178 | _xmlprefs = cmd.add_element("preferences") |
||
179 | for pref_name, pref_value in preferences.items(): |
||
180 | _xmlpref = _xmlprefs.add_element("preference") |
||
181 | _xmlpref.add_element("scanner_name", pref_name) |
||
182 | _xmlpref.add_element("value", str(pref_value)) |
||
183 | |||
184 | return self._send_xml_command(cmd) |
||
185 | |||
449 |