-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tools.app): Add tool to create html app
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ Welcome to Trame's documentation! | |
:caption: Tools | ||
|
||
tools.www | ||
tools.app | ||
|
||
.. include:: ../../../README.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |