Skip to content

Commit

Permalink
Options to hide directories or anonymize the username in output logs (#7
Browse files Browse the repository at this point in the history
)

* --hide-dirs option

* --anonymize-user-dir option

* Update usage image in README

* update help screenshot
  • Loading branch information
michelcrypt4d4mus authored Oct 18, 2024
1 parent 18b5531 commit 1448e62
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# NEXT RELEASE
* New `--hide-dirs` option
* New `--anonymize-user-dir` option
* Bump `PyPDF` to `5.0.1`
* More default sorting rules

Expand Down
5 changes: 4 additions & 1 deletion clown_sort/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Config:
leave_in_place: bool = False
screenshots_only: bool = True
delete_originals: bool = False
hide_dirs: bool = False
rescan_sorted: bool = False
yes_overwrite: bool = False
print_as_parsed: bool = False
Expand All @@ -50,9 +51,11 @@ def configure(cls):
cls.enable_debug_mode()

Config.filename_regex = re.compile(args.filename_regex)
Config.anonymize_user_dir = True if args.anonymize_user_dir else False
Config.hide_dirs = True if args.hide_dirs else False
Config.delete_originals = True if args.delete_originals else False
Config.leave_in_place = True if args.leave_in_place else False
Config.only_if_match = True if args.only_if_match else False
Config.delete_originals = True if args.delete_originals else False
Config.rescan_sorted = True if args.rescan_sorted else False
Config.yes_overwrite = True if args.yes_overwrite else False

Expand Down
27 changes: 16 additions & 11 deletions clown_sort/files/sortable_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
from clown_sort.filename_extractor import FilenameExtractor
from clown_sort.lib.rule_match import RuleMatch
from clown_sort.sort_selector import process_file_with_popup
from clown_sort.util.filesystem_helper import copy_file_creation_time
from clown_sort.util.filesystem_helper import copy_file_creation_time, loggable_filename
from clown_sort.util.logging import log
from clown_sort.util.rich_helper import (bullet_text, comma_join, console, copying_file_log_message,
indented_bullet, mild_warning, moving_file_log_message, print_dim_bullet, print_headline, stderr_console)
from clown_sort.util.rich_helper import (bullet_text, comma_join, console,
copying_file_log_message, indented_bullet, mild_warning, moving_file_log_message,
print_dim_bullet, stderr_console)

MAX_EXTRACTION_LENGTH = 4096
NOT_MOVING_FILE = "Not moving file to processed dir because it's"
Expand Down Expand Up @@ -197,7 +198,9 @@ def _extracted_text_panel(self) -> Panel:
return Panel(self._extracted_str(MAX_EXTRACTION_LENGTH), expand=True, style='dim')

def _filename_panel(self) -> Panel:
return Panel(str(self.file_path), expand=False, style='bright_white reverse')
"""Panelized version of the filename for display."""
filename = loggable_filename(self.file_path, Config)
return Panel(filename, expand=False, style='bright_white reverse')

