예제 #1
0
def hello_world(request):
    response = Response(
        'Hello world from Pyramid!\n',
        content_type='text/plain',
    )
    response.encode_content()
    return response
예제 #2
0
파일: client.py 프로젝트: nkabir/trumpet
class ClientView(BaseViewCallable):
    def __init__(self, request):
        super(ClientView, self).__init__(request)
        self.get_main()

    def get_main(self):
        template = 'trumpet:templates/mainview.mako'
        settings = self.get_app_settings()
        basecolor = settings.get('default.css.basecolor', 'white-smoke')
        env = dict(appname='frontdoor', basecolor=basecolor)
        content = render(template, env)
        self.response = Response(body=content)
        self.response.encode_content()
예제 #3
0
class ClientView(BaseView):
    def __init__(self, request):
        super(ClientView, self).__init__(request)
        self.get_main()

    def get_main(self):
        template = 'hubby:templates/mainview.mako'
        settings = self.get_app_settings()
        basecolor = settings.get('default.css.basecolor', 'white-smoke')
        env = dict(appname='frontdoor', basecolor=basecolor)
        content = render(template, env)
        self.response = Response(body=content)
        self.response.encode_content()
예제 #4
0
def nlprp_server(request: Request) -> Response:
    """
    Specimen NLPRP server for Pyramid.

    Args:
        request: the Pyramid :class:`Request` object

    Returns:
        a Pyramid :class:`Response`
    """
    headers = request.headers
    request_is_gzipped = headers.get("Content-Encoding", "") == "gzip"
    client_accepts_gzip = "gzip" in headers.get("Accept-Encoding", "")
    body = request.body
    log.debug(
        f"===========================================\n"
        f"Received request {request!r}:\n"
        f"    headers={dict(headers)!r}\n"
        f"    body={body!r}\n"
        f"    request_is_gzipped={request_is_gzipped}\n"
        f"    client_accepts_gzip={client_accepts_gzip}"
    )
    try:
        nreq = NlprpRequest(data=body, data_is_gzipped=request_is_gzipped)
        log.info(f"NLPRP request is {nreq}")
        assert nreq.valid()
        # Establish command
        log.debug(f"command={nreq.command!r}, args={nreq.args!r}")
    except Exception:
        # Could do more here!
        raise

    # Process NLPRP command
    http_status = HttpStatus.OK
    if nreq.command == "list_processors":
        replydict = cmd_list_processors(request, nreq)
    else:
        raise NotImplementedError()
    assert isinstance(replydict, dict), "Bug in server!"
    reply = NlprpResponse(http_status=http_status, reply_args=replydict)
    log.info(f"Sending NLPRP response: {reply}")

    # Create the HTTP response
    response = Response(reply.data_bytes, status=http_status)
    # Compress the response?
    if client_accepts_gzip:
        response.encode_content("gzip")
    # Done
    log.debug(f"Sending HTTP response: headers={response.headers}, "
              f"body={response.body}")
    return response
예제 #5
0
class LoaderView(BasicView):
    def __init__(self, request):
        super(LoaderView, self).__init__(request)
        subpath = self.request.subpath
        if len(subpath) != 1:
            subpath = '/'.join(subpath)
            self.response = HTTPNotFound('no such resource')
            return
        reqname = subpath[0]
        if not reqname.endswith('.js'):
            self.response = HTTPNotFound('no such resource')
            return
        if reqname in ['cs.js', 'coffee-script.js']:
            asset = 'haberdashery:%s' % os.path.join('stdlib', reqname)
            self.response = static_asset_response(request, asset)
            return
        name = reqname[:-3]
        template = Template(LOADER_TEMPLATE)
        content = template.render(name=name)
        self.response = Response(body=content,
                                 content_type='text/javascript')
        self.response.encode_content()
