示例#1
0
async def coadd():
    ''' 
    '''
    release = request.args.get('release', 'dr16')
    run2d = request.args.get('run2d', None)
    plate = int(request.args.get('plate', None))
    mjd = int(request.args.get('mjd', None))
    fiber = int(request.args.get('fiber', 1))
    sample = int(request.args.get('sample', 1))
    bands = request.args.get('bands', 'all')
    debug_flag = request.args.get('debug', '')

    survey = 'sdss' if not run2d.startswith('v') else 'eboss'

    root = '/ssd0/sdss/%s/%s/spectro/redux/' % (release,survey)
    path = root + '%s/%04i/' % (run2d, plate)
    fname = path + 'spec-%04i-%05i-%04i.npy' %  (plate,mjd,fiber)

    print(fname)
    if bands == 'all':
        with open(fname, 'r') as fd:
            _bytes = fd.read()
        return web.response(body=_bytes)
    else:
        data = np.load(str(fname))
        dbands = data[[c for c in list(data.dtype.names) if c in bands]]
        dbands = rfn.repack_fields(dbands)

        tmp_file = NamedTemporaryFile(delete=False, dir='/tmp').name
        np.save(tmp_file, dbands, allow_pickle=False)
        with open(tmp_file+'.npy', 'r') as fd:
            _bytes = fd.read()
        os.unlink(tmp_file)
        return web.response(body=_bytes)
    async def delete(self):
        pk = self.request.match_info.get('pk')

        async with self.request.app['pg_engine'].acquire() as conn:
            result = await conn.execute(tbl_message.select(tbl_message.c.id == pk))
            row = await result.first()
            if not row:
                return response({'text': 'Message not found'}, status=status.HTTP_404_NOT_FOUND)

            await conn.execute(tbl_message.delete(tbl_message.c.id == pk))
            return response({'text': f'Message with id {pk} was deleted.',
                             'redirect_url': str(self.request.app.router['message_list'].url_for())},
                            status=status.HTTP_204_NO_CONTENT)
    async def get(self):
        pk = self.request.match_info.get('pk')

        async with self.request.app['pg_engine'].acquire() as conn:
            result = await conn.execute(tbl_message.select(tbl_message.c.id == pk))
            row = await result.fetchone()
            log.debug(row)
            if not row:
                return response({'text': 'Message not found'}, status=status.HTTP_404_NOT_FOUND)

        return response({
            'title': f'{self.TITLE}: {row.id}',
            'object': {'username': row.username,
                       'timestamp': '{:%Y-%m-%d %H:%M:%S}'.format(row.timestamp),
                       'message': row.message}})
 async def response(request):
     logging.info('Response handler')
     r = await handler(request)  #handler 返回request的类型
     if isinstance(r,web.StreamResponse):
         return r
     if isinstance(r,bytes):
         resp = web.Response(body =r)
         resp.content_type = 'application/octet-stream'
         return resp
     if isinstance(r,str):
         if r.startswith('redirect:'):
             return web.HTTPFound(r[9:])
         resp = web.response(body=r.encode('utd-8'))
         resp.content_type = 'text/html;charset=utf-8'
         return resp 
     if isinstance(r,dict):
         template = r.get('__template__')
         if template is None:
             resp = web.Response(body = json.dump(r,ensure_ascii = False, default = lambda o:o.__dict__).encode('utf-8'))
             resp.content_type = 'application/json;charset =utf-8'
             return resp 
         else:
             resp = web.Response(body = app['__templating__'].get_templacte(template).render(**r).encode('utf-8'))
             resp.content_type ='text/html;charset=utf-8'
             return resp
     if isinstance(r,int) and r>=100 and r<600:
         return web.Response(r)
     if isinstance(r,tuple) and len(r)==2:
         t,m=r 
         if isinstance(t,int) and t>=100 and t < 600:
             return web.Response(t,str(m))
             
     resp = web.Response(body = str(r).encode('utf-8'))
     resp.content_type = 'text/plain;charset=utf-8'
     return resp 
示例#5
0
def index(request):

    # if not render it out and set the cache
    text = 'The index'
    # get the piopeline
    # return response
    return web.response(body=text.encode('utf-8'))
