22#
33# SPDX-License-Identifier: GPL-3.0-or-later
44
5- from typing import Any , Mapping , Optional
5+ from typing import Any , Mapping , Optional , Sequence
66
77from gvm .errors import RequiredArgument
88from gvm .protocols .core import Request
99from gvm .protocols .gmp .requests ._entity_id import EntityID
10- from gvm .utils import SupportsStr , to_bool
10+ from gvm .utils import to_bool
1111from gvm .xml import XmlCommand
1212
1313
1414class Agents :
1515
1616 @staticmethod
17- def _add_element (element , name : str , value : Optional [ SupportsStr ] ) -> None :
17+ def _add_element (element , name : str , value : Any ) -> None :
1818 """
1919 Helper to add a sub-element with a value if the value is not None.
2020
@@ -31,52 +31,56 @@ def _add_element(element, name: str, value: Optional[SupportsStr]) -> None:
3131 def _validate_agent_config (
3232 cls , config : Mapping [str , Any ], * , caller : str
3333 ) -> None :
34- """Ensure all required fields exist and are non-empty."""
35-
36- def valid (d : Mapping [str , Any ], key : str , path : str ):
37- if (
38- not isinstance (d , Mapping )
39- or d .get (key ) is None
40- or d .get (key ) == ""
41- ):
34+ """Ensure all required fields exist, are well-shaped, and non-empty."""
35+
36+ def valid_map (d : Any , key : str , path : str ) -> Mapping [str , Any ]:
37+ if not isinstance (d , Mapping ):
38+ raise RequiredArgument (
39+ function = caller , argument = f"config.{ path .rstrip ('.' )} "
40+ )
41+ v = d .get (key )
42+ if not isinstance (v , Mapping ):
43+ raise RequiredArgument (
44+ function = caller , argument = f"config.{ path } { key } "
45+ )
46+ return v
47+
48+ def valid_value (d : Mapping [str , Any ], key : str , path : str ) -> Any :
49+ v = d .get (key )
50+ if v is None or (isinstance (v , str ) and v .strip () == "" ):
4251 raise RequiredArgument (
4352 function = caller , argument = f"config.{ path } { key } "
4453 )
54+ return v
4555
4656 # agent_control.retry
47- ac = config .get ("agent_control" )
48- valid (config , "agent_control" , "" )
49- retry = ac .get ("retry" ) if isinstance (ac , Mapping ) else None
50- valid (ac , "retry" , "agent_control." )
51- valid (retry , "attempts" , "agent_control.retry." )
52- valid (retry , "delay_in_seconds" , "agent_control.retry." )
53- valid (retry , "max_jitter_in_seconds" , "agent_control.retry." )
57+ ac = valid_map (config , "agent_control" , "" )
58+ retry = valid_map (ac , "retry" , "agent_control." )
59+ valid_value (retry , "attempts" , "agent_control.retry." )
60+ valid_value (retry , "delay_in_seconds" , "agent_control.retry." )
61+ valid_value (retry , "max_jitter_in_seconds" , "agent_control.retry." )
5462
5563 # agent_script_executor
56- se = config .get ("agent_script_executor" )
57- valid (config , "agent_script_executor" , "" )
58- valid (se , "bulk_size" , "agent_script_executor." )
59- valid (se , "bulk_throttle_time_in_ms" , "agent_script_executor." )
60- valid (se , "indexer_dir_depth" , "agent_script_executor." )
61-
62- sched = (
63- se .get ("scheduler_cron_time" ) if isinstance (se , Mapping ) else None
64- )
65- if isinstance (sched , (list , tuple , set )):
66- items = list (sched )
64+ se = valid_map (config , "agent_script_executor" , "" )
65+ valid_value (se , "bulk_size" , "agent_script_executor." )
66+ valid_value (se , "bulk_throttle_time_in_ms" , "agent_script_executor." )
67+ valid_value (se , "indexer_dir_depth" , "agent_script_executor." )
68+
69+ sched = se .get ("scheduler_cron_time" )
70+ if isinstance (sched , Sequence ) and not isinstance (sched , (str , bytes )):
71+ items = [str (x ) for x in sched ]
6772 else :
6873 items = []
69- if not items or any (str (x ).strip () == "" for x in items ):
74+ if not items or any (not str (x ).strip () for x in items ):
7075 raise RequiredArgument (
7176 function = caller ,
7277 argument = "config.agent_script_executor.scheduler_cron_time" ,
7378 )
7479
7580 # heartbeat
76- hb = config .get ("heartbeat" )
77- valid (config , "heartbeat" , "" )
78- valid (hb , "interval_in_seconds" , "heartbeat." )
79- valid (hb , "miss_until_inactive" , "heartbeat." )
81+ hb = valid_map (config , "heartbeat" , "" )
82+ valid_value (hb , "interval_in_seconds" , "heartbeat." )
83+ valid_value (hb , "miss_until_inactive" , "heartbeat." )
8084
8185 @classmethod
8286 def _append_agent_config (cls , parent , config : Mapping [str , Any ]) -> None :
0 commit comments