def api_findByName(simulation_type, application_mode, simulation_name): if cfg.oauth_login: from sirepo import oauth oauth.set_default_state(logged_out_as_anonymous=True) # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, 'simulation.isExample': True, }) if len(rows) == 0: for s in simulation_db.examples(simulation_type): if s['models']['simulation']['name'] == simulation_name: simulation_db.save_new_example(s) rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) break else: raise AssertionError(util.err(simulation_name, 'simulation not found with type {}', simulation_type)) return javascript_redirect( uri_router.format_uri( simulation_type, application_mode, rows[0]['simulationId'], simulation_db.get_schema(simulation_type) ) )
def api_findByName(simulation_type, application_mode, simulation_name): if cfg.oauth_login: from sirepo import oauth oauth.set_default_state(logged_out_as_anonymous=True) redirect_uri = None # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) if len(rows) == 0: for s in simulation_db.examples(simulation_type): if s['models']['simulation']['name'] == simulation_name: simulation_db.save_new_example(simulation_type, s) rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) break if len(rows): if application_mode == 'default': redirect_uri = '/{}#/source/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'lattice': redirect_uri = '/{}#/lattice/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'wavefront' or application_mode == 'light-sources': redirect_uri = '/{}#/beamline/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) else: redirect_uri = '/{}#/source/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) if redirect_uri: return javascript_redirect(redirect_uri) werkzeug.exceptions.abort(404)
def app_find_by_name(simulation_type, application_mode, simulation_name): redirect_uri = None # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) if len(rows) == 0: for s in simulation_db.examples(simulation_type): if s['models']['simulation']['name'] == simulation_name: simulation_db.save_new_example(simulation_type, s) rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) break if len(rows): if application_mode == 'default': redirect_uri = '/{}#/source/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'lattice': redirect_uri = '/{}#/lattice/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'wavefront' or application_mode == 'light-sources': redirect_uri = '/{}#/beamline/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) else: redirect_uri = '/{}#/source/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) if redirect_uri: # redirect using javascript for safari browser which doesn't support hash redirects return flask.render_template( 'html/javascript-redirect.html', redirect_uri=redirect_uri ) werkzeug.exceptions.abort(404)
def app_find_by_name(simulation_type, application_mode, simulation_name): redirect_uri = None # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) if len(rows) == 0: for s in simulation_db.examples(simulation_type): if s['models']['simulation']['name'] == simulation_name: simulation_db.save_new_example(simulation_type, s) rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) break if len(rows): if application_mode == 'default': redirect_uri = '/{}#/source/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'lattice': redirect_uri = '/{}#/lattice/{}'.format(simulation_type, rows[0]['simulationId']) elif application_mode == 'wavefront' or application_mode == 'light-sources': redirect_uri = '/{}#/beamline/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) else: redirect_uri = '/{}#/source/{}?application_mode={}'.format( simulation_type, rows[0]['simulationId'], application_mode) if redirect_uri: # redirect using javascript for safari browser which doesn't support hash redirects return flask.render_template( 'html/javascript-redirect.html', redirect_uri=redirect_uri ) werkzeug.exceptions.abort(404)
def api_updateFolder(): #TODO(robnagler) Folder should have a serial, or should it be on data req = http_request.parse_post() o = srschema.parse_folder(req.req_data['oldName']) if o == '/': raise sirepo.util.Error( 'cannot rename root ("/") folder', 'old folder is root req={}', req, ) n = srschema.parse_folder(req.req_data['newName']) if n == '/': raise sirepo.util.Error( 'cannot rename folder to root ("/")', 'new folder is root req={}', req, ) for r in simulation_db.iterate_simulation_datafiles( req.type, _simulation_data_iterator): f = r.models.simulation.folder l = o.lower() if f.lower() == o.lower(): r.models.simulation.folder = n elif f.lower().startswith(o.lower() + '/'): r.models.simulation.folder = n + f[len():] else: continue simulation_db.save_simulation_json(r) return http_reply.gen_json_ok()
def api_copySimulation(): """Takes the specified simulation and returns a newly named copy with the suffix (copy X)""" req = _json_input() sim_type = req['simulationType'] name = req['name'] if 'name' in req else None folder = req['folder'] if 'folder' in req else '/' data = simulation_db.read_simulation_json(sim_type, sid=req['simulationId']) if not name: base_name = data['models']['simulation']['name'] names = simulation_db.iterate_simulation_datafiles( sim_type, _simulation_name) count = 0 while True: count += 1 name = base_name + ' (copy{})'.format( ' {}'.format(count) if count > 1 else '') if name in names and count < 100: continue break data['models']['simulation']['name'] = name data['models']['simulation']['folder'] = folder data['models']['simulation']['isExample'] = False data['models']['simulation']['outOfSessionSimulationId'] = '' return _save_new_and_reply(data)
def create_examples(): """Adds missing app examples to all users. """ from pykern import pkio from sirepo import feature_config from sirepo import server from sirepo import simulation_db from sirepo import cookie server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) cookie.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for s in simulation_db.examples(sim_type): if s.models.simulation.name not in names: simulation_db.save_new_example(s)
def app_simulation_list(): input = _json_input() simulation_type = input['simulationType'] search = input['search'] if 'search' in input else None return json.dumps( sorted(simulation_db.iterate_simulation_datafiles( simulation_type, simulation_db.process_simulation_list, search), key=lambda row: row['last_modified'], reverse=True))
def api_findByNameWithAuth(simulation_type, application_mode, simulation_name): req = http_request.parse_params(type=simulation_type) #TODO(pjm): need to unquote when redirecting from saved cookie redirect? if hasattr(urllib, 'unquote'): # python2 simulation_name = urllib.unquote(simulation_name) else: # python3 simulation_name = urllib.parse.unquote(simulation_name) # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles( req.type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, 'simulation.isExample': True, }, ) if len(rows) == 0: for s in simulation_db.examples(req.type): if s['models']['simulation']['name'] != simulation_name: continue simulation_db.save_new_example(s) rows = simulation_db.iterate_simulation_datafiles( req.type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }, ) break else: sirepo.util.raise_not_found( 'simulation not found by name={} type={}', simulation_name, req.type, ) m = simulation_db.get_schema(req.type).appModes[application_mode] return http_reply.gen_redirect_for_local_route( req.type, m.localRoute, PKDict(simulationId=rows[0].simulationId), query=m.includeMode and PKDict(application_mode=application_mode), )
def app_update_folder(): #TODO(robnagler) Folder should have a serial, or should it be on data data = _parse_data_input() old_name = data['oldName'] new_name = data['newName'] for row in simulation_db.iterate_simulation_datafiles(data['simulationType'], _simulation_data): folder = row['models']['simulation']['folder'] if folder.startswith(old_name): row['models']['simulation']['folder'] = re.sub(re.escape(old_name), new_name, folder, 1) simulation_db.save_simulation_json(data['simulationType'], row) return _json_response_ok();
def app_simulation_list(): input = _json_input() simulation_type = input['simulationType'] search = input['search'] if 'search' in input else None return json.dumps( sorted( simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, search), key=lambda row: row['last_modified'], reverse=True ) )
def app_simulation_list(): data = _parse_data_input() sim_type = data['simulationType'] search = data['search'] if 'search' in data else None simulation_db.verify_app_directory(sim_type) return _json_response( sorted( simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, search), key=lambda row: row['name'], ) )
def api_listSimulations(): data = _parse_data_input() sim_type = data['simulationType'] search = data['search'] if 'search' in data else None simulation_db.verify_app_directory(sim_type) return http_reply.gen_json( sorted( simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, search), key=lambda row: row['name'], ))
def api_updateFolder(): #TODO(robnagler) Folder should have a serial, or should it be on data data = _parse_data_input() old_name = data['oldName'] new_name = data['newName'] for row in simulation_db.iterate_simulation_datafiles(data['simulationType'], _simulation_data): folder = row['models']['simulation']['folder'] if folder.startswith(old_name): row['models']['simulation']['folder'] = re.sub(re.escape(old_name), new_name, folder, 1) simulation_db.save_simulation_json(row) return http_reply.gen_json_ok()
def api_listSimulations(): req = http_request.parse_post() simulation_db.verify_app_directory(req.type) return http_reply.gen_json( sorted( simulation_db.iterate_simulation_datafiles( req.type, simulation_db.process_simulation_list, req.req_data.get('search'), ), key=lambda row: row['name'], ))
def api_findByName(simulation_type, application_mode, simulation_name): sim_type = sirepo.template.assert_sim_type(simulation_type) # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, 'simulation.isExample': True, }, ) if len(rows) == 0: for s in simulation_db.examples(sim_type): if s['models']['simulation']['name'] != simulation_name: continue simulation_db.save_new_example(s) rows = simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }, ) break else: util.raise_not_found( 'simulation not found by name={} type={}', simulation_name, sim_type, ) # format the uri for the local route to this simulation for application_mode s = simulation_db.get_schema(sim_type) m = s.appModes[application_mode] r = m.localRoute assert r in s.localRoutes u = '/{}#/{}/{}'.format(sim_type, r, rows[0].simulationId) if m.includeMode: u += '?application_mode={}'.format(application_mode) return http_reply.gen_redirect_for_anchor(u)
def _simulations_using_file(simulation_type, file_type, search_name, ignore_sim_id=None): res = [] template = sirepo.template.import_module(simulation_type) if not hasattr(template, 'validate_delete_file'): return res for row in simulation_db.iterate_simulation_datafiles(simulation_type, _simulation_data): if template.validate_delete_file(row, search_name, file_type): sim = row['models']['simulation'] if ignore_sim_id and sim['simulationId'] == ignore_sim_id: continue if sim['folder'] == '/': res.append('/{}'.format(sim['name'])) else: res.append('{}/{}'.format(sim['folder'], sim['name'])) return res
def _simulations_using_file(req, ignore_sim_id=None): res = [] for r in simulation_db.iterate_simulation_datafiles( req.type, _simulation_data_iterator): if not req.sim_data.lib_file_in_use(r, req.filename): continue s = r.models.simulation if s.simulationId == ignore_sim_id: continue res.append('{}{}{}'.format( s.folder, '' if s.folder == '/' else '/', s.name, )) return res
def api_findByName(simulation_type, application_mode, simulation_name): # use the existing named simulation, or copy it from the examples rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, 'simulation.isExample': True, }) if len(rows) == 0: for s in simulation_db.examples(simulation_type): if s['models']['simulation']['name'] == simulation_name: simulation_db.save_new_example(s) rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, { 'simulation.name': simulation_name, }) break else: util.raise_not_found('{}: simulation not found by name: {}', simulation_type, simulation_name) return javascript_redirect( uri_router.format_uri( simulation_type, application_mode, rows[0]['simulationId'], simulation_db.get_schema(simulation_type) ) )
def app_copy_simulation(): """Takes the specified simulation and returns a newly named copy with the suffix (copy X)""" req = _json_input() simulation_type = req['simulationType'] data = simulation_db.open_json_file(simulation_type, sid=req['simulationId']) base_name = data['models']['simulation']['name'] names = simulation_db.iterate_simulation_datafiles(simulation_type, _simulation_name) count = 0 while True: count += 1 name = base_name + ' (copy{})'.format(' {}'.format(count) if count > 1 else '') if name in names and count < 100: continue break data['models']['simulation']['name'] = name data['models']['simulation']['isExample'] = '' data['models']['simulation']['outOfSessionSimulationId'] = '' return _save_new_and_reply(simulation_type, data)
def create_examples(): """Adds missing app examples to all users. """ server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) auth.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)
def app_copy_simulation(): """Takes the specified simulation and returns a newly named copy with the suffix (copy X)""" req = _json_input() simulation_type = req['simulationType'] data = simulation_db.open_json_file(simulation_type, sid=req['simulationId']) base_name = data['models']['simulation']['name'] names = simulation_db.iterate_simulation_datafiles(simulation_type, _simulation_name) count = 0 while True: count += 1 name = base_name + ' (copy{})'.format( ' {}'.format(count) if count > 1 else '') if name in names and count < 100: continue break data['models']['simulation']['name'] = name data['models']['simulation']['isExample'] = '' data['models']['simulation']['outOfSessionSimulationId'] = '' return _save_new_and_reply(simulation_type, data)
def create_examples(): """Adds missing app examples to all users""" import sirepo.auth_db import sirepo.server sirepo.server.init() for d in pkio.sorted_glob(simulation_db.user_path().join('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) with sirepo.auth_db.session_and_lock(), \ auth.set_user_outside_of_http_request(uid): for sim_type in feature_config.cfg().sim_types: simulation_db.verify_app_directory(sim_type) names = [x.name for x in simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })] for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)
def create_examples(): """Adds missing app examples to all users. """ server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*')): if _is_src_dir(d): continue uid = simulation_db.uid_from_dir_name(d) auth.set_user_for_utils(uid) for sim_type in feature_config.cfg().sim_types: simulation_db.verify_app_directory(sim_type) names = [ x.name for x in simulation_db.iterate_simulation_datafiles( sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, }) ] for example in simulation_db.examples(sim_type): if example.models.simulation.name not in names: _create_example(example)