def do_GET(self): """ Arguments: - `self`: """ truncate_path = self.path.split('?')[0].split('#')[0] path_items = self.path.split('?') query = path_items[1] if len(path_items) > 1 else '' response = 200 serverConfig = conf.getConfig() isInServerConfig = False if 'proxy' in serverConfig: for reg, target in serverConfig['proxy'].items(): if re.search(re.sub("\{.+\}$", "", reg), truncate_path): isInServerConfig = True if target.startswith('http'): #http url contentType, body = self.urlProxy( target, query, reg, truncate_path) elif target.startswith('plugins'): #local plugins contentType, body = self.pluginsProxy(target, query) if not isInServerConfig: if truncate_path == '/': #default page body = mgr.getIndex() response, contentType, body = (200, 'text/html', body) elif truncate_path.endswith('.ut'): #为模版文件 tplToken = truncate_path.replace('.ut', '')[1:] body = parser.parseTpl(tplToken) body = parser.compileCommon(body, 'local', True) body = parser.compilePlugin(tplToken, body) if conf.getConfig().get( 'type') and conf.getConfig()['type'] == 'mobile': body = body.replace( 'http://p0.123.sogou.com/u/js/mursa.js', 'http://ufo.sogou-inc.com/cdn/js/mursa-debug.js' ) #mobile project if len(body): response, contentType = (200, 'text/html') else: response, contentType, body = (404, 'text/html', 'no template called ' + tplToken) elif truncate_path.endswith('.m'): #为模版管理 tplToken = truncate_path.replace('.m', '')[1:] tpl = parser.parseTpl(tplToken) if len(tpl): body = mgr.getPage(tplToken) response, contentType, body = (200, 'text/html', body) else: response, contentType, body = (404, 'text/html', 'Error finding tpl file.') else: response, contentType, body = self.server_static(truncate_path) self.sendResponseWithOutput(response, contentType, body)
def do_GET(self): """ Arguments: - `self`: """ truncate_path = self.path.split('?')[0].split('#')[0] path_items = self.path.split('?') query = path_items[1] if len(path_items) > 1 else '' response = 200 serverConfig = conf.getConfig() isInServerConfig = False if 'proxy' in serverConfig: for reg,target in serverConfig['proxy'].items(): if re.search(re.sub( "\{.+\}$" , "" , reg ) , truncate_path): isInServerConfig = True if target.startswith('http'):#http url contentType,body =self.urlProxy(target , query, reg , truncate_path) elif target.startswith('plugins'):#local plugins contentType , body = self.pluginsProxy(target , query) if not isInServerConfig: if truncate_path == '/':#default page body = mgr.getIndex() response,contentType,body = (200 , 'text/html' , body) elif truncate_path.endswith('.ut'):#为模版文件 tplToken = truncate_path.replace('.ut' , '') [1:] body = parser.parseTpl(tplToken) body = parser.compileCommon(body , 'local' , True) body = parser.compilePlugin(tplToken,body) if conf.getConfig().get('type') and conf.getConfig()['type'] == 'mobile': body = body.replace('http://p0.123.sogou.com/u/js/mursa.js' , 'http://ufo.sogou-inc.com/cdn/js/mursa-debug.js') #mobile project if len(body): response,contentType = (200 , 'text/html') else: response,contentType,body = (404 , 'text/html' , 'no template called ' + tplToken) elif truncate_path.endswith('.m'):#为模版管理 tplToken = truncate_path.replace('.m' , '')[1:] tpl = parser.parseTpl(tplToken) if len(tpl): body = mgr.getPage(tplToken) response,contentType,body = (200,'text/html' , body) else: response,contentType,body = (404,'text/html' , 'Error finding tpl file.') else: response, contentType, body = self.server_static(truncate_path) self.sendResponseWithOutput(response , contentType , body)
def getPage(token): """获取mgr的页面 Arguments: - `token`: """ base = conf.getConfig()['base'] mgrTpl = os.path.join( base , 'assets' , 'mgr' , 'mgr.html') body = parser.parseTpl(mgrTpl , { 'name':token, 'data': getRawData(token) } , True) return body
def compileHTML(needCompress=False, needHtml=False): """为所有tpl文件加上时间戳 """ base = os.path.join(PATH, 'build', 'template') tplfiles = [] for dirpath, dirnames, filenames in os.walk(base): tplfiles.extend([ os.path.join(dirpath, f) for f in filenames if f.endswith('.tpl') ]) if COMPILE_FOLDER: for dirpath, dirnames, filenames in os.walk( os.path.join(PATH, 'build', COMPILE_FOLDER)): tplfiles.extend([os.path.join(dirpath, f) for f in filenames]) for tpl in tplfiles: f = parser.compileHTML(tpl, needCompress) utils.writefile(tpl, f) if needHtml: log.log('Render html file.\nIt will under build folder.') files = os.listdir(os.path.join(base)) tplfiles = [] for dirpath, dirnames, filenames in os.walk(base): tplfiles.extend([ os.path.join(dirpath, f) for f in filenames if f.endswith('.tpl') ]) for fname in tplfiles: token = fname.replace(base + '/', '').replace('.tpl', '') try: #有强行编译的需求 html = parser.parseTpl(token, isbuild=True) if token.find('/') != -1: subfolder = os.path.join(PATH, 'build', 'html', token.split('/')[0]) if not os.path.exists(subfolder): utils.createfolder(subfolder) utils.writefile( os.path.join(PATH, 'build', 'html', token + '.html'), html) except Exception as e: log.error(str(e)) if not conf.getConfig().get('html_force_output'): raise log.success('Render html success')
def getIndex(): base = conf.getConfig()['base'] indexTpl = os.path.join( base , 'assets' , 'mgr' , 'index.html' ) path = conf.getConfig()['path'] tpls = [] for dirpath , dirnames , filenames in os.walk( os.path.join( path , 'template' ) ): for f in filenames: if f.endswith('.tpl'): tpldir = dirpath.replace( os.path.join( path , 'template' ) , '' ) url = '/'.join( [ tpldir , f.replace('.tpl' , '') ] ) tpls.extend([ url[1:] ]) body = parser.parseTpl( indexTpl , { 'tpls':tpls } , True ) return body
def compileHTML( needCompress = False , needHtml = False ): """为所有tpl文件加上时间戳 """ base = os.path.join(PATH , 'build' , 'template') tplfiles = [] for dirpath , dirnames , filenames in os.walk(base): tplfiles.extend([ os.path.join( dirpath , f ) for f in filenames if f.endswith('.tpl') ]) if COMPILE_FOLDER: for dirpath , dirnames , filenames in os.walk(os.path.join(PATH , 'build' , COMPILE_FOLDER)): tplfiles.extend([ os.path.join( dirpath , f ) for f in filenames ]) for tpl in tplfiles: f = parser.compileHTML(tpl , needCompress) utils.writefile(tpl , f) if needHtml: log.log('Render html file.\nIt will under build folder.') files = os.listdir( os.path.join( base ) ) tplfiles = [] for dirpath , dirnames, filenames in os.walk(base): tplfiles.extend( [ os.path.join(dirpath , f) for f in filenames if f.endswith('.tpl') ] ) for fname in tplfiles: token = fname.replace( base + '/' , '' ).replace('.tpl' , '') try:#有强行编译的需求 html = parser.parseTpl(token , isbuild=True) if token.find('/') != -1: subfolder = os.path.join(PATH , 'build' , 'html' , token.split('/')[0]) if not os.path.exists(subfolder): utils.createfolder(subfolder) utils.writefile( os.path.join(PATH , 'build' , 'html' , token + '.html' ) , html ) except Exception as e: log.error(str(e)) if not conf.getConfig().get('html_force_output'): raise log.success('Render html success');