File: reader.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (79 lines) | stat: -rw-r--r-- 2,274 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from mcstasscript.instr_reader.control import InstrumentReader
from mcstasscript.interface.instr import McStas_instr


class McStas_file:
    """
    Reader of McStas files, can add to an existing McStasScript
    instrument instance or create a corresponding McStasScript python
    file.

    Methods
    -------

    add_to_instr(Instr)
        Add information from McStas file to McStasScript Instr instance

    write_python_file(filename)
        Write python file named filename that reproduce the McStas instr
    """

    def __init__(self, filename):
        """
        Initialization of McStas_file class, needs McStas instr filename

        Parameters
        ----------
            filename (str)
                Name of McStas instrument file to be read
        """

        # Check filename
        if not os.path.isfile(filename):
            raise ValueError("Given filename, \"" + filename
                             + "\" could not be found.")

        self.Reader = InstrumentReader(filename)

    def add_to_instr(self, Instr):
        """
        Adds information from the McStas file to McStasScript instr

        Parameters
        ----------
            Instr (McStasScript McStas_instr instance)
                McStas_instr instance to add instrument information to
        """

        # Check Instr
        if not isinstance(Instr, McStas_instr):
            raise TypeError("Given object is not of type McStas_instr!")

        self.Reader.add_to_instr(Instr)

    def write_python_file(self, filename, **kwargs):
        """
        Writes python file that reproduces McStas instrument file

        Parameters
        ----------
            filename (str)
                Filename of python file to be written
        """

        if "force" in kwargs:
            force = kwargs["force"]
        else:
            force = False

        # Check product_filename is available
        if os.path.isfile(filename):
            if force:
                os.remove(filename)
            else:
                raise ValueError("Filename \"" + filename
                                 + "\" already exists, you can overwrite with "
                                 + "force=True")

        self.Reader.generate_py_version(filename)