Ejemplo n.º 1
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    try:
        if not obj_id:
            my_model = get_active_model(request)
            if my_model:
                ret = my_model.serialize()
            else:
                ret = get_specifications(request, implemented_types)
        else:
            obj = get_session_object(obj_id, request)
            if obj:
                if ObjectImplementsOneOf(obj, implemented_types):
                    set_active_model(request, obj.id)
                    ret = obj.serialize()
                else:
                    # we refer to an object, but it is not a Model
                    raise cors_exception(request, HTTPBadRequest)
            else:
                raise cors_exception(request, HTTPNotFound)
    finally:
        gnome_sema.release()

    return ret
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    print 'rewinding', request.session.session_id
    active_model = get_active_model(request)
    ns = sess_namespaces.get(request.session.session_id, None)
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'
                 .format(id(session_lock), current_thread().ident))

        try:
            if (ns and ns.active_greenlet):
                ns.active_greenlet.kill(block=False)
                ns.num_sent = 0
            active_model.rewind()
        except Exception:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 5
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    try:
        if not obj_id:
            my_model = get_active_model(request)
            if my_model:
                ret = my_model.serialize()
            else:
                ret = get_specifications(request, implemented_types)
        else:
            obj = get_session_object(obj_id, request)
            if obj:
                if ObjectImplementsOneOf(obj, implemented_types):
                    set_active_model(request, obj.id)
                    ret = obj.serialize()
                else:
                    # we refer to an object, but it is not a Model
                    raise cors_exception(request, HTTPBadRequest)
            else:
                raise cors_exception(request, HTTPNotFound)
    finally:
        gnome_sema.release()

    return ret
