コード例 #1
0
def parse_file(lattice_text, maxId=0):
    parser = LineParser(maxId)
    lines = lattice_text.replace('\r', '').split('\n')
    prev_line = ''
    models = {
        'beamlines': [],
        'elements': [],
        'default_beamline_name': None,
        'rpnVariables': {},
    }
    for line in lines:
        parser.increment_line_number()
        if re.search(r'^\s*\!', line):
            continue
        if re.search(r'\&\s*$', line):
            prev_line += re.sub(r'(\s*\&\s*)$', '', line)
            continue
        if not _parse_line(parser, prev_line + line, models):
            break
        prev_line = ''
    models['rpnVariables'] = map(
        lambda x: {
            'name': x,
            'value': models['rpnVariables'][x]
        }, models['rpnVariables'].keys())
    return models
コード例 #2
0
def parse_file(command_text):
    parser = LineParser(0)
    lines = command_text.replace('\r', '').split('\n')
    prev_line = ''
    commands = []

    for line in lines:
        parser.increment_line_number()
        if re.search(r'^#', line):
            continue
        line = re.sub(r'\!.*$', '', line)
        if not line:
            continue
        if re.search(r'\&end', line):
            if not _parse_line(parser, prev_line + ' ' + line, commands):
                break
            prev_line = ''
        elif re.search(r'\&', line) or prev_line:
            prev_line += ' ' + line
        else:
            # ignoring lines between command markers
            pass
    if prev_line and re.search(r'\&', prev_line):
        parser.raise_error('missing &end for command: {}'.format(prev_line))
    _update_lattice_names(commands)
    return commands
コード例 #3
0
ファイル: zgoubi_parser.py プロジェクト: yeeon/sirepo
def parse_file(zgoubi_text, max_id=0):
    parser = LineParser(max_id)
    lines = zgoubi_text.replace('\r', '').split('\n')
    elements = []
    # skip first documentation line
    title = lines.pop(0)
    parser.increment_line_number()
    unhandled_elements = {}
    current_command = None
    for line in lines:
        parser.increment_line_number()
        line = re.sub(r'\!.*$', '', line)
        line = re.sub(r'^\s+', '', line)
        line = re.sub(r'\s+$', '', line)
        if not line:
            continue
        keyword = _parse_keyword(line)
        if keyword:
            if current_command:
                _add_command(parser, current_command, elements,
                             unhandled_elements)
            if keyword == 'END' or keyword == 'FIN':
                current_command = None
                break
            line = _strip_command_index(line)
            current_command = [line.split()]
            current_command[0][0] = keyword
        else:
            line = line.lstrip()
            current_command.append(line.split())
    assert current_command is None, 'missing END element'
    return title, elements, sorted(unhandled_elements.keys())
コード例 #4
0
 def parse_file(self, lattice_text):
     from sirepo import simulation_db
     self.data = simulation_db.default_data(self.sim_data.sim_type())
     self.parser = LineParser(100)
     self.data.models.rpnVariables = {}
     self.data.models.sequences = []
     # None | sequence | track | match | edit
     self.container = None
     self.elements_by_name = PKDict()
     lines = lattice_text.replace('\r', '').split('\n')
     self.__parse_lines(lines)
     return self.data
コード例 #5
0
def parse_file(lattice_text, rpn_variables, maxId=0):
    parser = LineParser(maxId)
    lines = lattice_text.replace('\r', '').split('\n')
    prev_line = ''
    models = PKDict(
        beamlines=[],
        elements=[],
        default_beamline_name=None,
        rpnVariables=PKDict(),
    )
    for line in lines:
        parser.increment_line_number()
        if re.search(r'^\s*\!', line):
            continue
        if re.search(r'\&\s*$', line):
            prev_line += re.sub(r'(\s*\&\s*)$', '', line)
            continue
        if not _parse_line(parser, prev_line + line, models):
            break
        prev_line = ''
    models['rpnVariables'] = [PKDict(name=k, value=v) for k, v in models.rpnVariables.items()] \
        + rpn_variables
    return models