AstroJS/botanjs/service/jwork.py

64 lines
1.5 KiB
Python
Raw Normal View History

2015-08-14 10:12:10 +00:00
#!/usr/bin/env python3
import os
from botanjs.classmap import ClassMap
2022-08-02 13:51:38 +00:00
if os.getenv( "UNIT_TEST" ) == "1":
CeleryExists = False
else:
CeleryExists = True
try:
from celery import Celery
except ImportError:
CeleryExists = False
2015-08-14 10:12:10 +00:00
if CeleryExists:
from celery.utils.log import get_task_logger
app = Celery( "botanJWork" )
log = get_task_logger( __name__ )
if os.path.exists( "settings.ini" ):
from botanjs.config import Config
2021-09-05 15:16:37 +00:00
app.conf.update( broker_url = Config["BotanJS"]["CeleryBroker"] )
else:
from botanjs.dummy import app
from botanjs.dummy import log
2015-08-14 10:12:10 +00:00
class JWork:
def saveCache( location, content = None, mode = None, externs = "" ):
if content != None:
log.info( "Writing file(" + str( len( content ) ) + "): " + os.path.abspath( location ) )
with open( location, "w" ) as f:
f.write( content )
if mode == "js":
JWork.compressJs.delay( location, externs )
elif mode == "css":
2021-09-05 15:16:37 +00:00
JWork.compressCss( location )
2015-08-14 10:12:10 +00:00
@app.task()
def compressJs( md5, externs ):
2022-08-02 13:35:37 +00:00
from botanjs.compressor.closure import Wrapper as ClosureWrapper
2015-08-14 10:12:10 +00:00
log.info( "Compress js: " + md5 )
w = ClosureWrapper()
w.scanExterns( externs )
w.compress( md5 )
@app.task()
def compressCss( md5 ):
2022-08-02 13:35:37 +00:00
from botanjs.compressor.yui import Wrapper as YUIWrapper
2015-08-14 10:12:10 +00:00
log.info( "Compress css: " + md5 )
w = YUIWrapper()
w.compress( md5 )
@app.task()
def buildClassMap( src, location ):
log.info( "Building Class Map" )
c = ClassMap( src )
2021-09-05 15:16:37 +00:00
os.makedirs( os.path.dirname( location ), exist_ok = True )
2015-08-14 10:12:10 +00:00
with open( location, "w" ) as f:
f.write( c.build() )