Argument bugfixes, and minification!
This commit is contained in:
parent
72b7f87dd7
commit
6f8b5a4e96
6 changed files with 1768 additions and 13 deletions
|
@ -8,7 +8,7 @@ def a():
|
||||||
yield 1
|
yield 1
|
||||||
+> 2
|
+> 2
|
||||||
def b():
|
def b():
|
||||||
+>> a()
|
yield from a()
|
||||||
+>> a()
|
+>> a()
|
||||||
b() |> list |> print
|
b() |> list |> print
|
||||||
a = 12 |> fibonacci
|
a = 12 |> fibonacci
|
||||||
|
|
|
@ -1,9 +1,21 @@
|
||||||
import tokenize, io, typer, typing, os, sys, pathlib, time
|
import tokenize, io, typer, typing, os, sys, pathlib, traceback
|
||||||
|
|
||||||
|
try:
|
||||||
|
import python_minifier as minifier
|
||||||
|
except ImportError as e:
|
||||||
|
traceback.print_exception(e)
|
||||||
|
minifier_avail = False
|
||||||
|
else:
|
||||||
|
minifier_avail = True
|
||||||
|
|
||||||
from std.ipathlib import Path
|
from std.ipathlib import Path
|
||||||
def patch_std(prefix: str):
|
def patch_std(prefix: str):
|
||||||
for i in (Path(__file__).parent.parent / 'std').iterdir():
|
for i in (Path(__file__).parent.parent / 'std').iterdir():
|
||||||
if i.is_dir(): continue
|
if i.is_dir(): continue
|
||||||
prefix = prefix.replace(f"std'{i.name}'", "'''" + i.read_text().replace("'''", "\\'''") + "'''")
|
module = i.read_text()
|
||||||
|
prefix = prefix.replace(f"std'{i.name}'", "'''" + module.replace("'''", "\\'''") + "'''")
|
||||||
|
|
||||||
|
|
||||||
return prefix
|
return prefix
|
||||||
|
|
||||||
PREFIX = patch_std((Path(__file__).parent / 'prefix.py').read_text())
|
PREFIX = patch_std((Path(__file__).parent / 'prefix.py').read_text())
|
||||||
|
@ -37,12 +49,12 @@ def translate(file: io.StringIO, debug: int = 0):
|
||||||
yield tokenize.NAME, name
|
yield tokenize.NAME, name
|
||||||
yield tokenize.OP, "<<"
|
yield tokenize.OP, "<<"
|
||||||
def dprint(data: str, prio: int):
|
def dprint(data: str, prio: int):
|
||||||
if debug > prio:
|
if debug < prio:
|
||||||
return
|
return
|
||||||
print(data)
|
print(data)
|
||||||
fdata = file.read()
|
fdata = file.read()
|
||||||
filedata = PREFIX + fdata
|
filedata = PREFIX + fdata
|
||||||
dprint(f'---START FILE---\nname: {file.name}, len: {len(fdata)}\n' + filedata + '\n---END FILE---\n', 4)
|
dprint(f'---START FILE---\nname: {file.name}, len: {len(fdata)}\n' + filedata + '\n---END FILE---\n', 5)
|
||||||
patched_file = io.StringIO(filedata)
|
patched_file = io.StringIO(filedata)
|
||||||
|
|
||||||
skip_token = 0
|
skip_token = 0
|
||||||
|
@ -135,7 +147,7 @@ def translate(file: io.StringIO, debug: int = 0):
|
||||||
yield type,name
|
yield type,name
|
||||||
dprint(f'---END DEBOUT---', 4)
|
dprint(f'---END DEBOUT---', 4)
|
||||||
|
|
||||||
def transpile(input_path: Path, verbose: int) -> None:
|
def transpile(input_path: Path, verbosity: int, minify: bool) -> None:
|
||||||
dir = Path('dist')
|
dir = Path('dist')
|
||||||
if input_path.is_dir():
|
if input_path.is_dir():
|
||||||
for i in input_path.glob('*'):
|
for i in input_path.glob('*'):
|
||||||
|
@ -143,22 +155,38 @@ def transpile(input_path: Path, verbose: int) -> None:
|
||||||
path = (dir / i.relative_to(input_path)).with_suffix('.py')
|
path = (dir / i.relative_to(input_path)).with_suffix('.py')
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with i.open() as f:
|
with i.open() as f:
|
||||||
path.write_text(tokenize.untokenize(translate(f, verbose)))
|
transpiled = tokenize.untokenize(translate(f, verbosity))
|
||||||
|
if minify:
|
||||||
|
if not minifier_avail:
|
||||||
|
print('ERROR: Minifier not available.')
|
||||||
|
return
|
||||||
|
transpiled = minifier.minify(transpiled)
|
||||||
|
path.write_text(transpiled)
|
||||||
else:
|
else:
|
||||||
with input_path.open() as f:
|
with input_path.open() as f:
|
||||||
input_path.with_suffix('.py').write_text(tokenize.untokenize(translate(f, verbose)))
|
transpiled = tokenize.untokenize(translate(f, verbosity))
|
||||||
|
if minify:
|
||||||
|
if not minifier_avail:
|
||||||
|
print('ERROR: Minifier not available.')
|
||||||
|
return
|
||||||
|
transpiled = minifier.minify(transpiled)
|
||||||
|
input_path.with_suffix('.py').write_text(transpiled)
|
||||||
|
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
|
|
||||||
|
verbosity_arg = typing.Annotated[int, typer.Option('--verbosity', '-v')]
|
||||||
|
minify_arg = typing.Annotated[bool, typer.Option('--minify', '-m')]
|
||||||
|
|
||||||
@app.command('t')
|
@app.command('t')
|
||||||
@app.command('ts')
|
@app.command('ts')
|
||||||
@app.command('transpile')
|
@app.command('transpile')
|
||||||
def transpile_cmd(input_path: pathlib.Path, verbose: int = 0) -> None:
|
def transpile_cmd(input_path: pathlib.Path, verbosity: verbosity_arg = 0, minify: minify_arg = False) -> None:
|
||||||
transpile(input_path, verbose)
|
transpile(input_path, verbosity, minify)
|
||||||
@app.command('r')
|
@app.command('r')
|
||||||
@app.command('run')
|
@app.command('run')
|
||||||
def run_cmd(input_path: pathlib.Path, verbose: int = 0) -> None:
|
def run_cmd(input_path: pathlib.Path, verbosity: verbosity_arg = 0, minify: minify_arg = False) -> None:
|
||||||
input_path = Path(input_path)
|
input_path = Path(input_path)
|
||||||
transpile(input_path, verbose)
|
transpile(input_path, verbosity, minify)
|
||||||
if input_path.is_dir():
|
if input_path.is_dir():
|
||||||
os.system(f'{sys.executable} -m dist')
|
os.system(f'{sys.executable} -m dist')
|
||||||
Path('dist').rmtree()
|
Path('dist').rmtree()
|
||||||
|
|
|
@ -5,3 +5,6 @@ description = "Add your description here"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.12"
|
requires-python = ">=3.12"
|
||||||
dependencies = ['typer']
|
dependencies = ['typer']
|
||||||
|
[project.optional-dependencies]
|
||||||
|
minifier = ['python-minifier']
|
||||||
|
all = ['python-minifier']
|
Binary file not shown.
23
uv.lock
generated
23
uv.lock
generated
|
@ -30,8 +30,20 @@ dependencies = [
|
||||||
{ name = "typer" },
|
{ name = "typer" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[package.optional-dependencies]
|
||||||
|
all = [
|
||||||
|
{ name = "python-minifier" },
|
||||||
|
]
|
||||||
|
minifier = [
|
||||||
|
{ name = "python-minifier" },
|
||||||
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [{ name = "typer" }]
|
requires-dist = [
|
||||||
|
{ name = "python-minifier", marker = "extra == 'all'" },
|
||||||
|
{ name = "python-minifier", marker = "extra == 'minifier'" },
|
||||||
|
{ name = "typer" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "markdown-it-py"
|
name = "markdown-it-py"
|
||||||
|
@ -63,6 +75,15 @@ wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/f7/3f/01c8b82017c199075f8f788d0d906b9ffbbc5a47dc9918a945e13d5a2bda/pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a", size = 1205513 },
|
{ url = "https://files.pythonhosted.org/packages/f7/3f/01c8b82017c199075f8f788d0d906b9ffbbc5a47dc9918a945e13d5a2bda/pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a", size = 1205513 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "python-minifier"
|
||||||
|
version = "2.11.3"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/39/63/403fb2d6394b3e455e046d91f64b96072803aaf119027a26e716ed94d63c/python_minifier-2.11.3.tar.gz", hash = "sha256:489133b91212ec9658a7b64d243eb9eb67d7e53faf2ac5166a33301c61b3dcab", size = 64438 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/53/32/61d20860d18afb81cb7258bb02d4eaf4b09170383c2374514f6aef384fa9/python_minifier-2.11.3-py3-none-any.whl", hash = "sha256:37e10e9e318be701eecb48764942426be73ae9f562d75bea4e29c5f66945ce97", size = 56172 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rich"
|
name = "rich"
|
||||||
version = "13.9.4"
|
version = "13.9.4"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue