Skip to content

Commit

Permalink
fix(tools.app): Add tool to create html app
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed May 29, 2022
1 parent 8fe7cc9 commit d8c11e6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Welcome to Trame's documentation!
:caption: Tools

tools.www
tools.app

.. include:: ../../../README.rst
18 changes: 18 additions & 0 deletions docs/api/source/tools.app.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Application file generator
==============================

This small utility let you run a command line to generate a derived HTML file
from the original `static web client output <https://trame.readthedocs.io/en/latest/tools.www.html>`_
to run a specific application from a launcher configuration.

In order to use that tool, you will need to provide the path of the base
application to use as template along with the name of the application you
aim to run from your launcher config.
This tool will then create a new HTML file within the same directory that
will start the provided application name rather than the default "trame" one.


.. code-block:: bash
python -m trame.tools.app --input ./www-content --name MySuperApp
# => create file MySuperApp.html from ./www-content/index.html
58 changes: 58 additions & 0 deletions trame/tools/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
r"""
From the directory containing the static content for a trame application to work,
generate another application specific HTML file.
"""
import argparse
import re
from pathlib import Path
import sys

APP_PATTERN = re.compile(r'data-app-name="\w+"')


def create_app_file(input_file, output_file, app_name):
# Read in the file
with open(input_file, "r") as f_in:
content = f_in.read()
patched_content = APP_PATTERN.sub(f'data-app-name="{app_name}"', content)
with open(output_file, "w") as f_out:
f_out.write(patched_content)


def main():
parser = argparse.ArgumentParser(
description="HTML app file generator for trame applications"
)

parser.add_argument(
"--name",
default="trame",
help="Application name to encode inside HTML {name}.html",
)

parser.add_argument(
"--input",
help="Input file to use as template",
)

args, _ = parser.parse_known_args()

# Handle input
input_file = Path(args.input)
if not input_file.exists():
parser.print_help()
sys.exit(0)

if input_file.is_dir():
input_file = input_file / "index.html"

if not input_file.exists():
parser.print_help()
sys.exit(0)

output_file = input_file.with_stem(args.name)
create_app_file(input_file, output_file, args.name)


if __name__ == "__main__":
main()

0 comments on commit d8c11e6

Please sign in to comment.