def _log_copy_file(self, destination_path: Path, match: Optional[re.Match] = None) -> None:
"""Log info about a file copy."""
Expand All @@ -209,15 +212,17 @@ def _log_copy_file(self, destination_path: Path, match: Optional[re.Match] = Non

if destination_path.parent == Config.destination_dir:
console.print(indented_bullet(log_msg.append('root sorted dir...')))
else:
log_msg.append(str(destination_path.parent), style='sort_destination')
return

dirname = loggable_filename(destination_path.parent, Config)
log_msg.append(str(dirname), style='sort_destination')

if match is not None:
log_msg.append(f" (matched '", style='dim')
log_msg.append(match.group(0).strip(), style='magenta dim')
log_msg.append("')", style='dim')
if match is not None:
log_msg.append(f" (matched '", style='dim')
log_msg.append(match.group(0).strip(), style='magenta dim')
log_msg.append("')", style='dim')

console.print(indented_bullet(log_msg))
console.print(indented_bullet(log_msg))

def _move_to_processed_dir(self) -> None:
"""Relocate the original file to the [SCREENSHOTS_DIR]/Processed/ folder."""
Expand Down
4 changes: 2 additions & 2 deletions clown_sort/sorting_rules/crypto.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
folder,regex
1mdb,1mdb|jho[-\s.]*low|Victor[-\s.]*Hoo
3ac,\b3ac\b|Three[-\s.]*Arrows[-\s.]*Capital|su[-\s.]*zhu|zhu[-\s.]*su|kyle[-\s.]*davies|OPNX|Kyle[-\s.]*Davies
A16Z,a16z|Andreessen|Packym|Arianna[-\s1]*Simpson|Ben[-\s.]*Horowitz|\bAXS\b|\bAli[-\s.]*Yahya|Bitclout
A16Z,a16z|Andreessen|Packym|Arianna[-\s1]*Simpson|B(en)?[-\s.]*Horowitz|\bAXS\b|\bAli[-\s.]*Yahya|Bitclout
Aave,\b(Aave|Stani|Kulechov)\b
AAX,\bAAX(Exchange)?\b
AEX,\bAEX(Global)?\b
Expand Down Expand Up @@ -53,7 +53,7 @@ Changelly,Changelly
China,fentanyl|\bchin(a|ese)|Hong[-\s.]*Kong|BANK[-\s.]*OF[-\s.]*COMMUNICATIONS|Wan[-\s.]*Kuok|Broken[-\s.]*Tooth|baiyun91586947
Circle,[@#]circle|usdc|circle[-\s.]*internet|disparte|\ballaire|jerallaire|Macromedia
CMCC Global,CMCC[-\s.]*Global
Coinbase,Coinbase|\$COIN\b|brian[-\s.]*armstrong|\b(Aggarwal|grewal|remitly|Venovate)\b|Asiff[-\s.]*Hirji|\bbalaji|Fred[-\s.]*Ehrsam|\$RELY\b|Emilie[-\s.]*Choi\b|BuildOnBase|Digital[-\s.]*Wealth|Asiff[-\s.]*Hirji|Keystone[-\s.]*Capital
Coinbase,Coinbase|\$COIN\b|brian[-\s.]*armstrong|\b(Aggarwal|grewal|remitly|Venovate)\b|Asiff[-\s.]*Hirji|\bbalaji|Fred[-\s.]*Ehrsam|\$RELY\b|Emilie[-\s.]*Choi\b|BuildOnBase|Digital[-\s.]*Wealth|Asiff[-\s.]*Hirji|Keystone[-\s.]*Capital|jesse[-\s.]*pollak
CoinField,\b(CoinField|CFC|Manticore|FlaxNFT|Lightman)\b|Sologenic|\bBob[-\s.]\bRas\b
Coinflex,Mark[-\s.]*Lamb|Roger[-\s.]*Ver|coinflex|Doug[-\s.]*Polk|OPNX|flexusd|Leslie[-\s.]*Lamb|dougpolkvids
CoinLoan,coinloan|Faliushin\b
Expand Down
6 changes: 6 additions & 0 deletions clown_sort/util/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
parser.add_argument('-y', '--yes-overwrite', action='store_true',
help='skip confirmation prompt and always overwrite if a file with the same name already exists')

parser.add_argument('--hide-dirs', action='store_true',
help='hide directories in log output (only show basenames)')

parser.add_argument('--anonymize-user-dir', action='store_true',
help='anonymize the user directory in log output')

parser.add_argument('--rescan-sorted', action='store_true',
help="rescan already sorted files (useful if you updated your sorting rules)")

Expand Down
19 changes: 19 additions & 0 deletions clown_sort/util/filesystem_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import stat
import time
from datetime import datetime
from getpass import getuser
from os import path
from pathlib import Path
from typing import List, Optional, Union
Expand All @@ -26,6 +27,9 @@
SORTABLE_FILE_EXTENSIONS = IMAGE_FILE_EXTENSIONS + [PDF_EXTENSION, '.mov']
MAC_SCREENSHOT_TIMESTAMP_FORMAT = '%Y-%m-%d at %I.%M.%S %p'

ANONYMIZED_USERNAME = 'uzor'
CURRENT_USERNAME = getuser()


def files_in_dir(dir: Union[os.PathLike, str], with_extname: Optional[str] = None) -> List[str]:
"""Paths for non-hidden, non-directory files, optionally ending in 'with_extname'."""
Expand Down Expand Up @@ -127,6 +131,21 @@ def extract_timestamp_from_filename(filename: str) -> datetime:
return datetime.strptime(match.group(1), MAC_SCREENSHOT_TIMESTAMP_FORMAT)


def loggable_filename(file_path: Path, config: 'Config') -> str:
"""Return a loggable version of the filename."""
if config.hide_dirs:
return Path(file_path).name

filename = str(file_path)

if config.anonymize_user_dir:
username_str = f"{path.sep}{CURRENT_USERNAME}{path.sep}"
anonuser_str = f"{path.sep}{ANONYMIZED_USERNAME}{path.sep}"
return filename.replace(username_str, anonuser_str)
else:
return filename


def _non_hidden_files_in_dir(dir: Union[os.PathLike, str]) -> List[str]:
return [path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')]

Expand Down
Binary file modified doc/output_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/sort_screenshots_help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1448e62

Please sign in to comment.