Skip to content

Commit

Permalink
checkout hash_cmd.py from main (hash_cmd --rotations is handled in a …
Browse files Browse the repository at this point in the history
…different PR)
  • Loading branch information
haianhng31 committed Nov 4, 2024
1 parent d841f60 commit 3b83a41
Showing 1 changed file with 5 additions and 39 deletions.
44 changes: 5 additions & 39 deletions python-threatexchange/threatexchange/cli/hash_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
import argparse
import pathlib
import typing as t
import tempfile

from threatexchange import common
from threatexchange.cli.cli_config import CLISettings
from threatexchange.cli.exceptions import CommandError
from threatexchange.content_type.content_base import ContentType
from threatexchange.content_type.photo import PhotoContent

from threatexchange.signal_type.signal_base import FileHasher, SignalType
from threatexchange.cli import command_base
Expand Down Expand Up @@ -79,27 +76,17 @@ def init_argparse(cls, settings: CLISettings, ap: argparse.ArgumentParser) -> No
help="only generate these signal types",
)

ap.add_argument(
"--rotations",
"--R",
action="store_true",
help="for photos, generate all 8 simple rotations",
)

def __init__(
self,
content_type: t.Type[ContentType],
signal_type: t.Optional[t.Type[SignalType]],
files: t.List[pathlib.Path],
rotations: bool = False,
) -> None:
self.content_type = content_type
self.signal_type = signal_type

self.files = files

self.rotations = rotations

def execute(self, settings: CLISettings) -> None:
hashers = [
s
Expand All @@ -112,31 +99,10 @@ def execute(self, settings: CLISettings) -> None:
f"{self.signal_type.get_name()} "
f"does not apply to {self.content_type.get_name()}"
)

hashers = [self.signal_type] # type: ignore # can't detect intersection types

if self.rotations and not issubclass(self.content_type, PhotoContent):
raise CommandError(
"--rotations flag is only available for Photo content type", 2
)

if self.rotations and issubclass(self.content_type, PhotoContent):
for file in self.files:
with open(file, "rb") as f:
image_bytes = f.read()
rotated_images = PhotoContent.all_simple_rotations(image_bytes)
for _, rotated_bytes in rotated_images.items():
with tempfile.NamedTemporaryFile() as temp_file: # Create a temporary file to hold the byte data
temp_file.write(rotated_bytes)
temp_file_path = pathlib.Path(temp_file.name)
for hasher in hashers:
hash_str = hasher.hash_from_file(temp_file_path)
if hash_str:
print(hasher.get_name(), hash_str)

else:
for file in self.files:
for hasher in hashers:
hash_str = hasher.hash_from_file(file)
if hash_str:
print(hasher.get_name(), hash_str)
for file in self.files:
for hasher in hashers:
hash_str = hasher.hash_from_file(file)
if hash_str:
print(hasher.get_name(), hash_str)

0 comments on commit 3b83a41

Please sign in to comment.