Making the code base easy to read and maintain and making it a oneline install
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build dist/ollama_answers.py by inlining assets as Python string constants."""
|
||||
import os
|
||||
import re
|
||||
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
ASSETS = os.path.join(ROOT, 'assets')
|
||||
SRC = os.path.join(ROOT, 'ollama_answers.py')
|
||||
DIST_DIR = os.path.join(ROOT, 'dist')
|
||||
DIST = os.path.join(DIST_DIR, 'ollama_answers.py')
|
||||
|
||||
|
||||
def read(path):
|
||||
with open(path, encoding='utf-8') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def main():
|
||||
css = read(os.path.join(ASSETS, 'ui.css'))
|
||||
html = read(os.path.join(ASSETS, 'ui.html'))
|
||||
js_raw = read(os.path.join(ASSETS, 'ui.js'))
|
||||
|
||||
frontend_js = js_raw.split('// === CITATION_HELPER_JS ===')[0].replace('// === FRONTEND_JS_TEMPLATE ===', '').strip()
|
||||
citation_js = js_raw.split('// === CITATION_HELPER_JS ===')[1].split('// === INTERACTIVE_JS ===')[0].strip()
|
||||
interactive_js = js_raw.split('// === INTERACTIVE_JS ===')[1].strip()
|
||||
|
||||
src = read(SRC)
|
||||
|
||||
asset_block = '''\
|
||||
# UI assets
|
||||
_ASSETS = os.path.join(os.path.dirname(__file__), 'assets')
|
||||
INTERACTIVE_CSS = open(os.path.join(_ASSETS, 'ui.css')).read()
|
||||
INTERACTIVE_HTML = open(os.path.join(_ASSETS, 'ui.html')).read()
|
||||
_js_raw = open(os.path.join(_ASSETS, 'ui.js')).read()
|
||||
FRONTEND_JS_TEMPLATE = _js_raw.split('// === CITATION_HELPER_JS ===')[0].replace('// === FRONTEND_JS_TEMPLATE ===', '').strip()
|
||||
CITATION_HELPER_JS = _js_raw.split('// === CITATION_HELPER_JS ===')[1].split('// === INTERACTIVE_JS ===')[0].strip()
|
||||
INTERACTIVE_JS = _js_raw.split('// === INTERACTIVE_JS ===')[1].strip()'''
|
||||
|
||||
inline_block = (
|
||||
'# UI assets\n'
|
||||
'INTERACTIVE_CSS = """\\\n' + css.replace('\\', '\\\\') + '\n"""\n'
|
||||
'INTERACTIVE_HTML = """\\\n' + html.replace('\\', '\\\\') + '\n"""\n'
|
||||
'FRONTEND_JS_TEMPLATE = r"""\\\n' + frontend_js + '\n"""\n'
|
||||
'CITATION_HELPER_JS = r"""\\\n' + citation_js + '\n"""\n'
|
||||
'INTERACTIVE_JS = r"""\\\n' + interactive_js + '\n"""\n'
|
||||
)
|
||||
|
||||
if asset_block not in src:
|
||||
print('ERROR: Could not locate the asset-loading block in ollama_answers.py')
|
||||
raise SystemExit(1)
|
||||
|
||||
dist_src = src.replace(asset_block, inline_block)
|
||||
dist_src = '# AUTO-GENERATED by build.py — do not edit directly\n' + dist_src
|
||||
|
||||
os.makedirs(DIST_DIR, exist_ok=True)
|
||||
with open(DIST, 'w', encoding='utf-8') as f:
|
||||
f.write(dist_src)
|
||||
|
||||
print('Built dist/ollama_answers.py successfully')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user