html_compose.cli
1import argparse 2import fileinput 3import sys 4 5from . import translate_html 6 7 8def from_html(args): 9 is_stdin = args.html == "-" 10 11 if is_stdin: 12 print("Reading from stdin. Press Ctrl+D to finish.") 13 14 try: 15 html_content = "\n".join( 16 [ 17 line 18 for line in fileinput.input(files=args.html, encoding="utf-8") 19 ] 20 ) 21 except Exception as exc: 22 print("Failed to read HTML content: {}".format(exc)) 23 return 24 except KeyboardInterrupt: 25 return 26 27 if is_stdin: 28 print("---\n") 29 30 tresult = translate_html.translate( 31 html_content, args.noimport, args.constructor 32 ) 33 34 if tresult is None: 35 print("Failed to translate HTML content") 36 return 37 38 if tresult.import_statement: 39 print(tresult.import_statement, "\n") 40 41 if tresult.custom_elements: 42 print("\n".join(tresult.custom_elements)) 43 44 if len(tresult.elements) > 1: 45 print(tresult.as_array()) 46 elif len(tresult.elements) == 1: 47 print(tresult.elements[0]) 48 49 50def parse_html_translate(parser): 51 parser.add_argument( 52 "html", 53 default="-", 54 nargs="?", 55 help="HTML file to translate (default: stdin)", 56 ) 57 58 parser.add_argument( 59 "-n", 60 "--noimport", 61 nargs="?", 62 const="html_compose", 63 default=None, 64 help="Instead of importing each element, use the specified module name", 65 ) 66 67 parser.add_argument( 68 "-c", 69 "--constructor", 70 action="store_true", 71 help="Always output element constructor", 72 ) 73 74 75def html_convert(): 76 parser = argparse.ArgumentParser(description="HTML to python translator") 77 parse_html_translate(parser) 78 args = parser.parse_args() 79 from_html(args) 80 81 82def parse_gallery(parser): 83 parser.add_argument("module", help="Path to the module to run") 84 parser.add_argument( 85 "--host", 86 default="localhost", 87 help="Host to run the gallery server on (default: localhost)", 88 ) 89 parser.add_argument( 90 "--port", 91 type=int, 92 default=8000, 93 help="Port to run the gallery server on (default: 8000)", 94 ) 95 parser.add_argument( 96 "--livereload-host", 97 default="(host)", 98 help="Host to run the gallery server on (defaults to host value)", 99 ) 100 parser.add_argument( 101 "--livereload-port", 102 type=int, 103 default=51353, 104 help="Port to run the gallery server on (default: 51353)", 105 ) 106 parser.add_argument( 107 "--force-polling", 108 action="store_true", 109 help="Force polling for file changes instead of using inotify or similar", 110 ) 111 parser.add_argument( 112 "--watch-extra-path", 113 action="append", 114 default=[], 115 help="Additional paths to watch for changes (can be specified multiple times)", 116 ) 117 parser.add_argument( 118 "--python-path", 119 nargs="?", 120 help="Add path to Python search path", 121 default=".", 122 ) 123 124 125def gallery(args): 126 from .gallery.server import run 127 128 if args.python_path: 129 if args.python_path not in sys.path: 130 sys.path.insert(0, args.python_path) 131 132 try: 133 run( 134 module_path=args.module, 135 host=args.host, 136 port=args.port, 137 force_polling=args.force_polling, 138 extra_paths=args.watch_extra_path, 139 livereload_host=args.livereload_host 140 if args.livereload_host != "(host)" 141 else args.host, 142 livereload_port=args.livereload_port, 143 ) 144 except KeyboardInterrupt: 145 print("\nExiting...") 146 147 148def cli(): 149 """ 150 Command-line tool to translate HTML to Python code using html_compose 151 152 This function reads from stdin by default, but accepts an optional filename as argument 153 """ 154 HTML_CONVERT = "convert" 155 parser = argparse.ArgumentParser(description="html-compose cli") 156 subparsers = parser.add_subparsers(dest="command") 157 158 html_parser = subparsers.add_parser( 159 HTML_CONVERT, help="Translate HTML to html-compose" 160 ) 161 parse_html_translate(html_parser) 162 gallery_parser = subparsers.add_parser( 163 "gallery", help="Run an HTML gallery server" 164 ) 165 parse_gallery(gallery_parser) 166 args = parser.parse_args() 167 if args.command == HTML_CONVERT: 168 from_html(args) 169 elif args.command == "gallery": 170 gallery(args) 171 else: 172 parser.print_help()
def
from_html(args):
9def from_html(args): 10 is_stdin = args.html == "-" 11 12 if is_stdin: 13 print("Reading from stdin. Press Ctrl+D to finish.") 14 15 try: 16 html_content = "\n".join( 17 [ 18 line 19 for line in fileinput.input(files=args.html, encoding="utf-8") 20 ] 21 ) 22 except Exception as exc: 23 print("Failed to read HTML content: {}".format(exc)) 24 return 25 except KeyboardInterrupt: 26 return 27 28 if is_stdin: 29 print("---\n") 30 31 tresult = translate_html.translate( 32 html_content, args.noimport, args.constructor 33 ) 34 35 if tresult is None: 36 print("Failed to translate HTML content") 37 return 38 39 if tresult.import_statement: 40 print(tresult.import_statement, "\n") 41 42 if tresult.custom_elements: 43 print("\n".join(tresult.custom_elements)) 44 45 if len(tresult.elements) > 1: 46 print(tresult.as_array()) 47 elif len(tresult.elements) == 1: 48 print(tresult.elements[0])
def
parse_html_translate(parser):
51def parse_html_translate(parser): 52 parser.add_argument( 53 "html", 54 default="-", 55 nargs="?", 56 help="HTML file to translate (default: stdin)", 57 ) 58 59 parser.add_argument( 60 "-n", 61 "--noimport", 62 nargs="?", 63 const="html_compose", 64 default=None, 65 help="Instead of importing each element, use the specified module name", 66 ) 67 68 parser.add_argument( 69 "-c", 70 "--constructor", 71 action="store_true", 72 help="Always output element constructor", 73 )
def
html_convert():
def
parse_gallery(parser):
83def parse_gallery(parser): 84 parser.add_argument("module", help="Path to the module to run") 85 parser.add_argument( 86 "--host", 87 default="localhost", 88 help="Host to run the gallery server on (default: localhost)", 89 ) 90 parser.add_argument( 91 "--port", 92 type=int, 93 default=8000, 94 help="Port to run the gallery server on (default: 8000)", 95 ) 96 parser.add_argument( 97 "--livereload-host", 98 default="(host)", 99 help="Host to run the gallery server on (defaults to host value)", 100 ) 101 parser.add_argument( 102 "--livereload-port", 103 type=int, 104 default=51353, 105 help="Port to run the gallery server on (default: 51353)", 106 ) 107 parser.add_argument( 108 "--force-polling", 109 action="store_true", 110 help="Force polling for file changes instead of using inotify or similar", 111 ) 112 parser.add_argument( 113 "--watch-extra-path", 114 action="append", 115 default=[], 116 help="Additional paths to watch for changes (can be specified multiple times)", 117 ) 118 parser.add_argument( 119 "--python-path", 120 nargs="?", 121 help="Add path to Python search path", 122 default=".", 123 )
def
gallery(args):
126def gallery(args): 127 from .gallery.server import run 128 129 if args.python_path: 130 if args.python_path not in sys.path: 131 sys.path.insert(0, args.python_path) 132 133 try: 134 run( 135 module_path=args.module, 136 host=args.host, 137 port=args.port, 138 force_polling=args.force_polling, 139 extra_paths=args.watch_extra_path, 140 livereload_host=args.livereload_host 141 if args.livereload_host != "(host)" 142 else args.host, 143 livereload_port=args.livereload_port, 144 ) 145 except KeyboardInterrupt: 146 print("\nExiting...")
def
cli():
149def cli(): 150 """ 151 Command-line tool to translate HTML to Python code using html_compose 152 153 This function reads from stdin by default, but accepts an optional filename as argument 154 """ 155 HTML_CONVERT = "convert" 156 parser = argparse.ArgumentParser(description="html-compose cli") 157 subparsers = parser.add_subparsers(dest="command") 158 159 html_parser = subparsers.add_parser( 160 HTML_CONVERT, help="Translate HTML to html-compose" 161 ) 162 parse_html_translate(html_parser) 163 gallery_parser = subparsers.add_parser( 164 "gallery", help="Run an HTML gallery server" 165 ) 166 parse_gallery(gallery_parser) 167 args = parser.parse_args() 168 if args.command == HTML_CONVERT: 169 from_html(args) 170 elif args.command == "gallery": 171 gallery(args) 172 else: 173 parser.print_help()
Command-line tool to translate HTML to Python code using html_compose
This function reads from stdin by default, but accepts an optional filename as argument