要求する
バックエンドテンプレートファイルはフロントエンドリソースを参照し、公開すると、フロントエンドリソースのバージョンが自動的にPythonスクリプトに置き換えられます。
準備オーケー
フロントエンドはWebpackを使用してビルドされ、ビルドが完了するたびにbuildfile.jsonが生成されます
buildfile.json
{ "about.css": "about-ca3f85cec35d3ab39ac0.css"、 "about.js": "about-613281148cb8885f2b3d.js"、 "home.css": "home-94093b44a5a85be01f38.css"、 "home.js": " home-6dd4e116818ee4945f65.js " } 复制代码
フロントエンドビルドファイルを読む
import os import json BUILD_FILES = {} def get_build_files(): build_file_path = '/ Users / pengjie / try / iseo2 / dist / buildfile.json' if(os.path.exists(build_file_path)): with open(build_file_path、 'r ')as f: return json.load(f) else: print('ビルドファイルが存在しません。 ') return' ' BUILD_FILES = get_build_files(); 复制代码
すべてのテンプレートファイルを取得する
def get_template_files(): template_path = '/ Users / pengjie / try / iseo2 / tmpviews' template_files = [] for dirpath、dirnames、filenames in os.walk(template_path): for filename in filenames: if(filename [0]!= ) '': ファイルパス= os.path.join(DIRPATH、ファイル名) template_files.append(ファイルパス) リターンtemplate_files 复制代码
置き換えるすべてのスクリプトとリンクを取得する
import os import re from bs4 import BeautifulSoup def get_replace_list(content): replace_list = [] soup = BeautifulSoup(content、 'html.parser') #获取所有リンク links = soup.find_all( 'link') for item in Links : name = os.path.basename(item.get( 'href')) matches = re.search(r '\-(。+)?(\。)'、name、re.I) key = name.replace(matches [0]、 '。') replace_list.append({'old':name、 'new':BUILD_FILES.get(key、 '')}) # 获取所有スクリプトscripts = soup.find_all( 'script') for itemスクリプト内: name = os.path.basename(item.get( 'src')) matches = re.search(r '\-(。+)?(\。)'、name、re。I) key = name.replace(matches [0]、 '。') replace_list.append({ '古い':名前、 '新しい':BUILD_FILES.get(キー、 '')}) 戻りreplace_list 复制代码
検索して置き換えます
def search_and_replace(tplpath): content = '' with open(tplpath、 'r'、encoding = "utf-8")as f: content = f.read() replace_list = get_replace_list(content) for item in replace_list: if( item ['old'] and item ['new']): content = content.replace(item ['old']、item ['new']) with open(tplpath、 "w"、encoding = "utf-8 ")as f: f.write(content) 复制代码
完全なスクリプトコンテンツ
import os import re import json from bs4 import BeautifulSoup BUILD_FILES = {} def run(): view_path = '/ Users / pengjie / try / iseo2 / tmpviews' for dirpath、dirnames、filenames in os.walk(view_path): for filename in filenames: if(filename [0]!= '。'): filepath = os.path.join(dirpath、filename) #读取文件、获取受换列表 content = '' with open(filepath、 'r'、encoding = "utf-8")as f: content = f.read() replace_list = get_replace_list(content) for item in replace_list: if(item ['old'] and item ['new']): content = content.replace(item ['old']、item ['new']) with open(filepath、 "w"、encoding = "utf -8 ")as f: f.write(content) print( '----') #获取保留换列表 def get_replace_list(content): replace_list = [] soup = BeautifulSoup(content、 'html.parser') #获取 リンク内の アイテムの所有リンクリンク= soup.find_all( 'link'): name = os.path.basename(item.get( 'href')) matches = re.search(r '\-(。+)?( \。) '、name、re.I) key = name.replace(matches [0]、'。 ') replace_list.append({'old':name、 'new':BUILD_FILES.get(key、 '')}) # 获取所有スクリプトscripts = soup.find_all( 'script') for item in scripts: name = os.path .basename(item。get( 'src')) matches = re.search(r '\- (。+)?(\。) '、name、re.I) key = name.replace(matches [0]、'。 ') replace_list.append({' old ':name、' new ':BUILD_FILES.get (key、 '')}) return replace_list #获取着建文件 def get_build_file(): build_file_path = '/ Users / pengjie / try / iseo2 / dist / buildfile.json' if(os.path.exists(build_file_path)): with open(build_file_path、 'r')as f: return json.load(f) else: print( 'ビルドファイルが存在しません。') return '' if __name__ == ' __ main __ ':BUILD_FILES = get_build_file() if(BUILD_FILES): run() 复制代码
完全なプロジェクトコードを入手するには、ここをクリックしてください