def app_import_file(simulation_type): template = sirepo.template.import_module(simulation_type) error, data = template.import_file(flask.request, simulation_db.simulation_lib_dir(simulation_type), simulation_db.tmp_dir()) if error: return _json_response({'error': error}) data['models']['simulation']['folder'] = flask.request.form['folder'] return _save_new_and_reply(simulation_type, data)
def api_importFile(simulation_type=None): """ Args: simulation_type (str): which simulation type Params: file: file data folder: where to import to """ import sirepo.importer error = None f = None try: template = simulation_type and sirepo.template.import_module( simulation_type) f = flask.request.files.get('file') assert f, \ ValueError('must supply a file') if pkio.has_file_extension(f.filename, 'json'): data = sirepo.importer.read_json(f.read(), template) #TODO(pjm): need a separate URI interface to importer, added exception for rs4pi for now # (dicom input is normally a zip file) elif pkio.has_file_extension(f.filename, 'zip') and simulation_type != 'rs4pi': data = sirepo.importer.read_zip(f.stream, template) else: assert simulation_type, \ 'simulation_type is required param for non-zip|json imports' assert hasattr(template, 'import_file'), \ ValueError('Only zip files are supported') data = template.import_file( flask.request, simulation_db.simulation_lib_dir(simulation_type), simulation_db.tmp_dir(), ) #TODO(robnagler) need to validate folder data.models.simulation.folder = flask.request.form['folder'] data.models.simulation.isExample = False return _save_new_and_reply(data) except Exception as e: pkdlog('{}: exception: {}', f and f.filename, pkdexc()) error = str(e.message) if hasattr(e, 'message') else str(e) return http_reply.gen_json({ 'error': error if error else 'An unknown error occurred', })
def api_importFile(simulation_type=None): """ Args: simulation_type (str): which simulation type Params: file: file data folder: where to import to """ import sirepo.importer error = None f = None try: template = simulation_type and sirepo.template.import_module(simulation_type) f = flask.request.files.get('file') assert f, \ ValueError('must supply a file') if pkio.has_file_extension(f.filename, 'json'): data = sirepo.importer.read_json(f.read(), template) elif pkio.has_file_extension(f.filename, 'zip'): data = sirepo.importer.read_zip(f.stream, template) else: assert simulation_type, \ 'simulation_type is required param for non-zip|json imports' data = template.import_file( flask.request, simulation_db.simulation_lib_dir(simulation_type), simulation_db.tmp_dir(), ) #TODO(robnagler) need to validate folder data.models.simulation.folder = flask.request.form['folder'] return _save_new_and_reply(data.simulationType, data) except Exception as e: pkdlog('{}: exception: {}', f and f.filename, pkdexc()) error = e.message if hasattr(e, 'message') else str(e) return _json_response({'error': error})