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
|
from mcstasscript.instr_reader.util import SectionReader
class InitializeReader(SectionReader):
"""
Reads the initialize section of a McStas instrument file.
The initialize lines are added to the McStasScript instrument, and
are sent to the function writing the lines to the python file.
"""
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_initialize_line(self, line):
"""
Reads lines from INITIALIZE file and returns True as long as
the stop characters has not been encountered. Comments are
ignored with typical c syntax.
"""
continue_initialize = True
# Remove comments
if "//" in line:
line = line.split("//", 1)[0].strip()
if line.startswith("INITIALIZE"):
line = line.split("INITIALIZE", 1)[1].strip()
if line.startswith("INITIALISE"):
line = line.split("INITIALISE", 1)[1].strip()
# Remove block opening
if "%{" in line:
line = line.split("%{", 1)[1].strip()
if "%}" in line:
line = line.split("%}", 1)[0].strip()
continue_initialize = False
# If the line is just a new line quit
if line == "\n" or line == "":
return continue_initialize
# Remove newline at the end of the line
if line.endswith("\n"):
line = line[:-1]
self.Instr.append_initialize(line)
# Need to prepare string for being written again
write_line = line.replace("\\n", "\\\\n")
write_line = write_line.replace("\\t", "\\\\t")
write_line = write_line.replace('"', '\\\"')
# May need to expand to more cases
# Write line to Python file
write_string = []
write_string.append(self.instr_name)
write_string.append(".append_initialize(")
write_string.append("\"" + write_line + " \"")
write_string.append(")\n")
self._write_to_file(write_string)
return continue_initialize
|