예제 #6
0
    def __init__(self, request):
        super(WebView, self).__init__(request)
        #resources = self.request.root.static_resources
        #resources.initialize_webview_layout.need()
        #resources.favicon.need()
        #resources.fullcalendar.need()
        #resources.backbone.need()
        #resources.require.need()
        #resources.require.main.need()
        view_name = request.view_name
        print "view_name", view_name
        nextdir = request.subpath[0]
        subpath = os.path.join(*request.subpath)
        print "request.subpath", request.subpath
        #import pdb ; pdb.set_trace()
        if nextdir == 'apples':
            subpath = os.path.join(*request.subpath[1:])
            js = self.mgr.get_js_by_name(subpath)
            print "SUBPATH", subpath
            print "JS", js
            template = 'trumpet:templates/webview-main.mako'
        
            env = dict(datamain='loader')
            
            content = render(template, env)
            self.response = Response(body=content)            
        elif nextdir == 'stdlib':
            asset = 'haberdashery:%s' % '/'.join(request.subpath)
            self.response = static_asset_response(request, asset)
        elif len(request.subpath) > 1 and request.subpath[1] == 'stdlib':
            asset = 'haberdashery:%s' % '/'.join(request.subpath)
            self.response = static_asset_response(request, asset)
        elif request.subpath[-1] == 'loader.js':
            appname = request.subpath[0]
            #js = self.mgr.get_js_by_name(appname)
        
            print "LOADER!!!!!!", appname, request.subpath
            print "WE MADE IT THIS FAR"
            template = Template(LOADER_TEMPLATE)
            #env = dict(loader='stdlib/coffee/config')
            loader = os.path.join('../', 'app', appname)
            print "loader", loader, request.path
            env = dict(loader=loader)
            #import pdb ; pdb.set_trace()
            content = template.render(**env)
            self.response = Response(body=content,
                                content_type='text/javascript')
        elif request.subpath[-1] in ['cs.js', 'coffee-script.js']:
            asset = 'haberdashery:/stdlib/%s' % request.subpath[-1]
            self.response = static_asset_response(request, asset)
        elif nextdir == 'app':
            name = os.path.join(*request.subpath[1:])
            filetype = 'text/html'
            if name.endswith('.js'):
                name = name[:-3]
                filetype = 'text/javascript'
            elif name.endswith('.coffee'):
                name = name[:-7]
                filetype = 'text/coffeescript'
                
            js = self.mgr.get_js_by_name(name)
            response = Response(body=js.content,
                                content_type=filetype)
            response.encode_content()
            self.response = response
        else:
            self.data = dict(datamain=subpath)
            subpath = os.path.join(*request.subpath)
            print "SSSSSSSSSSSSUBPATH", subpath
            template = 'trumpet:templates/webview-main.mako'
            env = dict(datamain='%s/loader' % subpath)
            content = render(template, env)
            self.response = Response(body=content)

        self.context = self.request.context
예제 #7
0
class ClientView(BaseUserViewCallable):
    def __init__(self, request):
        super(ClientView, self).__init__(request)
        self.settings = self.get_app_settings()
        self.data = dict(
            basecolor=self.settings.get('default.css.basecolor', 'vanilla'),
            csspath=self.settings.get('default.css.path',
                                      '/assets/stylesheets'),
            jspath=self.settings.get('default.js.path', '/assets/client'),
        )

    def _make_response(self, settings, appname):
        content = make_app_page(appname, settings, request=self.request)
        self.response = Response(body=content)
        self.response.encode_content()

    def _make_env(self, appname):
        env = dict(appname=appname,
                   basecolor=basecolor,
                   csspath=csspath,
                   jspath=jspath)
        return env

    @view_config(route_name='home')
    def index(self):
        self.data['appname'] = self.settings.get('default.js.mainapp', 'index')
        return self.data

    @view_config(route_name='admin')
    def admin(self):
        self.data['appname'] = self.settings.get('default.js.admin_app',
                                                 'admin')
        return self.data

    def handle_get(self):
        request = self.request
        view = request.view_name
        subpath = request.subpath
        settings = self.get_app_settings()
        if not view:
            route = request.matched_route.name
            if route == 'home':
                self.get_main()
                return
            elif route == 'apps':
                self.get_main(appname=request.matchdict['appname'])
                return
            else:
                raise HTTPNotFound("no such animal")
        elif view == 'admin':
            appname = settings.get('default.js.admin_app', 'admin')
            basecolor = settings.get('default.admin.basecolor', 'DarkSeaGreen')
            self.get_main(appname=appname, basecolor=basecolor)
        else:
            raise HTTPNotFound('no way')

    def get_main(self, appname=None, basecolor=None):
        settings = self.get_app_settings()
        if appname is None:
            appname = settings.get('default.js.mainapp', 'frontdoor')
        content = make_app_page(appname,
                                settings,
                                basecolor=basecolor,
                                request=self.request)
        self.response = Response(body=content)
        self.response.encode_content()