Merge branch 'core-master' into Astro

This commit is contained in:
2021-09-05 23:30:07 +08:00
17 changed files with 206 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import os
from sys import platform
from tempfile import NamedTemporaryFile
from botanjs.config import Config as config
@@ -45,8 +46,6 @@ class Wrapper:
with open( loc, "rb" ) as f:
content = f.read()
with NamedTemporaryFile() as f:
with NamedTemporaryFile( delete = ( not platform == "win32" ) ) as f:
f.write( content[12:-5] )
os.system( self.C + self.E + " --js " + f.name + " --js_output_file " + loc[:-3] + ".c.js" )

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import os
from sys import platform
from botanjs.config import Config as config
COMPILER = config[ "BotanJS" ][ "YuiCompressor" ]
@@ -20,5 +21,8 @@ class Wrapper:
self.C = "java -jar " + COMPILER + " " + " ".join( COMPILER_OPTIONS )
def compress( self, loc ):
os.system( self.C + " " + loc + " -o " + loc[:-4] + ".c.css" )
if platform == "win32":
loc = loc.replace( "C:", "" ).replace( "\\\\", "/" )
os.system( self.C + " " + loc + " -o " + loc[:-4] + ".c.css" )

View File

@@ -18,7 +18,7 @@ class dummyTask( object ):
pass
class dummyConf:
def update( self, BROKER_URL = None ):
def update( self, broker_url = None ):
pass
class app:

View File

@@ -99,7 +99,9 @@ class BotanClassResolver:
classMap = ""
flagCompress = True
oModeIfSizelt = 50 * 1024
returnHash = False
returnDynamic = False
resv = None
def __init__( self, jwork, BotanRoot, classMap, cacheRoot ):
@@ -122,12 +124,17 @@ class BotanClassResolver:
return content
def BotanCache( self, t ):
content = ""
with open( t, "r" ) as f:
content = f.read()
def BotanCache( self, srcFile, fileHash, hashContentDynamic ):
return content
for _ in [0]:
if hashContentDynamic and os.path.getsize( srcFile ) < self.oModeIfSizelt:
break
if self.returnHash:
return fileHash
with open( srcFile, "r" ) as f:
return f.read()
def cleanList( self, lista ):
olist = []
@@ -196,7 +203,7 @@ class BotanClassResolver:
dates.append( os.path.getmtime( os.path.join( self.R, "_this.js" ) ) );
if self.flagCompress and self.useCache( cFile, dates ):
return cFHash if self.returnHash else self.BotanCache( cFile )
return self.BotanCache( cFile, cFHash, self.returnDynamic )
elif self.useCache( oFile, dates ):
self.JWork.saveCache(
@@ -207,7 +214,7 @@ class BotanClassResolver:
, os.path.join( self.R, "externs" )
)
return oFHash if self.returnHash else self.BotanCache( oFile )
return self.BotanCache( oFile, oFHash, False )
def useCache( self, f, dList ):
if not os.path.exists( f ):
@@ -248,7 +255,7 @@ class BotanClassResolver:
outputJs = wrapScope( outputJs )
[ self.JWork.saveCache if self.returnHash else self.JWork.saveCache ][0] (
self.JWork.saveCache(
os.path.join( self.CR, md5[0] )
, outputJs
, "js"
@@ -283,9 +290,7 @@ class BotanClassResolver:
for f in self.cleanList( cList ):
outputCss += self.BotanFile( f )
[ self.JWork.saveCache if self.returnHash else self.JWork.saveCache ][0] (
os.path.join( self.CR, md5[0] ), outputCss, "css"
)
self.JWork.saveCache( os.path.join( self.CR, md5[0] ), outputCss, "css" )
if self.returnHash:
return md5[0]
@@ -299,13 +304,26 @@ class BotanClassResolver:
flag = mode[0]
requestAPIs = code
# Return compressed contents if possible
# otherwise return raw contents
if flag == "o":
mode = mode[1:]
# Return raw contents only
elif flag == "r":
mode = mode[1:]
self.flagCompress = False
# Return hashed filenames only
elif flag == "h":
mode = mode[1:]
self.returnHash = True
# Return hashed filenames if content is larger than self.oModeIfSizelt bytes
# otherwise act as "o" mode
else:
self.returnHash = True
self.returnDynamic = True
try:
requestAPIs = (
@@ -329,7 +347,8 @@ class BotanClassResolver:
for apis in requestAPIs:
if apis == None: continue
if not apis:
continue
classList = []
lookupList = imports

View File

@@ -17,7 +17,7 @@ if CeleryExists:
if os.path.exists( "settings.ini" ):
from botanjs.config import Config
app.conf.update( BROKER_URL = Config["BotanJS"]["CeleryBroker"] )
app.conf.update( broker_url = Config["BotanJS"]["CeleryBroker"] )
else:
from botanjs.dummy import app
@@ -34,7 +34,7 @@ class JWork:
if mode == "js":
JWork.compressJs.delay( location, externs )
elif mode == "css":
JWork.compressCss.delay( location )
JWork.compressCss( location )
@app.task()
def compressJs( md5, externs ):
@@ -54,5 +54,6 @@ class JWork:
log.info( "Building Class Map" )
c = ClassMap( src )
os.makedirs( os.path.dirname( location ), exist_ok = True )
with open( location, "w" ) as f:
f.write( c.build() )

View File

@@ -23,9 +23,9 @@ class WebAPI:
self.BMap = os.path.join( self.BCache, "bmap.xml" )
if brokerURL != None:
CeleryApp.conf.update( BROKER_URL = brokerURL )
CeleryApp.conf.update( broker_url = brokerURL )
self.app = Flask( __name__, static_url_path = self.BCache, static_folder = self.BCache )
self.app = Flask( __name__, static_url_path = "/cache/botanjs", static_folder = self.BCache )
self.app.jinja_env.add_extension( "compressinja.html.HtmlCompressor" )
self.app.add_url_rule( "/" , view_func = self.index )
@@ -33,6 +33,7 @@ class WebAPI:
self.app.add_url_rule( "/<mode>/<path:code>" , view_func = self.api_request )
def run( self, *args, **kwargs ):
JWork.buildClassMap( self.BRoot, self.BMap )
return self.app.run( *args, **kwargs )
def index( self ):
@@ -40,6 +41,10 @@ class WebAPI:
def api_request( self, mode, code ):
if mode == "rebuild":
JWork.buildClassMap.delay( self.BRoot, self.BMap )
return "OK", 200
if code == "zpayload":
code = request.args.get( "p" )
@@ -53,6 +58,8 @@ class WebAPI:
srvHandler = JCResv( JWork, self.BRoot, self.BMap, self.BCache )
return Response( srvHandler.getAPI( code, mode = mode ), mimetype = t )
except Exception as e:
if self.app.config[ "DEBUG" ]:
raise
return str(e), 404

View File

@@ -1,2 +1,2 @@
/** @type {Object} */
_AstXObject_ = {};
var _AstXObject_ = {};