Skip to content

Commit

Permalink
add generic cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Mar 13, 2024
1 parent 15d4770 commit def0e4b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
26 changes: 19 additions & 7 deletions src/aexpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@


@dataclass
class CliContext:
class CliOptions:
verbose: int = 0
interact: bool = False
gzip: bool = False
compress: bool = False

def args(self):
verbose = min(5, max(0, self.verbose))
return (
(["-" + "v" * verbose] if verbose > 0 else [])
+ (["--interact"] if self.interact else [])
+ (["--gzip"] if self.compress else [])
)


@dataclass
class CliContext(CliOptions):
service: ServiceProvider = DEFAULT_SERVICE


Expand Down Expand Up @@ -155,7 +167,7 @@ def main(
clictx = ctx.ensure_object(CliContext)
clictx.verbose = verbose
clictx.interact = interact
clictx.gzip = gzip
clictx.compress = gzip

loggingLevel = {
0: logging.CRITICAL,
Expand Down Expand Up @@ -387,7 +399,7 @@ def preprocess(
)

result = context.product
StreamProductSaver(distribution, gzip=clictx.gzip).save(result, context.log)
StreamProductSaver(distribution, gzip=clictx.compress).save(result, context.log)

print(result.overview(), file=sys.stderr)
if clictx.interact:
Expand Down Expand Up @@ -531,7 +543,7 @@ def extract(

result = context.product

StreamProductSaver(description, gzip=clictx.gzip).save(result, context.log)
StreamProductSaver(description, gzip=clictx.compress).save(result, context.log)

print(result.overview(), file=sys.stderr)

Expand Down Expand Up @@ -581,7 +593,7 @@ def diff(ctx: click.Context, old: IO[bytes], new: IO[bytes], difference: IO[byte
context = clictx.service.diff(oldData, newData)

result = context.product
StreamProductSaver(difference, gzip=clictx.gzip).save(result, context.log)
StreamProductSaver(difference, gzip=clictx.compress).save(result, context.log)

print(result.overview(), file=sys.stderr)

Expand Down Expand Up @@ -613,7 +625,7 @@ def report(ctx: click.Context, difference: IO[bytes], report: IO[bytes]):
context = clictx.service.report(data)

result = context.product
StreamProductSaver(report, gzip=clictx.gzip).save(result, context.log)
StreamProductSaver(report, gzip=clictx.compress).save(result, context.log)

print(result.overview(), file=sys.stderr)
print(f"\n{result.content}", file=sys.stderr)
Expand Down
6 changes: 2 additions & 4 deletions src/aexpy/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def stat(ctx: click.Context, files: tuple[Path], output: IO[bytes]):
worker.count(files, context.product)

result = context.product
StreamProductSaver(output, gzip=clictx.gzip).save(result, context.log)
StreamProductSaver(output, gzip=clictx.compress).save(result, context.log)

print(result.overview(), file=sys.stderr)

Expand Down Expand Up @@ -103,10 +103,8 @@ def runimage(

worker = AexPyDockerWorker(
cwd=volume,
verbose=clictx.verbose,
compress=clictx.gzip,
cli=clictx,
tag=tag,
interact=clictx.interact,
)

result = worker.run(list(args), capture_output=False)
Expand Down
29 changes: 9 additions & 20 deletions src/aexpy/tools/workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Callable, override

from ... import __version__, getEnvironmentManager
from ...cli import CliOptions
from ...diffing import Differ
from ...extracting import Extractor
from ...io import StreamProductSaver
Expand Down Expand Up @@ -39,15 +40,11 @@ class AexPyWorker:
def __init__(
self,
/,
verbose: int = 0,
compress: bool = False,
cwd: Path | None = None,
interact: bool = False,
cli: CliOptions | None = None,
logger: Logger | None = None,
) -> None:
self.verbose = min(5, max(0, verbose))
self.compress = compress
self.interact = interact
self.cli = cli or CliOptions()
self.logger = logger or logging.getLogger()
self.cwd = cwd or Path(os.getcwd()).resolve()

Expand All @@ -58,13 +55,7 @@ def resolvePath(self, /, path: Path):
return path

def run(self, /, args: list[str], **kwargs) -> subprocess.CompletedProcess[bytes]:
args = (
self.getCommandPrefix()
+ (["-" + "v" * self.verbose] if self.verbose > 0 else [])
+ (["--interact"] if self.interact else [])
+ (["--gzip"] if self.compress else [])
+ args
)
args = self.getCommandPrefix() + self.cli.args() + args
self.logger.debug(f"Worker run args: {args}")

kwargs.setdefault("capture_output", True)
Expand All @@ -73,7 +64,7 @@ def run(self, /, args: list[str], **kwargs) -> subprocess.CompletedProcess[bytes
{
**os.environ,
"PYTHONUTF8": "1",
"AEXPY_GZIP_IO": "1" if self.compress else "0",
"AEXPY_GZIP_IO": "1" if self.cli.compress else "0",
"AEXPY_ENV_PROVIDER": getEnvironmentManager(),
},
)
Expand All @@ -90,7 +81,7 @@ def runParsedOutput[T: Product](self, /, type: type[T], args: list[str], **kwarg
res = self.run(args + ["-"], **kwargs)
result = AexPyResult[T](code=res.returncode, log=res.stderr, out=res.stdout)
try:
if self.compress:
if self.cli.compress:
result.data = type.model_validate_json(gzip.decompress(result.out))
else:
result.data = type.model_validate_json(result.out)
Expand Down Expand Up @@ -150,15 +141,13 @@ def defaultTag(cls):
def __init__(
self,
/,
verbose: int = 0,
compress: bool = False,
cwd: Path | None = None,
interact: bool = False,
cli: CliOptions | None = None,
logger: Logger | None = None,
*,
tag: str = "",
) -> None:
super().__init__(verbose, compress, cwd, interact, logger)
super().__init__(cwd, cli, logger)
self.tag = tag or self.defaultTag()

def getImageTag(self, /, version: str):
Expand Down Expand Up @@ -188,7 +177,7 @@ def getCommandPrefix(self, /):
user,
"--rm",
]
+ (["-t"] if self.interact else [])
+ (["-t"] if self.cli.interact else [])
+ [self.tag]
)

Expand Down

0 comments on commit def0e4b

Please sign in to comment.