Beispiel #1
0
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