Ejemplo n.º 6
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    print('rewinding', request.session.session_id)
    active_model = get_active_model(request)
    ns = request.registry.get('sio_ns')
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'.format(
            id(session_lock),
            current_thread().ident))

        try:
            if ns:
                sio = ns.get_sockid_from_sessid(request.session.session_id)
                if (ns.active_greenlets.get(sio)):
                    with ns.session(sio) as sock_session:
                        ns.active_greenlets.get(sio).kill(block=False)
                        sock_session['num_sent'] = 0
            active_model.rewind()
        except Exception:
            raise cors_exception(request,
                                 HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'.format(
                id(session_lock),
                current_thread().ident))
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    log.info('location_api.cors_origins_for("get") = {0}'
             .format(location_api.cors_origins_for('get')))

    # first, lets just query that we can get to the data
    locations_dir = request.registry.settings['locations_dir']
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, _dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(ujson.load(open(join(path, f), 'r')))
             for f in filenames
             if f == 'compiled.json']

            [location_file_dirs.append(join(path, basename(path) + '_save'))
             for f in filenames
             if f == 'compiled.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            session_lock = acquire_session_lock(request)
            log.info('  session lock acquired (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))

            try:
                location_file = location_file_dirs[matching[0][0]]
                log.info('load location: {0}'.format(location_file))
                load_location_file(location_file, request)
            except Exception:
                raise cors_exception(request, HTTPInternalServerError,
                                     with_stacktrace=True)
            finally:
                session_lock.release()
                log.info('  session lock released (sess:{}, thr_id: {})'
                         .format(id(session_lock), current_thread().ident))

            return matching[0][1]
        else:
            raise cors_exception(request, HTTPNotFound)
    else:
        features = [Feature(geometry=Point(c['geometry']['coordinates']),
                            properties={'title': c['name'],
                                        'slug': slugify.slugify_url(c['name']),
                                        'content': c['steps']
                                        }
                            )
                    for c in location_content]
        return FeatureCollection(features)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
def get_geojson(request, implemented_types):
    '''Returns the GeoJson for a Gnome Map object.'''
    obj = get_session_object(obj_id_from_url(request), request)

    if obj:
        if ObjectImplementsOneOf(obj, implemented_types):
            return obj.to_geojson()
        else:
            raise cors_exception(request, HTTPNotImplemented)
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 12
0
def get_geojson(request, implemented_types):
    '''Returns the GeoJson for a Gnome Map object.'''
    obj = get_session_object(obj_id_from_url(request), request)

    if obj:
        if ObjectImplementsOneOf(obj, implemented_types):
            return obj.to_geojson()
        else:
            raise cors_exception(request, HTTPNotImplemented)
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 13
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    log.info('location_api.cors_origins_for("get") = {0}'
             .format(location_api.cors_origins_for('get')))

    # first, lets just query that we can get to the data
    locations_dir = request.registry.settings['locations_dir']
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, _dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(ujson.load(open(join(path, f), 'r')))
             for f in filenames
             if f == 'compiled.json']

            [location_file_dirs.append(path + "/" + basename(path) + '_save')
             for f in filenames
             if f == 'compiled.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            gnome_sema = request.registry.settings['py_gnome_semaphore']
            gnome_sema.acquire()
            try:
                location_file = location_file_dirs[matching[0][0]]
                log.info('load location: {0}'.format(location_file))
                load_location_file(location_file, request)
            except:
                raise cors_exception(request, HTTPInternalServerError,
                                     with_stacktrace=True)
            finally:
                gnome_sema.release()

            return matching[0][1]
        else:
            raise cors_exception(request, HTTPNotFound)
    else:
        features = [Feature(geometry=Point(c['geometry']['coordinates']),
                            properties={'title': c['name'],
                                        'slug': slugify.slugify_url(c['name']),
                                        'content': c['steps']
                                        }
                            )
                    for c in location_content]
        return FeatureCollection(features)
Ejemplo n.º 14
0
def get_raster(request):
    '''
        Outputs the map's raster in binary format
    '''
    log_prefix = 'req({0}): get_raster():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None:
            raster = obj.raster.copy()
            #transpose for client
            bbox = obj.land_polys.bounding_box.AsPoly().reshape(-1).tolist()
            return zlib.compress(np.ascontiguousarray(
                raster.T).tobytes()), raster.T.shape, bbox
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 15
0
def get_lines(request):
    '''
    Outputs the object's grid lines in binary format
    '''
    log_prefix = 'req({0}): get_grid():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None:
            lengths, lines = obj.get_lines()
            lines_bytes = b''.join([l.tobytes() for l in lines])

            return (zlib.compress(lengths.tobytes() + lines_bytes),
                    len(lengths))
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 16
0
def get_centers(request):
    '''
        Outputs GNOME grid centers for a particular obj
    '''

    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None:
            centers = obj.get_centers()
            return zlib.compress(centers.astype(np.float32).tobytes())
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 17
0
def get_grid(request):
    '''
        Outputs the object's grid cells in binary format
    '''
    log_prefix = 'req({0}): get_grid():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None and isinstance(obj, (GridCurrent, GridWind)):
            cells = obj.grid.get_cells()

            return zlib.compress(cells.astype(np.float32).tobytes())
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 18
0
def get_grid_centers(request):
    '''
        Outputs GNOME grid centers for a particular mover
    '''
    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('  {0} {1}'.format(log_prefix, 'semaphore acquired...'))

    try:
        obj_id = request.matchdict.get('obj_id')[0]
        mover = get_session_object(obj_id, request)

        if mover is not None:
            
            if isinstance(mover, CurrentMoversBase):
                # signature = get_grid_signature(mover)
                centers = get_center_points(mover)

            return centers.tolist()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        gnome_sema.release()
        log.info('  ' + log_prefix + 'semaphore released...')

    log.info('<<' + log_prefix)
Ejemplo n.º 19
0
def get_grid_centers(request):
    '''
        Outputs GNOME grid centers for a particular mover
    '''
    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('  {0} {1}'.format(log_prefix, 'semaphore acquired...'))

    try:
        obj_id = request.matchdict.get('obj_id')[0]
        mover = get_session_object(obj_id, request)

        if mover is not None:

            if isinstance(mover, CurrentMoversBase):
                # signature = get_grid_signature(mover)
                centers = get_center_points(mover)

            return centers.tolist()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        gnome_sema.release()
        log.info('  ' + log_prefix + 'semaphore released...')

    log.info('<<' + log_prefix)
Ejemplo n.º 20
0
def get_vector_data(request):
    log_prefix = 'req({0}): get_grid():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'.format(
        log_prefix, id(session_lock),
        current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)

        if obj is not None and isinstance(obj, (GridCurrent, GridWind)):
            vec_data = obj.get_data_vectors()

            return zlib.compress(vec_data.tobytes()), vec_data.shape
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 21
0
def get_help(request):
    '''Get the requested help file if it exists'''
    help_dir = get_help_dir_from_config(request)
    requested_dir = (urllib.unquote(sep.join(
        request.matchdict['dir'])).encode('utf8'))
    requested_file = join(help_dir, requested_dir)

    if isfile(requested_file + '.rst'):
        # a single help file was requested
        html = ''
        with open(requested_file + '.rst', 'r') as f:
            html = publish_parts(f.read(), writer_name='html')['html_body']

        return {'path': requested_file, 'html': html}
    elif isdir(requested_file) and requested_dir is not '':
        # a directory was requested
        # aggregate the files contained with in the given directory
        # and sub dirs.
        for path, _dirnames, filenames in walk(requested_file):
            filenames.sort()

            html = ''

            for fname in filenames:
                with open(join(path, fname), 'r') as f:
                    html += publish_parts(f.read(),
                                          writer_name='html')['html_body']

            return {'path': requested_file, 'html': html}
    elif isdir(requested_file) and requested_dir is '':
        aggregate = []
        for path, _dirnames, filenames in walk(requested_file):
            filenames.sort()

            # exclude location file user guides
            if path.count(join('model', 'locations')) == 0:
                for fname in filenames:
                    text = ''
                    with open(join(path, fname), 'r') as f:
                        text = f.read()

                    parts_whole = publish_parts(text)
                    parts = publish_parts(text, writer_name='html')

                    html = parts['html_body']
                    keywords = iter_keywords(parts_whole['whole'])

                    aggregate.append({
                        'path':
                        join(path, fname.replace('.rst', '')),
                        'html':
                        html,
                        'keywords':
                        keywords
                    })

        return aggregate
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 22
0
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)
Ejemplo n.º 23
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    active_model = get_active_model(request)
    if active_model:
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()

        try:
            active_model.rewind()
        except:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            gnome_sema.release()
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 24
0
def get_rewind(request):
    '''
        rewinds the current active Model.
    '''
    active_model = get_active_model(request)
    if active_model:
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()

        try:
            active_model.rewind()
        except:
            raise cors_exception(request,
                                 HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            gnome_sema.release()
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 25
0
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()
Ejemplo n.º 26
0
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()
Ejemplo n.º 27
0
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)
Ejemplo n.º 28
0
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()
Ejemplo n.º 29
0
def get_help(request):
    '''Get the requested help file if it exists'''
    help_dir = get_help_dir_from_config(request)
    requested_dir = (urllib.unquote(sep.join(request.matchdict['dir']))
                     .encode('utf8'))
    requested_file = join(help_dir, requested_dir)

    if isfile(requested_file + '.rst'):
        # a single help file was requested
        html = ''
        with open(requested_file + '.rst', 'r') as f:
            html = publish_parts(f.read(), writer_name='html')['html_body']

        return {'path': requested_file, 'html': html}
    elif isdir(requested_file) and requested_dir is not '':
        # a directory was requested
        # aggregate the files contained with in the given directory
        # and sub dirs.
        for path, dirnames, filenames in walk(requested_file):
            html = ''
            for fname in filenames:
                with open(join(path, fname), 'r') as f:
                    html += publish_parts(f.read(),
                                          writer_name='html')['html_body']

            return {'path': requested_file, 'html': html}
    elif isdir(requested_file) and requested_dir is '':
        aggregate = []

        for path, dirnames, filenames in walk(requested_file):
            for fname in filenames:
                text = ''
                with open(join(path, fname), 'r') as f:
                    text = f.read()

                parts_whole = publish_parts(text)
                parts = publish_parts(text, writer_name='html')

                html = parts['html_body']
                keywords = iter_keywords(parts_whole['whole'])

                aggregate.append({'path': join(path,
                                               fname.replace('.rst', '')),
                                  'html': html,
                                  'keywords': keywords})

        return aggregate
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 30
0
def create_help_feedback(request):
    '''Creates a feedback entry for the given help section'''
    try:
        json_request = ujson.loads(request.body)
    except:
        raise cors_exception(request, HTTPBadRequest)

    json_request['ts'] = int(time.time())
    client = redis.Redis('localhost')

    if 'index' not in json_request:
        json_request['index'] = client.incr('index')

    client.set('feedback' + str(json_request['index']), json_request)

    return json_request
Ejemplo n.º 31
0
def create_help_feedback(request):
    '''Creates a feedback entry for the given help section'''
    try:
        json_request = ujson.loads(request.body)
    except:
        raise cors_exception(request, HTTPBadRequest)

    json_request['ts'] = int(time.time())
    client = redis.Redis('localhost')

    if 'index' not in json_request:
        json_request['index'] = client.incr('index')

    client.set('feedback' + str(json_request['index']), json_request)

    return json_request
Ejemplo n.º 32
0
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 = 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!'))

    # now we try to load our model from the zipfile.
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('semaphore acquired.')
    try:
        log.info('loading our model from zip...')
        new_model = load(file_path)
        new_model._cache.enabled = False

        init_session_objects(request, force=True)

        RegisterObject(new_model, request)

        log.info('setting active model...')
        set_active_model(request, new_model.id)
    except:
        raise cors_exception(request, HTTPBadRequest, with_stacktrace=True)
    finally:
        gnome_sema.release()
        log.info('semaphore released.')

    # We will want to clean up our tempfile when we are done.
    os.remove(file_path)

    return cors_response(request, Response('OK'))
Ejemplo n.º 33
0
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 = 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!'))

    # now we try to load our model from the zipfile.
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('semaphore acquired.')
    try:
        log.info('loading our model from zip...')
        new_model = load(file_path)
        new_model._cache.enabled = False

        init_session_objects(request, force=True)

        RegisterObject(new_model, request)

        log.info('setting active model...')
        set_active_model(request, new_model.id)
    except:
        raise cors_exception(request, HTTPBadRequest, with_stacktrace=True)
    finally:
        gnome_sema.release()
        log.info('semaphore released.')

    # We will want to clean up our tempfile when we are done.
    os.remove(file_path)

    return cors_response(request, Response('OK'))
Ejemplo n.º 34
0
def get_metadata(request):
    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    session_lock = acquire_session_lock(request)
    log.info('  {} session lock acquired (sess:{}, thr_id: {})'
             .format(log_prefix, id(session_lock), current_thread().ident))
    try:
        obj_id = request.matchdict.get('obj_id')[0]
        obj = get_session_object(obj_id, request)
        if obj is not None:
            return obj.get_metadata()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    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)
