def normalize_settings( cls, sett ):
     """:meth:`pluggdapps.plugin.ISettings.normalize_settings` interface
     method.
     """
     x = sett['routemapper'].strip() 
     sett['routemapper'] = h.abspath_from_asset_spec(x) if x else x
     return sett
Beispiel #2
0
    def __call__( self, request, c ):
        """:meth:`pluggdapps.web.interfaces.IHTTPView.__call__` interface
        method.
        """
        resp = request.response
        assetpath = h.abspath_from_asset_spec( self.view['rootloc'] )
        docfile = join( assetpath, request.matchdict['path'] )

        if docfile and isfile( docfile ) :
            # Collect information about the document, for response.
            resp.set_status( b'200' )
            stat = os.stat( docfile )
            (typ, enc) = mimetypes.guess_type( docfile )
            if typ :
                resp.media_type = typ
            if enc :
                resp.content_coding = enc
            # Populate the context
            c.etag['body'] = open( docfile, 'rb' ).read()
            c['last_modified'] = h.http_fromdate( stat.st_mtime )
            cc = ('public,max-age=%s' % str(self['max_age']) ).encode('utf-8')
            resp.set_header( 'cache_control', cc )
            # Send Response
            resp.write( c['body'] )
            resp.flush( finishing=True )
        else :
            resp.pa.logwarn( "Not found %r" % docfile )
            resp.set_status( b'404' )
            resp.flush( finishing=True )
    def render(self, *args, **kwargs):
        """:meth:`pluggdapps.interfaces.IHTTPResponse.render`
        interface method.
        
        positional argument,

        ``request``,
            Instance of plugin implement
            :class:`pluggdapps.web.interfaces.IHTTPRequest` interface.

        ``context``,
            Dictionary of context information to be passed.

        keyword arguments,

        ``file``,
            Template file to be used for rendering.

        ``text``,
            Template text to be used for rendering.

        ``ITemplate``,
            :class:`ITemplate` plugin to use for rendering. This argument
            must be in canonical form of plugin's name.

        If ``file`` keyword argument is passed, this method will resolve the
        correct renderer plugin based on file-extension. if ``text`` keyword
        argument is passed, better pass the ``ITemplate`` argument as
        well.
        """
        request, context = args[0], args[1]
        renderer = kwargs.get('ITemplate', None)
        if renderer is None:
            tfile = kwargs.get('file', '')
            _, ext = splitext(tfile)
            renderer = self._renderers.get(ext, None) if ext else None

            # If in debug mode enable ttl file reloading.
            tfile = h.abspath_from_asset_spec(tfile)
            if self['debug'] and isfile(tfile):
                self.pa._monitoredfiles.append(tfile)

        if renderer in self._renderer_plugins:
            plugin = self._renderer_plugins[renderer]
        elif renderer:
            plugin = self.qp(ITemplate, renderer)
        else:
            plugin = None

        if plugin:
            self.media_type = 'text/html'
            self._renderer_plugins.setdefault(renderer, plugin)
            return plugin.render(context, **kwargs)
        else:
            raise Exception('Unknown renderer')
Beispiel #4
0
    def render( self, *args, **kwargs ):
        """:meth:`pluggdapps.interfaces.IHTTPResponse.render`
        interface method.
        
        positional argument,

        ``request``,
            Instance of plugin implement
            :class:`pluggdapps.web.interfaces.IHTTPRequest` interface.

        ``context``,
            Dictionary of context information to be passed.

        keyword arguments,

        ``file``,
            Template file to be used for rendering.

        ``text``,
            Template text to be used for rendering.

        ``ITemplate``,
            :class:`ITemplate` plugin to use for rendering. This argument
            must be in canonical form of plugin's name.

        If ``file`` keyword argument is passed, this method will resolve the
        correct renderer plugin based on file-extension. if ``text`` keyword
        argument is passed, better pass the ``ITemplate`` argument as
        well.
        """
        request, context = args[0], args[1]
        renderer = kwargs.get( 'ITemplate', None )
        if renderer is None :
            tfile = kwargs.get( 'file', '' )
            _, ext = splitext( tfile )
            renderer = self._renderers.get( ext, None ) if ext else None

            # If in debug mode enable ttl file reloading.
            tfile = h.abspath_from_asset_spec( tfile )
            if self['debug'] and isfile( tfile ):
                self.pa._monitoredfiles.append( tfile )

        if renderer in self._renderer_plugins :
            plugin = self._renderer_plugins[ renderer ]
        elif renderer :
            plugin = self.qp( ITemplate, renderer )
        else :
            plugin = None

        if plugin :
            self.media_type = 'text/html'
            self._renderer_plugins.setdefault( renderer, plugin )
            return plugin.render( context, **kwargs )
        else :
            raise Exception('Unknown renderer')
