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_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 update_model(request): ''' Returns Model object in JSON. - This method varies slightly from the common object method in that if we don't specify a model ID, we: - update the current active model if it exists or... - generate a 'Not Found' exception. ''' log_prefix = 'req({0}): update_model():'.format(id(request)) log.info('>>' + log_prefix) ret = None try: json_request = ujson.loads(request.body.decode('utf-8')) except Exception: raise cors_exception(request, HTTPBadRequest) if 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)) obj_id = obj_id_from_req_payload(json_request) if obj_id: active_model = get_session_object(obj_id, request) else: active_model = get_active_model(request) if active_model: try: if UpdateObject(active_model, json_request, get_session_objects(request)): set_session_object(active_model, request) ret = active_model.serialize(options=web_ser_opts) except Exception: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: session_lock.release() log.info(' ' + log_prefix + 'session lock released...') else: session_lock.release() log.info(' {} session lock released (sess:{}, thr_id: {})'.format( log_prefix, id(session_lock), current_thread().ident)) msg = ("raising cors_exception() in update_model. " "Updating model before it exists.") log.warning(' ' + log_prefix + msg) raise cors_exception(request, HTTPNotFound) log.info('<<' + log_prefix) return ret
def update_model(request): ''' Returns Model object in JSON. - This method varies slightly from the common object method in that if we don't specify a model ID, we: - update the current active model if it exists or... - generate a 'Not Found' exception. ''' log_prefix = 'req({0}): update_model():'.format(id(request)) log.info('>>' + log_prefix) ret = None 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) gnome_sema = request.registry.settings['py_gnome_semaphore'] gnome_sema.acquire() log.info(' ' + log_prefix + 'semaphore acquired...') obj_id = obj_id_from_req_payload(json_request) if obj_id: active_model = get_session_object(obj_id, request) else: active_model = get_active_model(request) if active_model: try: if UpdateObject(active_model, json_request, get_session_objects(request)): set_session_object(active_model, request) ret = active_model.serialize() except: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') else: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') msg = ("raising cors_exception() in update_model. " "Updating model before it exists.") log.warning(' ' + log_prefix + msg) raise cors_exception(request, HTTPNotFound) log.info('<<' + log_prefix) return ret
def upload_model(request): ''' Uploads a new model in the form of a zipfile and registers it as the current active model. We are generating our own filename instead of trusting the incoming filename since that might result in insecure paths. We may want to eventually use something other than /tmp, and if you write to an untrusted location you will need to do some extra work to prevent symlink attacks. ''' clean_session_dir(request) file_path, _name = process_upload(request, 'new_model') # Now that we have our file, we will now try to load the model into # memory. # Now that we have our file, is it a zipfile? if not is_savezip_valid(file_path): raise cors_response( request, HTTPBadRequest('Incoming file is not a ' 'valid zipfile!')) resp_msg = 'OK' # now we try to load our model from the zipfile. session_lock = acquire_session_lock(request) log.info(' session lock acquired (sess:{}, thr_id: {})'.format( id(session_lock), current_thread().ident)) try: log.info('loading our model from zip...') init_session_objects(request, force=True) refs = get_session_objects(request) new_model = Model.load(file_path, refs=refs) new_model._cache.enabled = False new_model._schema.register_refs(new_model._schema(), new_model, refs) # from ..views import implemented_types # # RegisterObject(new_model, request, implemented_types) log.info('setting active model...') set_active_model(request, new_model.id) except Exception: raise cors_exception(request, HTTPBadRequest, with_stacktrace=True) finally: session_lock.release() log.info(' session lock released (sess:{}, thr_id: {})'.format( id(session_lock), current_thread().ident)) # We will want to clean up our tempfile when we are done. os.remove(file_path) return cors_response(request, Response(resp_msg))
def update_model(request): ''' Returns Model object in JSON. - This method varies slightly from the common object method in that if we don't specify a model ID, we: - update the current active model if it exists or... - generate a 'Not Found' exception. ''' log_prefix = 'req({0}): update_model():'.format(id(request)) log.info('>>' + log_prefix) ret = None 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) gnome_sema = request.registry.settings['py_gnome_semaphore'] gnome_sema.acquire() log.info(' ' + log_prefix + 'semaphore acquired...') obj_id = obj_id_from_req_payload(json_request) if obj_id: active_model = get_session_object(obj_id, request) else: active_model = get_active_model(request) if active_model: try: if UpdateObject(active_model, json_request, get_session_objects(request)): set_session_object(active_model, request) ret = active_model.serialize() except: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) finally: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') else: gnome_sema.release() log.info(' ' + log_prefix + 'semaphore released...') msg = ("raising cors_exception() in update_model. " "Updating model before it exists.") log.warning(' ' + log_prefix + msg) raise cors_exception(request, HTTPNotFound) log.info('<<' + log_prefix) return ret
def activate_uploaded_model(request): ''' Activates a new model from a zipfile that is stored in the uploads folder, and registers it as the current active model. ''' clean_session_dir(request) zipfile_path, _name = activate_uploaded(request) log.info('Model zipfile: {}'.format(zipfile_path)) # Now that we have our file, we will now try to load the model into # memory. # Now that we have our file, is it a zipfile? if not is_savezip_valid(zipfile_path): raise cors_response(request, HTTPBadRequest('File is not a valid zipfile!')) # now we try to load our model from the zipfile. session_lock = acquire_session_lock(request) log.info(' session lock acquired (sess:{}, thr_id: {})'.format( id(session_lock), current_thread().ident)) try: log.info('Cargando nuestro modelo del zip...') init_session_objects(request, force=True) refs = get_session_objects(request) new_model = Model.load(zipfile_path, refs=refs) new_model._cache.enabled = False new_model._schema.register_refs(new_model._schema(), new_model, refs) # from ..views import implemented_types # RegisterObject(new_model, request, implemented_types) log.info('setting active model...') set_active_model(request, new_model.id) except Exception: raise cors_exception(request, HTTPBadRequest, with_stacktrace=True) finally: session_lock.release() log.info(' session lock released (sess:{}, thr_id: {})'.format( id(session_lock), current_thread().ident)) # We will want to clean up our temporary zipfile when we are done. os.remove(zipfile_path) return cors_response(request, Response('OK'))
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_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 update_map(request): '''Updates a Gnome Map object.''' try: json_request = ujson.loads(request.body.decode('utf-8')) except Exception: raise cors_exception(request, HTTPBadRequest) if not JSONImplementsOneOf(json_request, implemented_types): raise cors_exception(request, HTTPNotImplemented) obj = get_session_object(obj_id_from_req_payload(json_request), request) if obj: try: UpdateObject(obj, json_request, get_session_objects(request)) except Exception: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) else: raise cors_exception(request, HTTPNotFound) set_session_object(obj, request) return obj.serialize(options=web_ser_opts)
def update_map(request): '''Updates a Gnome Map object.''' 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) obj = get_session_object(obj_id_from_req_payload(json_request), request) if obj: try: UpdateObject(obj, json_request, get_session_objects(request)) except: raise cors_exception(request, HTTPUnsupportedMediaType, with_stacktrace=True) else: raise cors_exception(request, HTTPNotFound) set_session_object(obj, request) return obj.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