Skip to content

Commit eb523fc

Browse files
committed
add: format to output
1 parent e004f69 commit eb523fc

8 files changed

Lines changed: 74 additions & 2 deletions

File tree

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
recursive-include pof *.py
22
include pof/utils/generator/names.txt
3+
include pof/utils/generator/comments.txt
34
include pof/utils/se/homoglyphs.txt
45

56
recursive-include tests *

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ The `out2.py` and `out3.py` files are identical, they both contain the source co
571571
> [!NOTE]
572572
> By default pof uses a custom `Untokenizer` that removes useless spaces (`NoSpaceUntokenizer` defined in `./pof/utils/tokens.py`), so first generation (in the example `out.py`) will not have spaces present in the subsquent outputs.
573573
574+
### Format
575+
576+
You can choose to automatically format the output code using black.
577+
578+
From the CLI add the `--format` flag.
579+
580+
From lib:
581+
582+
```py
583+
from pof.utils.format import black_format
584+
585+
obf = ExampleObfuscator().obfuscate(...)
586+
out = black_format(obf)
587+
print(out)
588+
```
589+
574590
### Generators
575591

576592
Generators are used to generate new names, they can be used to classes, variables, functions, constants or any other.

pof/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from pof.obfuscator import __all__ as all_obfuscator
2828
from pof.stager import * # noqa: F403
2929
from pof.stager import __all__ as all_stager
30+
from pof.utils.format import black_format
3031

3132
handler = logging.StreamHandler()
3233
formatter = logging.Formatter("%(levelname)s %(message)s\x1b[39m")
@@ -137,6 +138,9 @@ def _handle(args) -> int:
137138

138139
end = time.time()
139140

141+
if args.format:
142+
out = black_format(out)
143+
140144
time_diff = round(end - start, 4)
141145
logging.info(f"took: {time_diff}s")
142146
args.output.write(out)
@@ -175,6 +179,11 @@ def _cli() -> int:
175179
help="obfuscation function",
176180
default="obfuscate",
177181
)
182+
parser.add_argument(
183+
"--format",
184+
action="store_true",
185+
help="format output with black",
186+
)
178187
parser.add_argument(
179188
"--logging",
180189
help="logging level, DEBUG, INFO, ERROR, CRITICAL",

pof/cli_v2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pof.evasion import * # noqa: F403
2727
from pof.obfuscator import * # noqa: F403
2828
from pof.stager import * # noqa: F403
29+
from pof.utils.format import black_format
2930

3031
handler = logging.StreamHandler()
3132
formatter = logging.Formatter("%(levelname)s %(message)s\x1b[39m")
@@ -344,6 +345,9 @@ def _handle(args) -> int:
344345

345346
end = time.time()
346347

348+
if args.format_black:
349+
out = black_format(out)
350+
347351
time_diff = round(end - start, 4)
348352
logging.info("took: %ds", time_diff)
349353
args.output.write(out)
@@ -468,6 +472,11 @@ def _cli() -> int:
468472
parser.add_argument("--eva-uptime", action="store_true")
469473
parser.add_argument("--eva-utc", action="store_true")
470474

475+
# format
476+
parser.add_argument(
477+
"--format-black", action="store_true", help="format output with black"
478+
)
479+
471480
# control
472481
parser.add_argument(
473482
"--obf-count",

pof/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
class BaseObfuscator:
6666
@staticmethod
6767
def _get_tokens(source: str):
68-
# TODO: this is not safe, the \r could be inside a string, probably
68+
# TODO (deoktr): this is not safe, the \r could be inside a string, probably
6969
# should get tokens first, and update all the instances of newline
7070
if "\r" in source:
7171
source = source.replace("\r\n", "\n").replace("\r", "\n")

pof/utils/format.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# POF, a free and open source Python obfuscation framework.
2+
# Copyright (C) 2022 - 2025 POF Team
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
18+
import logging
19+
20+
try:
21+
import black
22+
23+
BLACK_INSTALLED = True
24+
except ImportError:
25+
BLACK_INSTALLED = False
26+
27+
from pof.errors import PofError
28+
29+
30+
def black_format(src: str) -> str:
31+
"""Format raw source code using black."""
32+
if not BLACK_INSTALLED:
33+
logging.error("'black' is not installed, cannot format using it")
34+
return src
35+
return black.format_str(src, mode=black.FileMode())

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "python-obfuscation-framework"
7-
version = "1.6.3"
7+
version = "1.7.0"
88
description = "Python Obfuscation Framework"
99
readme = "README.md"
1010
requires-python = ">=3.5"
@@ -32,6 +32,7 @@ classifiers = [
3232
dependencies = [
3333
"rope>=1.0.0",
3434
"Pillow>=10.0.0",
35+
"black>=24.10.0",
3536
]
3637
# dynamic = ["version"]
3738

server/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"pygments>=2.19.2",
3131
"python-obfuscation-framework>=1.7.0",
3232
"gunicorn>=23.0.0",
33+
"black==24.10.0",
3334
]
3435

3536
[project.optional-dependencies]

0 commit comments

Comments
 (0)