Ejemplo n.º 35
0
def create_help_feedback(request):
    '''Creates a feedback entry for the given help section'''
    try:
        json_request = ujson.loads(request.body)
    except:
        raise cors_exception(request, HTTPBadRequest)

    json_request['ts'] = int(time.time())

    # find redis using redis sessions config
    rhost = request.registry.settings.get('redis.help.host', 'localhost')
    rport = request.registry.settings.get('redis.help.port', 6379)

    client = redis.Redis(host=rhost, port=rport)

    if 'index' not in json_request:
        json_request['index'] = client.incr('index')

    client.set('feedback' + str(json_request['index']), json_request)

    return json_request
Ejemplo n.º 36
0
def create_help_feedback(request):
    '''Creates a feedback entry for the given help section'''
    try:
        json_request = ujson.loads(request.body)
    except:
        raise cors_exception(request, HTTPBadRequest)

    json_request['ts'] = int(time.time())

    # find redis using redis sessions config
    rhost = request.registry.settings.get('redis.help.host', 'localhost')
    rport = request.registry.settings.get('redis.help.port', 6379)

    client = redis.Redis(host=rhost, port=rport)

    if 'index' not in json_request:
        json_request['index'] = client.incr('index')

    client.set('feedback' + str(json_request['index']), json_request)

    return json_request