Beispiel #5
0
    def __init__( self, compiler, ttlloc=None, ttltext=None ):
        self.compiler = compiler

        if ttlloc :
            ttlfile = h.abspath_from_asset_spec( ttlloc )
            cachedir = compiler['cache_directory']
            self.encoding = self.charset( ttlfile=ttlfile,
                                          encoding=compiler['encoding'] )
            if compiler['nocache'] :
                self.pyfile = '<Source provided directly as text>'

            elif cachedir :
                if not isdir( cachedir ):
                    compiler.pa.logdebug(
                            "Creating cache directory %r " % cachedir )
                    os.makedirs( cachedir, exist_ok=True )
                self.pyfile = join( cachedir, ttlloc+'.py' )
                if not isfile( self.pyfile ):
                    os.makedirs( dirname( self.pyfile ), exist_ok=True )
            else :
                self.pyfile = ttlfile + '.py'

            self.ttlfile = h.locatefile( ttlfile, compiler['directories'] )
            self.ttltext = open( self.ttlfile, encoding=self.encoding ).read()

            # If reloading is available from the platform do so.
            try :
                if compiler['debug'] and isfile( ttlfile ) :
                    compiler.pa._monitoredfiles.append(tttllfile)
            except :
                pass

        elif ttltext :
            parselines = ttltext.encode('utf-8').splitlines()
            self.encoding = self.charset( parselines=parselines,
                                          encoding=compiler['encoding'] )
            self.pyfile = '<Source provided directly as text>'
            self.ttlfile = '<Source provided directly as text>'
            self.ttltext = ttltext.decode( self.encoding ) \
                                if isinstance( ttltext, bytes ) else ttltext

        else :
            raise Exception( 'Invalid ttl source !!' )

            return self._ttlhash
    def __init__(self, compiler, ttlloc=None, ttltext=None):
        self.compiler = compiler

        if ttlloc:
            ttlfile = h.abspath_from_asset_spec(ttlloc)
            cachedir = compiler['cache_directory']
            self.encoding = self.charset(ttlfile=ttlfile,
                                         encoding=compiler['encoding'])
            if compiler['nocache']:
                self.pyfile = '<Source provided directly as text>'

            elif cachedir:
                if not isdir(cachedir):
                    compiler.pa.logdebug("Creating cache directory %r " %
                                         cachedir)
                    os.makedirs(cachedir, exist_ok=True)
                self.pyfile = join(cachedir, ttlloc + '.py')
                if not isfile(self.pyfile):
                    os.makedirs(dirname(self.pyfile), exist_ok=True)
            else:
                self.pyfile = ttlfile + '.py'

            self.ttlfile = h.locatefile(ttlfile, compiler['directories'])
            self.ttltext = open(self.ttlfile, encoding=self.encoding).read()

            # If reloading is available from the platform do so.
            try:
                if compiler['debug'] and isfile(ttlfile):
                    compiler.pa._monitoredfiles.append(tttllfile)
            except:
                pass

        elif ttltext:
            parselines = ttltext.encode('utf-8').splitlines()
            self.encoding = self.charset(parselines=parselines,
                                         encoding=compiler['encoding'])
            self.pyfile = '<Source provided directly as text>'
            self.ttlfile = '<Source provided directly as text>'
            self.ttltext = ttltext.decode( self.encoding ) \
                                if isinstance( ttltext, bytes ) else ttltext

        else:
            raise Exception('Invalid ttl source !!')

            return self._ttlhash