Beispiel #1
0
class Translator( object ):
    """Translate TSS file(s) to CSS

    ``tssloc``,
        Location of Tayra template file, either as relative directory or as
        asset specification.
    ``tsstext``,
        Tayra template text. It is assumed in unicode format. 
    ``tssconfig``,
        Configuration parameter, will find its way into every object defined by
        the templating engine.
    """
    def __init__( self, tssloc=None, tsstext=None, tssconfig={} ):
        self.tssconfig = dict( defaultconfig.items() )
        self.tssconfig.update( tssconfig )
        self.tssconfig.setdefault( 'devmod', DEVMOD )
        # Initialize plugins
        self.tssloc, self.tsstext = tssloc, tsstext
        self.tssparser = TSSParser( tssconfig=self.tssconfig )

    def __call__( self, entryfn='main', context={} ):
        """Compile, execute and return css text corresponding to this TSS
        document.

        key-word arguments,
        ``entryfn``,
            name of entry function to be called.
        ``context``,
            dictionary of key,value pairs to be used as context for generating
            css text.

        Arguments to main() function can be passed via context variables,
        ``_mainargs`` (a list of positional arguments) and ``_mainkwargs`` a
        dictionary of key-word arguments.

        dictionary object ``context`` will also be available as _tsscontext
        variable.
        """
        from tss.compiler import Compiler
        self.compiler = Compiler( tsstext=self.tsstext,
                                  tssloc=self.tssloc,
                                  tssconfig=self.tssconfig,
                                  tssparser=self.tssparser
                                )
        context['_tsscontext'] = context
        module = self.compiler.exectss( context=context )
        # Fetch parent-most module
        entry = getattr( module, entryfn )
        args = context.get( '_mainargs', [] )
        kwargs = context.get( '_mainkwargs', {} )
        css = entry( *args, **kwargs ) if callable( entry ) else ''
        return css