File: unpickler.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (47 lines) | stat: -rw-r--r-- 1,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pickle


class CustomMcStasUnpickler(pickle.Unpickler):
    """
    Helps pickle find dynamic classes on users computer, McStas version

    Usage:
    pickle_data = CustomUnpickler(open('filename.dill', 'rb')).load()
    """

    def find_class(self, module, name):
        if not hasattr(self, "mcstasscript_instr"):
            from mcstasscript.interface import instr

            self.mcstasscript_instr = instr.McStas_instr("dummy")
            self.component_list = list(self.mcstasscript_instr.component_reader.component_category.keys())

        if str(module) == 'mcstasscript.interface.instr':
            if name in self.component_list:
                self.mcstasscript_instr._create_component_instance("dummy", name)

        return super().find_class(module, name)


class CustomMcXtraceUnpickler(pickle.Unpickler):
    """
    Helps pickle find dynamic classes on users computer, McXtrace version

    Usage:
    pickle_data = CustomUnpickler(open('filename.dill', 'rb')).load()
    """

    def find_class(self, module, name):
        if not hasattr(self, "mcstasscript_instr"):
            from mcstasscript.interface import instr

            self.mcstasscript_instr = instr.McXtrace_instr("dummy")
            self.component_list = list(self.mcstasscript_instr.component_reader.component_category.keys())

        if str(module) == 'mcstasscript.interface.instr':
            if name in self.component_list:
                self.mcstasscript_instr._create_component_instance("dummy", name)

        return super().find_class(module, name)