Ejemplo n.º 37
0
def get_current_info(request):
    '''
        Outputs GNOME current information for a particular current mover
        in a geojson format.
        The output is a collection of Features.
        The Features contain a MultiPolygon
    '''
    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('  {0} {1}'.format(log_prefix, 'semaphore acquired...'))

    try:
        obj_id = request.matchdict.get('obj_id')[0]
        mover = get_session_object(obj_id, request)

        if mover is not None:
            # start = active_model.start_time
            # start_seconds = time_utils.date_to_sec(start)
            # num_hours = active_model.duration.total_seconds() / 60 / 60
            #
            # times = [start_seconds + 3600. * dt
            #          for dt in range(int(num_hours))]

            if isinstance(mover, CurrentMoversBase):
                # signature = get_grid_signature(mover)
                cells = get_cells(mover)

            return cells.reshape(-1,
                                 cells.shape[-1] * cells.shape[-2]).tolist()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        gnome_sema.release()
        log.info('  ' + log_prefix + 'semaphore released...')

    log.info('<<' + log_prefix)
Ejemplo n.º 38
0
def get_current_info(request):
    '''
        Outputs GNOME current information for a particular current mover
        in a geojson format.
        The output is a collection of Features.
        The Features contain a MultiPolygon
    '''
    log_prefix = 'req({0}): get_current_info():'.format(id(request))
    log.info('>>' + log_prefix)

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    log.info('  {0} {1}'.format(log_prefix, 'semaphore acquired...'))

    try:
        obj_id = request.matchdict.get('obj_id')[0]
        mover = get_session_object(obj_id, request)

        if mover is not None:
            # start = active_model.start_time
            # start_seconds = time_utils.date_to_sec(start)
            # num_hours = active_model.duration.total_seconds() / 60 / 60
            #
            # times = [start_seconds + 3600. * dt
            #          for dt in range(int(num_hours))]

            if isinstance(mover, CurrentMoversBase):
                # signature = get_grid_signature(mover)
                cells = get_cells(mover)

            return cells.reshape(-1, cells.shape[-1]*cells.shape[-2]).tolist()
        else:
            exc = cors_exception(request, HTTPNotFound)
            raise exc
    finally:
        gnome_sema.release()
        log.info('  ' + log_prefix + 'semaphore released...')

    log.info('<<' + log_prefix)
