13Import this module at the top of your steering file to get a (more) pickable version of basf2.
14This is useful if you want to pickle the path using --dump-path and execute it later using --execute-path.
15Usually all calls to functions in basf2 like use_central_database are forgotten if you only save the path.
16With this module, these functions are executed again if you execute the pickled path using --execute-path.
18Technically this works by recording all calls to basf2 functions.
19If you have other code which should be pickled as well you can wrap it in the make_code_pickable function.
21If you want to exclude some functions, delete them from this module using
22del pickable_basf2.functionname
29import unittest.mock
as mock
30sys.modules[
'original_basf2'] = sys.modules[
'basf2']
35 """ Drop-in replacement of the basf2 module, which keeps track of all functions calls """
38 """ Return attribute with the given name in the basf2 namespace """
39 return getattr(original_basf2, name)
48def process(path, max_event=0):
49 """ Process call which pickles the recorded state in addition to the path """
50 sys.modules[
'basf2'] = original_basf2
51 original_basf2.process(path, max_event)
52 state = list(map(tuple, manager.mock_calls))
53 pickle_path = original_basf2.get_pickle_path()
56 if pickle_path !=
'' and path
is not None:
57 serialized = original_basf2.serialize_path(path)
58 serialized[
'state'] = state
59 pickle.dump(serialized, open(pickle_path,
'bw'))
60 sys.modules[
'basf2'] = basf2_state_recorder
63for name, x
in original_basf2.__dict__.items():
65 if inspect.isfunction(x)
or isinstance(x, type(original_basf2.find_file)):
67 mock_x = mock.Mock(x, side_effect=x)
68 manager.attach_mock(mock_x, name)
69 setattr(basf2_state_recorder, name, mock_x)
72 setattr(basf2_state_recorder, name, x)
75basf2_state_recorder.process = process
77sys.modules[
'basf2'] = basf2_state_recorder
78sys.modules[
'pickable_basf2'] = basf2_state_recorder
def __getattr__(self, name)