示例#6
0
 def response(request):
     logging.info("\tResponse handle...%s..." % request)
     r = yield from handler(request)
     logging.info('response_factory: get handlers response OK here')
     logging.info('type(response): %s' % type(r))
     if isinstance(r, web.StreamResponse):
         return r
     if isinstance(r, bytes):
         resp = web.Response(body=r)
         resp.content_type = 'application/octet-stream'
         return resp
     if isinstance(r, str):
         if r.startswith('redirect:'):
             return web.HTTPFound(r[9:])
         resp = web.Response(body=str(r).encode('utf-8'))
         resp.content_type = 'text/html;charset=utf-8'
         return resp
     if isinstance(r, dict):
         template = r.get('__template__')
         if template is None:
             resp = web.Response(body=json.dumps(
                 r, ensure_ascii=False,
                 default=lambda o: o.__dict__).encode('utf-8'))
             resp.content_type = 'application/json;charset=utf-8'
             return resp
         else:
             r['__user__'] = request.__user__
             resp = web.Response(
                 body=app['__templating__'].get_template(template).render(
                     **r).encode('utf-8'))
             resp.content_type = 'text/html;charset=utf-8'
             return resp
     if isinstance(
             r, int) and r >= 100 and r < 600:  # need check r or anther t
         return web.response(r)
     if isinstance(r, tuple) and len(r) == 2:
         t, m = r
         if isinstance(t, int) and t >= 100 and t < 600:
             return web.response(t, str(m))
     # default
     logging.info("default...")
     resp = web.Response(body=str(r).encode('utf-8'))
     resp.content_type = 'text/pain;charset=utf-8'
     return resp
示例#7
0
文件: app.py 项目: tuouo/PythonWeb
 def response(request):
     logging.info("\tResponse handle...%s..." % request)
     r = yield from handler(request)
     logging.info('response_factory: get handlers response OK here')
     logging.info('type(response): %s' % type(r))
     if isinstance(r, web.StreamResponse):
         return r
     if isinstance(r, bytes):
         resp = web.Response(body = r)
         resp.content_type = 'application/octet-stream'
         return resp
     if isinstance(r, str):
         if r.startswith('redirect:'):
             return web.HTTPFound(r[9:])
         resp = web.Response(body = str(r).encode('utf-8'))
         resp.content_type = 'text/html;charset=utf-8'
         return resp
     if isinstance(r, dict):
         template = r.get('__template__')
         if template is None:
             resp = web.Response(body = json.dumps(r, ensure_ascii=False, default=lambda o:o.__dict__).encode('utf-8'))
             resp.content_type = 'application/json;charset=utf-8'
             return resp
         else:
             r['__user__'] = request.__user__
             resp = web.Response(body = app['__templating__'].get_template(template).render(**r).encode('utf-8'))
             resp.content_type = 'text/html;charset=utf-8'
             return resp
     if isinstance(r, int) and r >= 100 and r < 600:		# need check r or anther t
         return web.response(r)
     if isinstance(r, tuple) and len(r) == 2:
         t, m = r
         if isinstance(t, int) and t >= 100 and t < 600:
              return web.response(t, str(m))
     # default
     logging.info("default...")
     resp = web.Response(body = str(r).encode('utf-8'))
     resp.content_type = 'text/pain;charset=utf-8'
     return resp
    async def get(self):
        msgs = []

        async with self.request.app['pg_engine'].acquire() as conn:
            async for row in conn.execute(tbl_message.select().order_by(
                    tbl_message.c.timestamp.desc())):
                ts = '{:%Y-%m-%d %H:%M:%S}'.format(row.timestamp)
                msgs.append({'id': row.id,
                             'username': row.username,
                             'timestamp': ts})

        return response({
            'title': self.TITLE,
            'object_list': msgs})
    async def post(self):
        new_message, missing_fields = {}, []
        fields = ['username', 'message']
        data = await self.request.post()
        log.debug(data)
        for f in fields:
            new_message[f] = data.get(f)
            if not new_message[f]:
                missing_fields.append(f)

        if missing_fields:
            return response({'text': 'Invalid form submission, missing fields: {}'.format(
                ', '.join(missing_fields))},
                status=status.HTTP_400_BAD_REQUEST)

        async with self.request.app['pg_engine'].acquire() as conn:
            await conn.execute(tbl_message.insert().values(
                username=new_message['username'],
                message=new_message['message'],
            ))

        return response({'text': 'Message is successfully created.',
                         'redirect_url': str(self.request.app.router['message_list'].url_for())},
                        status=status.HTTP_201_CREATED)
 async def get(self):
     return response({
         'title': self.TITLE,
         'text': 'Hello World'})
示例#11
0
async def junk12k():
    with open('/home/fitz/specdal/bench/junk12K', 'r') as fd:
        _bytes = fd.read()
    return web.response(body=_bytes)