def create_map(request): '''Creates a Gnome Map object.''' log_prefix = 'req({0}): create_map():'.format(id(request)) init_session_objects(request) try: json_request = ujson.loads(request.body) except: raise cors_exception(request, HTTPBadRequest) if not JSONImplementsOneOf(json_request, implemented_types): raise cors_exception(request, HTTPNotImplemented) if 'filename' in json_request: json_request['filename'] = get_file_path(request, json_request=json_request) gnome_sema = request.registry.settings['py_gnome_semaphore'] gnome_sema.acquire() log.info(' ' + log_prefix + 'semaphore acquired...') try: obj = CreateObject(json_request, get_session_objects(request)) except: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') set_session_object(obj, request) return obj.serialize()
def create_model(request): ''' Creates a new model ''' log_prefix = 'req({0}): create_object():'.format(id(request)) log.info('>>' + log_prefix) try: json_request = ujson.loads(request.body.decode('utf-8')) except Exception: json_request = None if json_request and not JSONImplementsOneOf(json_request, implemented_types): raise cors_exception(request, HTTPNotImplemented) session_lock = acquire_session_lock(request) log.info(' {} session lock acquired (sess:{}, thr_id: {})'.format( log_prefix, id(session_lock), current_thread().ident)) try: clean_session_dir(request) init_session_objects(request, force=True) if json_request: new_model = CreateObject(json_request, get_session_objects(request)) else: new_model = Model() set_session_object(new_model, request) set_active_model(request, new_model.id) except Exception: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: session_lock.release() log.info(' {} session lock released (sess:{}, thr_id: {})'.format( log_prefix, id(session_lock), current_thread().ident)) log.info('<<' + log_prefix) return new_model.serialize(options=web_ser_opts)
def create_model(request): ''' Creates a new model ''' log_prefix = 'req({0}): create_object():'.format(id(request)) log.info('>>' + log_prefix) try: json_request = ujson.loads(request.body) except: json_request = None if json_request and not JSONImplementsOneOf(json_request, implemented_types): raise cors_exception(request, HTTPNotImplemented) gnome_sema = request.registry.settings['py_gnome_semaphore'] gnome_sema.acquire() log.info(' ' + log_prefix + 'semaphore acquired...') try: init_session_objects(request, force=True) if json_request: new_model = CreateObject(json_request, get_session_objects(request)) else: new_model = Model() set_session_object(new_model, request) set_session_object(new_model._map, request) set_active_model(request, new_model.id) except: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') log.info('<<' + log_prefix) return new_model.serialize()
def create_map(request): '''Creates a Gnome Map object.''' init_session_objects(request.session) try: json_request = json.loads(request.body) except: raise HTTPBadRequest() if not JSONImplementsOneOf(json_request, implemented_types): raise HTTPNotImplemented() # TODO: should we tie our data directory to the installation path? # or should we be more flexible? map_dir = get_map_dir_from_config(request) json_request['filename'] = os.path.join(map_dir, json_request['filename']) obj = CreateObject(json_request, request.session['objects']) set_session_object(obj, request.session) return obj.serialize()
def create_model(request): ''' Creates a new model ''' log_prefix = 'req({0}): create_object():'.format(id(request)) print '>>', log_prefix try: json_request = json.loads(request.body) except: json_request = None if json_request and not JSONImplementsOneOf(json_request, implemented_types): raise HTTPNotImplemented() gnome_sema = request.registry.settings['py_gnome_semaphore'] gnome_sema.acquire() print ' ', log_prefix, 'semaphore acquired...' try: init_session_objects(request.session, force=True) if json_request: new_model = CreateObject(json_request, get_session_objects(request.session)) else: new_model = Model() set_session_object(new_model, request.session) set_session_object(new_model._map, request.session) set_active_model(request.session, new_model.id) finally: gnome_sema.release() print ' ', log_prefix, 'semaphore released...' print '<<', log_prefix return new_model.serialize()
def run_export_model(request): ''' Configures the active model as specified by the request, then spawns a gevent greenlet that runs the model, writing only step number to the web socket. When the greenlet running the model dies, it removes the outputters that were added via a linked function ''' print('async export hit') log_prefix = 'req{0}: run_export_model()'.format(id(request)) log.info('>>' + log_prefix) sess_id = request.session.session_id ns = request.registry.get('sio_ns') if ns is None: raise ValueError('no namespace associated with session') active_model = get_active_model(request) #setup temporary outputters and temporary output directory session_path = get_session_dir(request) temporary_outputters = [] payload = ujson.loads(request.body) outpjson = payload['outputters'] model_filename = payload['model_name'] td = tempfile.mkdtemp() for itm in list(outpjson.values()): itm['filename'] = os.path.join(td, itm['filename']) obj = CreateObject(itm, get_session_objects(request)) temporary_outputters.append(obj) for o in temporary_outputters: #separated these just in case an exception occurs when #creating an outputter, which may leave a different successfully added #outputter behind if one was created before the exception active_model.outputters += o log.info('attaching export outputter: ' + o.filename) sid = ns.get_sockid_from_sessid(request.session.session_id) def get_export_cleanup(): def cleanup(grn): try: #remove outputters from the model num = 0 for m in temporary_outputters: active_model.outputters.remove(m.id) num += 1 active_model.rewind() log.info(grn.__repr__() + ': cleaned up ' + str(num) + ' outputters') end_filename = None if (grn.exception or isinstance(grn.value, GreenletExit)): #A cleanly stopped Greenlet may exit with GreenletExit #Do not consider this a 'successful' export run even if files exist ns.emit('export_failed', room=sid) else: if len(temporary_outputters) > 1: #need to zip up outputs end_filename = model_filename + '_output.zip' zipfile_ = zipfile.ZipFile( os.path.join(session_path, end_filename), 'w', compression=zipfile.ZIP_DEFLATED) for m in temporary_outputters: obj_fn = m.filename if not os.path.exists(obj_fn): obj_fn = obj_fn + '.zip' #special case for shapefile outputter which strips extensions... zipfile_.write(obj_fn, os.path.basename(obj_fn)) else: #only one output file, because one outputter selected obj_fn = temporary_outputters[0].filename if not os.path.exists(obj_fn): obj_fn = obj_fn + '.zip' #special case for shapefile outputter end_filename = os.path.basename(obj_fn) shutil.move(obj_fn, os.path.join(session_path, end_filename)) ns.emit('export_finished', end_filename, room=sid) except Exception: if ('develop_mode' in list(request.registry.settings.keys()) and request.registry.settings['develop_mode'].lower() == 'true'): import pdb pdb.post_mortem(sys.exc_info()[2]) raise return cleanup if sid is None: raise ValueError('no sock_session associated with pyramid_session') with ns.session(sid) as sock_session: sock_session['num_sent'] = 0 if active_model and not ns.active_greenlets.get(sid): gl = ns.active_greenlets[sid] = gevent.spawn( execute_async_model, active_model, ns, sid, request) gl.session_hash = request.session_hash gl.link(get_export_cleanup()) return None else: print("Already started") return None