File: app.py

package info (click to toggle)
python-trame 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (59 lines) | stat: -rw-r--r-- 1,418 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
r"""
From the directory containing the static content for a trame application to work,
generate another application specific HTML file.
"""

import argparse
import re
import sys
from pathlib import Path

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()