File: read_definition.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 (150 lines) | stat: -rw-r--r-- 5,591 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from mcstasscript.instr_reader.util import SectionReader


class DefinitionReader(SectionReader):
    """
    Responsible for reading the defintion section of McStas instrument
    file. Contains instrument name and instrument parameters.
    """

    def __init__(self, Instr, write_file, product_filename,
                 get_next_line, return_line):

        super().__init__(Instr, write_file, product_filename,
                         get_next_line, return_line)

    def read_definition_line(self, line):
        """
        Reads line of instrument definition, returns bolean.  If it encounters
        the end of the definition section, it returns False, otherwise True.

        The contents of the definition section is written to the McStasScript
        Instr object.
        """

        continue_definition = True

        # Remove comments
        if "//" in line:
            line = line.split("//")[0]

        if "(" in line:
            # Start of instrument definition, get name
            self.instr_name = line.split("(")[0].strip().split(" ")[-1]
            self._start_py_file()
            # Remove the parameters from the paranthesis
            parameters = line.split("(")[1]
            if ")" in line:
                # Found end of definition
                continue_definition = False
                # these parameters are to be analyzed
                parameters = parameters.split(")")[0]

        elif ")" in line:
            # Found end of definition
            continue_definition = False
            # these parameters are to be analyzed
            parameters = line.split(")")[0]
        else:
            # Neither start or end on this line, analyze everything
            parameters = line

        # Separate into individual parameters
        parameters = parameters.split(",")
        if "\n" in parameters:
            parameters.remove("\n")

        for parameter in parameters:
            # Analyze individual parameter
            parameter = parameter.strip()

            if parameter == "":
                # If the parameter is empty, skip it.
                continue

            # Ready for keyword arguments
            kw_args = {}

            # Default to double type if nothing else is set
            parameter_type = "double"
            if " " and "=" in parameter:
                # Read parameter type
                type_and_name = parameter.split("=", 1)[0].strip()

                if " " in type_and_name:
                    parameter_type = type_and_name.split(" ", 1)[0].strip()
                    parameter = parameter.split(" ", 1)[1].strip()
            elif " " in parameter:
                # Read parameter type
                parameter_type = parameter.split(" ", 1)[0].strip()
                parameter = parameter.split(" ", 1)[1].strip()

            if "=" in parameter:
                # Read default value
                parameter_name = parameter.split("=")[0].strip()
                value = parameter.split("=")[1].strip()

                if parameter_type == "string":
                    if '"' in value:
                        pass
                        # Value has to be normal for object version
                        #value = value.replace('"', "\\\"")
                        #value = "\"" + value + "\""
                else:
                    if parameter_type == "int":
                        value = int(value)
                    else:
                        value = float(value)

                # Add defualt value to keyword arguments
                kw_args["value"] = value

            else:
                # No default value, just return the striped name
                parameter_name = parameter.strip()

            # Add this parameter to the object
            self.Instr.add_parameter(parameter_type, parameter_name, **kw_args)

            # Fix values for script version
            if parameter_type == "string" and "value" in kw_args:
                if isinstance(kw_args["value"], str):
                    kw_args["value"] = kw_args["value"].replace('"', '\\\"')
                    kw_args["value"] = "\"" + kw_args["value"] + "\""

            # Also write it to a file?
            write_string = []
            write_string.append(self.instr_name)
            write_string.append(".add_parameter(")
            write_string.append("\"" + parameter_type + "\"")
            write_string.append(", ")
            write_string.append("\"" + parameter_name + "\"")
            write_string.append(self._kw_to_string(kw_args))
            write_string.append(")\n")

            self._write_to_file(write_string)

        return continue_definition

    def _start_py_file(self):
        write_string = []

        # Write warning about robustness of this feature
        write_string.append("\"\"\"\n")
        write_string.append("This McStasScript file was generated from a\n")
        write_string.append("McStas instrument file. It is advised to check\n")
        write_string.append("the content to ensure it is as expected.\n")
        write_string.append("\"\"\"\n")

        # import McStasScript
        write_string.append("from mcstasscript.interface ")
        write_string.append("import ")
        write_string.append("instr, plotter, functions")
        write_string.append("\n\n")

        write_string.append(self.instr_name)
        write_string.append(" = instr.McStas_instr(")
        write_string.append("\"" + self.instr_name + "_generated\"")
        write_string.append(")\n")

        self._write_to_file(write_string)