-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from MohammadrezaAmani/inui-1.0.0.1-query
fit(CLI): add cli tools to code.
- Loading branch information
Showing
9 changed files
with
249 additions
and
95 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
Binary file not shown.
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,20 @@ | ||
import logging | ||
import importlib | ||
|
||
logging.basicConfig( | ||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | ||
) | ||
|
||
|
||
def build(module, variable, out: str | None, _print: bool = True): | ||
lib = importlib.import_module(module) | ||
|
||
rendered = str(getattr(lib, variable)) | ||
if out: | ||
with open(out, "w", encoding="utf-8") as f: | ||
f.write(rendered) | ||
return rendered | ||
else: | ||
if _print: | ||
print(rendered) | ||
return rendered |
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,146 @@ | ||
import argparse | ||
import sys | ||
from inui.hotreload import hot_reload | ||
from inui import build | ||
from inui.toinui import convert | ||
import logging | ||
|
||
logging.basicConfig( | ||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | ||
) | ||
COMMANDS = { | ||
"run": { | ||
"aliases": ["r"], | ||
"help": "Run Code.", | ||
"args": [ | ||
{ | ||
"name": "file", | ||
"help": "Target file in the format module:content or path.to.module:content", | ||
}, | ||
{"name": "--host", "default": "127.0.0.1", "help": "Host to bind to"}, | ||
{"name": "--port", "type": int, "default": 8000, "help": "Port to bind to"}, | ||
{ | ||
"name": "--reload", | ||
"type": bool, | ||
"default": True, | ||
"help": "Use Hot Reload", | ||
}, | ||
{ | ||
"name": "--time", | ||
"type": float, | ||
"default": 0.5, | ||
"help": "HotReload sleep time.", | ||
}, | ||
{ | ||
"name": "--static-dir", | ||
"type": str, | ||
"default": ".", | ||
"help": "Static files directory", | ||
}, | ||
], | ||
}, | ||
"build": { | ||
"aliases": ["b"], | ||
"help": "Build the application", | ||
"args": [ | ||
{"name": "file", "help": "Target file in the format module:content"}, | ||
{ | ||
"name": "--out", | ||
"required": False, | ||
"default": None, | ||
"help": "Output path", | ||
}, | ||
], | ||
}, | ||
"convert": { | ||
"aliases": ["c"], | ||
"help": "Convert HTML to INUI.", | ||
"args": [ | ||
{"name": "--html", "required": False, "help": "HTML content as a string"}, | ||
{ | ||
"name": "--url", | ||
"required": False, | ||
"help": "URL to fetch HTML content from", | ||
}, | ||
{"name": "--file_name", "required": False, "help": "Path to the HTML file"}, | ||
{ | ||
"name": "--out", | ||
"required": False, | ||
"help": "Path to save the output file", | ||
}, | ||
{ | ||
"name": "--indent", | ||
"type": int, | ||
"required": False, | ||
"default": 4, | ||
"help": "Indentation level for output", | ||
}, | ||
], | ||
}, | ||
} | ||
|
||
|
||
def create_parser(): | ||
parser = argparse.ArgumentParser( | ||
prog="inui", | ||
description="CLI for INUI: A Powerful and Highly Customizable Python Library for UI", | ||
) | ||
subparsers = parser.add_subparsers( | ||
dest="command", required=True, help="Available subcommands" | ||
) | ||
|
||
alias_map = {} | ||
for cmd, config in COMMANDS.items(): | ||
alias_list = ", ".join(config["aliases"]) | ||
subparser = subparsers.add_parser( | ||
cmd, | ||
help=f"{config['help']} (aliases: {alias_list})", | ||
description=f"{config['help']} (aliases: {alias_list})", | ||
) | ||
for arg in config["args"]: | ||
subparser.add_argument(arg.pop("name"), **arg) | ||
for alias in config["aliases"]: | ||
alias_map[alias] = cmd | ||
|
||
return parser, alias_map | ||
|
||
|
||
def main(): | ||
parser, alias_map = create_parser() | ||
|
||
if len(sys.argv) > 1 and sys.argv[1] in alias_map: | ||
sys.argv[1] = alias_map[sys.argv[1]] | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == "run": | ||
module_name, variable_name = args.file.split(":") | ||
|
||
hot_reload( | ||
host=args.host, | ||
port=args.port, | ||
module=module_name, | ||
variable_name=variable_name, | ||
sleep_time=args.time, | ||
static_dir=args.static_dir, | ||
) | ||
if args.command == "build": | ||
module_name, variable_name = args.file.split(":") | ||
build( | ||
module=module_name, | ||
variable=variable_name, | ||
out=args.out, | ||
) | ||
|
||
if args.command == "convert": | ||
convert( | ||
html=args.html, | ||
url=args.url, | ||
file_name=args.file_name, | ||
save_to=args.out, | ||
indent=args.indent, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
Oops, something went wrong.