예제 #1
0
파일: __init__.py 프로젝트: prataprc/tss
def tss_cmdline( tssloc, **kwargs ):
    from tss.compiler import Compiler

    tssconfig = dict( defaultconfig.items() )
    options = kwargs.pop('_options', None )
    # directories, module_directory, devmod
    tssconfig.update( kwargs )

    # Parse command line arguments and configuration
    tssconfig.setdefault('devmod', DEVMOD)
    args = eval( options.args ) if options and options.args else []
    context = options and options.context or {}
    context = eval(context) if isinstance(context, basestring) else context
    context.update( _mainargs=args ) if args else None
    debuglevel = int(options.debug) if options and options.debug else 0
    show, dump = (options.show, options.dump) if options else (None, None)

    # Setup parser
    tssparser = TSSParser( tssconfig=tssconfig, debug=debuglevel )
    comp = Compiler( tssloc=tssloc, tssconfig=tssconfig, tssparser=tssparser )
    pyfile = comp.tssfile+'.py'
    cssfile = basename( comp.tssfile ).rsplit('.', 1)[0] + '.css'
    cssfile = join( dirname(comp.tssfile), cssfile )

    if debuglevel :
        print "AST tree ..."
        tu = comp.toast()
    elif show :
        print "AST tree ..."
        tu = comp.toast()
        tu.asttransorms( comp.igen, tu.table )
        tu.show()
    elif dump :
        tu = comp.toast()
        rctext = tu.dump()
        if rctext != codecs.open( comp.tssfile, encoding=comp.encoding ).read() :
            print "Mismatch ..."
        else : print "Success ..."
    else :
        print "Generating py / CSS file ... "
        pytext = comp.topy() # pytext in unicode
        # Intermediate file should always be encoded in 'utf-8'
        enc = comp.encoding.rstrip('-sig') # -sig is used to interpret BOM
        codecs.open( pyfile, mode='w', encoding=enc ).write(pytext)

        t = Translator( tssloc=tssloc, tssconfig=tssconfig )
        css = t( context=context )
        codecs.open( cssfile, mode='w', encoding=enc ).write( css )

        # This is for measuring performance
        st = dt.now()
        [ t( context=context ) for i in range(2) ]
        print (dt.now() - st) / 2
예제 #2
0
파일: __init__.py 프로젝트: prataprc/tss
    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
예제 #3
0
파일: __init__.py 프로젝트: prataprc/tss
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