Ejemplo n.º 39
0
def get_step(request):
    '''
        Generates and returns an image corresponding to the step.
    '''
    log_prefix = 'req({0}): get_step():'.format(id(request))
    log.info('>>' + log_prefix)

    active_model = get_active_model(request)
    if active_model:
        # generate the next step in the sequence.
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()
        log.info('  ' + log_prefix + 'semaphore acquired...')

        try:
            if active_model.current_time_step == -1:
                # our first step, establish uncertain models
                drop_uncertain_models(request)

                log.info('\thas_weathering_uncertainty {0}'.
                         format(active_model.has_weathering_uncertainty))
                if active_model.has_weathering_uncertainty:
                    set_uncertain_models(request)
                else:
                    log.info('Model does not have weathering uncertainty')

            begin = time.time()
            output = active_model.step()

            begin_uncertain = time.time()
            steps = get_uncertain_steps(request)
            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].iteritems():
                        aggregate[k].append(v)

                for k, v in aggregate.iteritems():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin

        except StopIteration:
            log.info('  ' + log_prefix + 'stop iteration exception...')
            drop_uncertain_models(request)
            raise cors_exception(request, HTTPNotFound)
        except:
            log.info('  ' + log_prefix + 'unknown exception...')
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            gnome_sema.release()
            log.info('  ' + log_prefix + 'semaphore released...')

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 40
0
def get_full_run(request):
    '''
        Performs a full run of the current active Model, turning off any
        response options.
        Returns the final step results.
    '''
    active_model = get_active_model(request)
    if active_model:
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()

        try:
            weatherer_enabled_flags = [w.on for w in active_model.weatherers]

            for w in active_model.weatherers:
                if isinstance(w, (Skimmer, Burn, ChemicalDispersion)):
                    w.on = False

            active_model.rewind()

            drop_uncertain_models(request)

            if active_model.has_weathering_uncertainty:
                log.info('Model has weathering uncertainty')
                set_uncertain_models(request)
            else:
                log.info('Model does not have weathering uncertainty')

            begin = time.time()

            for step in active_model:
                output = step
                steps = get_uncertain_steps(request)

            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].iteritems():
                        aggregate[k].append(v)

                for k, v in aggregate.iteritems():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
        except:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            for a, w in zip(weatherer_enabled_flags, active_model.weatherers):
                w.on = a
            gnome_sema.release()

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 41
0
def get_full_run(request):
    '''
        Performs a full run of the current active Model, turning off any
        response options.
        Returns the final step results.
    '''
    active_model = get_active_model(request)
    if active_model:
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()

        try:
            weatherer_enabled_flags = [w.on for w in active_model.weatherers]

            for w in active_model.weatherers:
                if isinstance(w, (Skimmer, Burn, ChemicalDispersion)):
                    w.on = False

            active_model.rewind()

            drop_uncertain_models(request)

            if active_model.has_weathering_uncertainty:
                log.info('Model has weathering uncertainty')
                set_uncertain_models(request)
            else:
                log.info('Model does not have weathering uncertainty')

            begin = time.time()

            for step in active_model:
                output = step
                steps = get_uncertain_steps(request)

            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].iteritems():
                        aggregate[k].append(v)

                for k, v in aggregate.iteritems():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {
                    'time_stamp': nominal['time_stamp'],
                    'nominal': nominal,
                    'low': low,
                    'high': high
                }
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {
                    'time_stamp': nominal['time_stamp'],
                    'nominal': nominal,
                    'low': None,
                    'high': None
                }
                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
        except:
            raise cors_exception(request,
                                 HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            for a, w in zip(weatherer_enabled_flags, active_model.weatherers):
                w.on = a
            gnome_sema.release()

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 42
0
def get_step(request):
    '''
        Generates and returns an image corresponding to the step.
    '''
    log_prefix = 'req({0}): get_step():'.format(id(request))
    log.info('>>' + log_prefix)

    active_model = get_active_model(request)
    if active_model:
        # generate the next step in the sequence.
        gnome_sema = request.registry.settings['py_gnome_semaphore']
        gnome_sema.acquire()
        log.info('  ' + log_prefix + 'semaphore acquired...')

        try:
            if active_model.current_time_step == -1:
                # our first step, establish uncertain models
                drop_uncertain_models(request)

                log.info('\thas_weathering_uncertainty {0}'.format(
                    active_model.has_weathering_uncertainty))
                if active_model.has_weathering_uncertainty:
                    set_uncertain_models(request)
                else:
                    log.info('Model does not have weathering uncertainty')

            begin = time.time()
            output = active_model.step()

            begin_uncertain = time.time()
            steps = get_uncertain_steps(request)
            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].iteritems():
                        aggregate[k].append(v)

                for k, v in aggregate.iteritems():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {
                    'time_stamp': nominal['time_stamp'],
                    'nominal': nominal,
                    'low': low,
                    'high': high
                }
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {
                    'time_stamp': nominal['time_stamp'],
                    'nominal': nominal,
                    'low': None,
                    'high': None
                }
                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin

        except StopIteration:
            log.info('  ' + log_prefix + 'stop iteration exception...')
            drop_uncertain_models(request)
            raise cors_exception(request, HTTPNotFound)
        except:
            log.info('  ' + log_prefix + 'unknown exception...')
            raise cors_exception(request,
                                 HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            gnome_sema.release()
            log.info('  ' + log_prefix + 'semaphore released...')

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 43
0
def get_full_run(request):
    '''
        Performs a full run of the current active Model, turning off any
        response options.
        Returns the final step results.
    '''
    log_prefix = 'req({0}): get_full_run():'.format(id(request))
    log.info('>>' + log_prefix)

    response_on = request.json_body['response_on']

    active_model = get_active_model(request)
    if active_model:
        session_lock = acquire_session_lock(request)
        log.info('  session lock acquired (sess:{}, thr_id: {})'
                 .format(id(session_lock), current_thread().ident))

        try:
            weatherer_enabled_flags = [w.on for w in active_model.weatherers]

            if response_on is False:
                for w in active_model.weatherers:
                    if isinstance(w, (Skimmer, Burn, ChemicalDispersion)):
                        w.on = False

            active_model.rewind()

            drop_uncertain_models(request)

            if active_model.has_weathering_uncertainty:
                log.info('Model has weathering uncertainty')
                set_uncertain_models(request)
            else:
                log.info('Model does not have weathering uncertainty')

            begin = time.time()

            for step in active_model:
                output = step
                steps = get_uncertain_steps(request)

            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    for k, v in step_output['WeatheringOutput'].items():
                        aggregate[k].append(v)

                for k, v in aggregate.items():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['total_response_time'] = end - begin

            active_model.rewind()
        except Exception:
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            for a, w in zip(weatherer_enabled_flags, active_model.weatherers):
                w.on = a
            session_lock.release()
            log.info('  session lock released (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))

        log.info('<<' + log_prefix)
        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed)
Ejemplo n.º 44
0
def get_step(request):
    '''
        Generates and returns an image corresponding to the step.
    '''
    log_prefix = 'req({0}): get_step():'.format(id(request))
    log.info('>>' + log_prefix)

    active_model = get_active_model(request)
    if active_model:
        # generate the next step in the sequence.
        session_lock = acquire_session_lock(request)
        log.info('  {} session lock acquired (sess:{}, thr_id: {})'
                 .format(log_prefix, id(session_lock), current_thread().ident))

        try:
            if active_model.current_time_step == -1:
                # our first step, establish uncertain models
                drop_uncertain_models(request)

                log.info('\thas_weathering_uncertainty {0}'.
                         format(active_model.has_weathering_uncertainty))
                if active_model.has_weathering_uncertainty:
                    set_uncertain_models(request)
                else:
                    log.info('Model does not have weathering uncertainty')

            begin = time.time()
            output = active_model.step()

            begin_uncertain = time.time()
            steps = get_uncertain_steps(request)
            end = time.time()

            if steps and 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                aggregate = defaultdict(list)
                low = {}
                high = {}
                full_output = {}

                for idx, step_output in enumerate(steps):
                    # step_output could contain an exception from one
                    # of our uncertainty worker processes.  If so, then
                    # we should propagate the exception with its original
                    # context.
                    if (isinstance(step_output, tuple) and
                            len(step_output) >= 3 and
                            isinstance(step_output[1], Exception)):
                        raise step_output[1].with_traceback(step_output[2])

                    for k, v in step_output['WeatheringOutput'].items():
                        aggregate[k].append(v)

                for k, v in aggregate.items():
                    low[k] = min(v)
                    high[k] = max(v)

                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': low,
                               'high': high}
                for idx, step_output in enumerate(steps):
                    full_output[idx] = step_output['WeatheringOutput']

                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin
            elif 'WeatheringOutput' in output:
                nominal = output['WeatheringOutput']
                full_output = {'time_stamp': nominal['time_stamp'],
                               'nominal': nominal,
                               'low': None,
                               'high': None}
                output['WeatheringOutput'] = full_output
                output['uncertain_response_time'] = end - begin_uncertain
                output['total_response_time'] = end - begin

        except StopIteration:
            log.info('  ' + log_prefix + 'stop iteration exception...')
            drop_uncertain_models(request)
            raise cors_exception(request, HTTPNotFound)
        except Exception:
            log.info('  ' + log_prefix + 'unknown exception...')
            raise cors_exception(request, HTTPUnprocessableEntity,
                                 with_stacktrace=True)
        finally:
            session_lock.release()
            log.info('  {} session lock released (sess:{}, thr_id: {})'
                     .format(log_prefix, id(session_lock),
                             current_thread().ident))

        return output
    else:
        raise cors_exception(request, HTTPPreconditionFailed,
                             explanation=(b'Your session timed out - the model is no longer active'))