Skip to content

Commit e8f6713

Browse files
committed
change default cli behavior, remove replace directive
1 parent 6f12d6d commit e8f6713

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ build
55
python_obfuscator.egg-info
66
.pytest_cache
77
.mypy_cache
8-
.ruff_cache
8+
.ruff_cache
9+
obfuscated/

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ pip install python-obfuscator
1818

1919
## Quickstart
2020

21-
Print out obfuscated code
21+
By default, obfuscated code is written under an **`obfuscated/`** directory (created next to your current working directory). Paths under the current directory are preserved (e.g. `src/app.py``obfuscated/src/app.py`).
22+
2223
```
2324
pyobfuscate -i your_file.py
2425
```
2526

26-
Apply changes to the input file
27+
Print to stdout instead (e.g. for piping):
28+
2729
```
28-
pyobfuscate -i your_file.py -r True
30+
pyobfuscate -i your_file.py --stdout
2931
```
3032

3133
## More Detailed Documentation
@@ -62,7 +64,7 @@ print("{} that's a great number!".format(user_value))
6264
[With `pyobfuscate -i file.py`](https://gist.github.com/davidteather/b6ff932140d8c174b9c6f50c9b42fdaf)
6365

6466

65-
[With `--one-liner True`](https://gist.github.com/davidteather/75e48c04bf74f0262fe2919239a74295)
67+
[With `--one-liner`](https://gist.github.com/davidteather/75e48c04bf74f0262fe2919239a74295)
6668

6769
## Authors
6870

python_obfuscator/cli/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
import python_obfuscator
77
from python_obfuscator.techniques import one_liner as one_liner_technique
88

9+
DEFAULT_OUTPUT_DIR = "obfuscated"
10+
11+
12+
def _resolved_output_path(input_path: Path) -> Path:
13+
"""Write under ./obfuscated/, preserving path relative to cwd when possible."""
14+
cwd = Path.cwd().resolve()
15+
try:
16+
rel = input_path.resolve().relative_to(cwd)
17+
except ValueError:
18+
rel = Path(input_path.name)
19+
out = cwd / DEFAULT_OUTPUT_DIR / rel
20+
out.parent.mkdir(parents=True, exist_ok=True)
21+
return out
22+
923

1024
def main(
1125
input_path: Annotated[
@@ -19,12 +33,11 @@ def main(
1933
readable=True,
2034
),
2135
],
22-
replace: Annotated[
36+
stdout: Annotated[
2337
bool,
2438
typer.Option(
25-
"--replace",
26-
"-r",
27-
help="Overwrite the input file with obfuscated code.",
39+
"--stdout",
40+
help="Print obfuscated code to stdout instead of writing a file.",
2841
),
2942
] = False,
3043
include_one_liner: Annotated[
@@ -44,10 +57,12 @@ def main(
4457
data = input_path.read_text()
4558
obfuscated_data = obfuscate.obfuscate(data, remove_techniques=remove)
4659

47-
if replace:
48-
input_path.write_text(obfuscated_data)
49-
else:
60+
if stdout:
5061
typer.echo(obfuscated_data)
62+
else:
63+
out_path = _resolved_output_path(input_path)
64+
out_path.write_text(obfuscated_data)
65+
typer.secho(f"Wrote {out_path}", err=True)
5166

5267

5368
def cli() -> None:

0 commit comments

Comments
 (0)