6464]
6565
6666
67- def _probe_config_file (* cf , default = None ):
68- # type: (str, Optional[str] ) -> Union[str, None]
67+ def _probe_config_file (* cf ):
68+ # type: (str) -> Union[str, None]
6969 path = pathlib .Path (os .path .expanduser ("~" ))
7070 if not path .exists ():
7171 # ~ folder doesn't exist. Unsalvageable
7272 return None
73- cf_path = path .joinpath (* cf )
74- if not cf_path .exists ():
75- if default is not None :
76- # We have a default ! set it
77- cf_path .parent .mkdir (parents = True , exist_ok = True )
78- with cf_path .open ("w" ) as fd :
79- fd .write (default )
80- return str (cf_path .resolve ())
81- return None
82- return str (cf_path .resolve ())
73+ return str (path .joinpath (* cf ).resolve ())
8374
8475
8576def _read_config_file (cf , _globals = globals (), _locals = locals (),
86- interactive = True ):
87- # type: (str, Dict[str, Any], Dict[str, Any], bool) -> None
77+ interactive = True , default = None ):
78+ # type: (str, Dict[str, Any], Dict[str, Any], bool, Optional[str] ) -> None
8879 """Read a config file: execute a python file while loading scapy, that
8980 may contain some pre-configured values.
9081
@@ -93,11 +84,13 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
9384 function. Otherwise, vars are only available from inside the scapy
9485 console.
9586
96- params:
97- - _globals: the globals() vars
98- - _locals: the locals() vars
99- - interactive: specified whether or not errors should be printed
87+ Parameters:
88+
89+ :param _globals: the globals() vars
90+ :param _locals: the locals() vars
91+ :param interactive: specified whether or not errors should be printed
10092 using the scapy console or raised.
93+ :param default: if provided, set a default value for the config file
10194
10295 ex, content of a config.py file:
10396 'conf.verb = 42\n '
@@ -107,6 +100,16 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
107100 2
108101
109102 """
103+ cf_path = pathlib .Path (cf )
104+ if not cf_path .exists ():
105+ log_loading .debug ("Config file [%s] does not exist." , cf )
106+ if default is None :
107+ return
108+ # We have a default ! set it
109+ cf_path .parent .mkdir (parents = True , exist_ok = True )
110+ with cf_path .open ("w" ) as fd :
111+ fd .write (default )
112+ log_loading .debug ("Config file [%s] created with default." , cf )
110113 log_loading .debug ("Loading config file [%s]" , cf )
111114 try :
112115 with open (cf ) as cfgf :
@@ -151,8 +154,7 @@ def _validate_local(k):
151154# conf.use_pcap = True
152155""" .strip ()
153156
154- DEFAULT_PRESTART_FILE = _probe_config_file (".config" , "scapy" , "prestart.py" ,
155- default = DEFAULT_PRESTART )
157+ DEFAULT_PRESTART_FILE = _probe_config_file (".config" , "scapy" , "prestart.py" )
156158DEFAULT_STARTUP_FILE = _probe_config_file (".config" , "scapy" , "startup.py" )
157159
158160
@@ -718,7 +720,8 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
718720 _read_config_file (
719721 PRESTART_FILE ,
720722 interactive = True ,
721- _locals = _scapy_prestart_builtins ()
723+ _locals = _scapy_prestart_builtins (),
724+ default = DEFAULT_PRESTART ,
722725 )
723726
724727 SESSION = init_session (session_name , mydict = mydict , ret = True )
0 commit comments