44import threading
55import time
66import sys
7+ import types
78
89import astunparse
910from pathlib import Path
@@ -29,6 +30,7 @@ def __init__(self, marshaller="dill", mode="memory"):
2930 "jupyter" : {"os_environ" : "" , "sys_path" : "" , "var" : "" },
3031 "subprocess" : {"os_environ" : "" , "sys_path" : "" , "var" : "" },
3132 }
33+ self .is_dump_detailed_report = False
3234
3335 def preprocess (self ):
3436
@@ -114,14 +116,23 @@ def jupyter_dump(self):
114116 f"import { self .marshaller } \n "
115117 "from jumper.userpersistence import dump_runtime, dump_variables, BusySpinner\n "
116118 "spinner = BusySpinner()\n "
117- "spinner.start('Loading data...')\n "
119+ f"if { self .is_dump_detailed_report } :\n "
120+ " spinner.start('Dumping runtime environment and sys.path...')\n "
121+ f"else:\n "
122+ " spinner.start('Loading data...')\n "
118123 "dump_runtime(os.environ, sys.path,"
119124 f"'{ self .paths ['jupyter' ]['os_environ' ]} ',"
120125 f"'{ self .paths ['jupyter' ]['sys_path' ]} ',{ self .marshaller } )\n "
126+ f"if { self .is_dump_detailed_report } :\n "
127+ " spinner.report('Dumping runtime environment and sys.path done.')\n "
128+ " spinner.start('Dumping variables...')\n "
121129 f"dump_variables({ str (self .jupyter_variables )} ,globals(),"
122130 f"'{ self .paths ['jupyter' ]['var' ]} ',"
123131 f"{ self .marshaller } )\n "
124- "spinner.stop('Data is loaded.')\n "
132+ f"if { self .is_dump_detailed_report } :\n "
133+ " spinner.stop('Dumping variables done.')\n "
134+ f"else:\n "
135+ " spinner.stop('Data is loaded.')\n "
125136 )
126137
127138 return jupyter_dump_
@@ -206,15 +217,14 @@ def parse(self, code, mode):
206217 self .subprocess_variables .clear ()
207218 self .subprocess_definitions += user_definitions
208219 self .subprocess_variables .extend (user_variables )
209- # print(f'subprocess {self.subprocess_variables=}')
210- # print(f'subprocess {self.subprocess_definitions=}')
211220 elif mode == "jupyter" :
212221 # Update aggregated storage of definitions and user variables
213222 # for entire notebook.
214223 self .jupyter_definitions += user_definitions
215224 self .jupyter_variables .extend (user_variables )
216- # print(f'jupyter {self.jupyter_definitions=}')
217- # print(f'jupyter {self.jupyter_variables=}')
225+
226+ def set_dump_report_level (self ):
227+ self .is_dump_detailed_report = int (os .getenv ('MARSHALLING_DETAILED_REPORT' , '0' ))
218228
219229
220230def dump_runtime (
@@ -238,9 +248,9 @@ def dump_runtime(
238248
239249
240250def dump_variables (variables_names , globals_ , var_dump_ , marshaller ):
241- # print(f"dump_variables {variables_names=}")
242251 user_variables = {
243252 k : v for k , v in globals_ .items () if k in variables_names
253+ and not isinstance (globals_ [k ], types .ModuleType )
244254 }
245255
246256 for el in user_variables .keys ():
@@ -251,14 +261,8 @@ def dump_variables(variables_names, globals_, var_dump_, marshaller):
251261 if non_persistent_class in globals ().keys ():
252262 user_variables [el ].__class__ = globals ()[non_persistent_class ]
253263
254- # estimated_size = sum(asizeof.asizeof(v) for v in user_variables.values())
255- # print("Estimated size:", variables_names)
256-
257264 with os .fdopen (os .open (var_dump_ , os .O_WRONLY | os .O_CREAT ), "wb" ) as file :
258- # wrapped_file = ProgressWriter(file, total_estimated=estimated_size)
259265 marshaller .dump (user_variables , file )
260- # marshaller.dump(user_variables, wrapped_file)
261- # wrapped_file.write(b'')
262266
263267
264268def load_runtime (
@@ -407,16 +411,18 @@ def _spinner_task(self):
407411 sys .stdout .flush ()
408412 time .sleep (0.1 )
409413 idx += 1
410- with self ._lock :
411- sys .stdout .write (f"\r { self .done_message } { ' ' * len (self .working_message )} \n " )
412- sys .stdout .flush ()
413414
414415 def start (self , working_message = 'Working...' ):
415416 self .working_message = working_message
416417 if not self ._thread .is_alive ():
417418 self ._thread .start ()
418419
420+ def report (self , done_message = 'Done.' ):
421+ with self ._lock :
422+ sys .stdout .write (f"\r { done_message } { ' ' * len (self .working_message )} \n " )
423+ sys .stdout .flush ()
424+
419425 def stop (self , done_message = 'Done.' ):
420- self .done_message = done_message
426+ self .report ( done_message )
421427 self ._stop_event .set ()
422428 self ._thread .join ()
0 commit comments