def import_file(request, lib_dir=None, tmp_dir=None, test_data=None): # input_data is passed by test cases only f = request.files['file'] filename = werkzeug.secure_filename(f.filename) input_data = test_data if 'simulationId' in request.form: input_data = simulation_db.read_simulation_json( elegant_common.SIM_TYPE, sid=request.form['simulationId']) if re.search(r'.ele$', filename, re.IGNORECASE): data = elegant_command_importer.import_file(f.read()) elif re.search(r'.lte$', filename, re.IGNORECASE): data = elegant_lattice_importer.import_file(f.read(), input_data) if input_data: _map_commands_to_lattice(data) else: raise IOError('invalid file extension, expecting .ele or .lte') data['models']['simulation']['name'] = re.sub(r'\.(lte|ele)$', '', filename, flags=re.IGNORECASE) if input_data and not test_data: simulation_db.delete_simulation( elegant_common.SIM_TYPE, input_data['models']['simulation']['simulationId']) return data
def import_file(req, test_data=None, **kwargs): # input_data is passed by test cases only input_data = test_data text = pkcompat.from_bytes(req.file_stream.read()) if 'simulationId' in req.req_data: input_data = simulation_db.read_simulation_json( SIM_TYPE, sid=req.req_data.simulationId) if re.search(r'\.ele$', req.filename, re.IGNORECASE): data = elegant_command_importer.import_file(text) elif re.search(r'\.lte$', req.filename, re.IGNORECASE): data = elegant_lattice_importer.import_file(text, input_data) if input_data: _map_commands_to_lattice(data) elif re.search(r'\.madx$', req.filename, re.IGNORECASE): from sirepo.template import madx_converter, madx_parser data = madx_converter.from_madx( SIM_TYPE, madx_parser.parse_file(text, downcase_variables=True)) else: raise IOError('invalid file extension, expecting .ele or .lte') data.models.simulation.name = re.sub(r'\.(lte|ele|madx)$', '', req.filename, flags=re.IGNORECASE) if input_data and not test_data: simulation_db.delete_simulation( SIM_TYPE, input_data.models.simulation.simulationId) return data
def parse_input_text(path, text=None, input_data=None): def _map(data): for cmd in data.models.commands: if cmd._type == 'run_setup': cmd.lattice = 'Lattice' break for cmd in data.models.commands: if cmd._type == 'run_setup': name = cmd.use_beamline.upper() for bl in data.models.beamlines: if bl.name.upper() == name: cmd.use_beamline = bl.id break if text is None: text = pkio.read_text(path) e = path.ext.lower() if e == '.ele': return elegant_command_importer.import_file(text) if e == '.lte': data = elegant_lattice_importer.import_file(text, input_data) if input_data: _map(data) return data if e == '.madx': return ElegantMadxConverter().from_madx_text(text) raise IOError( f'{path.basename}: invalid file format; expecting .madx, .ele, or .lte' )
def import_file(request, lib_dir=None, tmp_dir=None, test_data=None): # input_data is passed by test cases only f = request.files['file'] filename = werkzeug.secure_filename(f.filename) input_data = test_data if 'simulationId' in request.form: input_data = simulation_db.read_simulation_json(elegant_common.SIM_TYPE, sid=request.form['simulationId']) if re.search(r'.ele$', filename, re.IGNORECASE): data = elegant_command_importer.import_file(f.read()) elif re.search(r'.lte$', filename, re.IGNORECASE): data = elegant_lattice_importer.import_file(f.read(), input_data) if input_data: _map_commands_to_lattice(data) else: raise IOError('invalid file extension, expecting .ele or .lte') data['models']['simulation']['name'] = re.sub(r'\.(lte|ele)$', '', filename, flags=re.IGNORECASE) if input_data and not test_data: simulation_db.delete_simulation(elegant_common.SIM_TYPE, input_data['models']['simulation']['simulationId']) return data
def import_file(request, lib_dir=None, tmp_dir=None, test_data=None): # input_data is passed by test cases only f = request.files["file"] filename = werkzeug.secure_filename(f.filename) input_data = test_data if "simulationId" in request.form: input_data = simulation_db.read_simulation_json(_SIMULATION_TYPE, sid=request.form["simulationId"]) try: if re.search(r".ele$", filename, re.IGNORECASE): data = elegant_command_importer.import_file(f.read()) elif re.search(r".lte$", filename, re.IGNORECASE): data = elegant_lattice_importer.import_file(f.read(), input_data) if input_data: _map_commands_to_lattice(data) else: raise IOError("invalid file extension, expecting .ele or .lte") data["models"]["simulation"]["name"] = re.sub(r"\.(lte|ele)$", "", filename, re.IGNORECASE) if input_data and not test_data: simulation_db.delete_simulation(_SIMULATION_TYPE, input_data["models"]["simulation"]["simulationId"]) return None, data except IOError as e: return e.message, None
def import_file(req, test_data=None, **kwargs): # input_data is passed by test cases only input_data = test_data if 'simulationId' in req: input_data = simulation_db.read_simulation_json( elegant_common.SIM_TYPE, sid=req.simulationId) if re.search(r'.ele$', req.filename, re.IGNORECASE): data = elegant_command_importer.import_file(req.file_stream.read()) elif re.search(r'.lte$', req.filename, re.IGNORECASE): data = elegant_lattice_importer.import_file(req.file_stream.read(), input_data) if input_data: _map_commands_to_lattice(data) else: raise IOError('invalid file extension, expecting .ele or .lte') data.models.simulation.name = re.sub(r'\.(lte|ele)$', '', req.filename, flags=re.IGNORECASE) if input_data and not test_data: simulation_db.delete_simulation( elegant_common.SIM_TYPE, input_data.models.simulation.simulationId) return data