64
64
]
65
65
66
66
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]
69
69
path = pathlib .Path (os .path .expanduser ("~" ))
70
70
if not path .exists ():
71
71
# ~ folder doesn't exist. Unsalvageable
72
72
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 ())
83
74
84
75
85
76
def _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
88
79
"""Read a config file: execute a python file while loading scapy, that
89
80
may contain some pre-configured values.
90
81
@@ -93,11 +84,13 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
93
84
function. Otherwise, vars are only available from inside the scapy
94
85
console.
95
86
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
100
92
using the scapy console or raised.
93
+ :param default: if provided, set a default value for the config file
101
94
102
95
ex, content of a config.py file:
103
96
'conf.verb = 42\n '
@@ -107,6 +100,16 @@ def _read_config_file(cf, _globals=globals(), _locals=locals(),
107
100
2
108
101
109
102
"""
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 )
110
113
log_loading .debug ("Loading config file [%s]" , cf )
111
114
try :
112
115
with open (cf ) as cfgf :
@@ -151,8 +154,7 @@ def _validate_local(k):
151
154
# conf.use_pcap = True
152
155
""" .strip ()
153
156
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" )
156
158
DEFAULT_STARTUP_FILE = _probe_config_file (".config" , "scapy" , "startup.py" )
157
159
158
160
@@ -718,7 +720,8 @@ def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO):
718
720
_read_config_file (
719
721
PRESTART_FILE ,
720
722
interactive = True ,
721
- _locals = _scapy_prestart_builtins ()
723
+ _locals = _scapy_prestart_builtins (),
724
+ default = DEFAULT_PRESTART ,
722
725
)
723
726
724
727
SESSION = init_session (session_name , mydict = mydict , ret = True )
0 commit comments