Skip to content

Commit

Permalink
Allow version to be imported without importing the main module
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquegemignani committed Apr 30, 2022
1 parent a64b22d commit eb93c55
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
9 changes: 8 additions & 1 deletion open_dread_rando/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
from .dread_patcher import patch, patch_with_status_update
from pathlib import Path

from .patch_util import patch_with_status_update


def patch(input_path: Path, output_path: Path, configuration: dict):
from .dread_patcher import patch_extracted
return patch_extracted(input_path, output_path, configuration)
2 changes: 1 addition & 1 deletion open_dread_rando/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main():
with args.input_json.open() as f:
configuration = json.load(f)

dread_patcher.patch(
dread_patcher.patch_extracted(
args.input_path,
args.output_path,
configuration,
Expand Down
49 changes: 1 addition & 48 deletions open_dread_rando/dread_patcher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import shutil
import typing
from pathlib import Path
Expand Down Expand Up @@ -104,7 +103,7 @@ def add_custom_files(editor: PatcherEditor):
editor.add_new_asset(str(relative), child.read_bytes(), [])


def patch(input_path: Path, output_path: Path, configuration: dict):
def patch_extracted(input_path: Path, output_path: Path, configuration: dict):
LOG.info("Will patch files from %s", input_path)

DefaultValidatingDraft7Validator(_read_schema()).validate(configuration)
Expand Down Expand Up @@ -179,49 +178,3 @@ def patch(input_path: Path, output_path: Path, configuration: dict):
LOG.info("Done")


def patch_with_status_update(input_path: Path, output_path: Path, configuration: dict,
status_update: typing.Callable[[float, str], None]):
total_logs = 108

class StatusUpdateHandler(logging.Handler):
count = 0

def emit(self, record: logging.LogRecord) -> None:
message = self.format(record)

# Ignore "Writing <...>.pkg" messages, since there's also Updating...
if message.startswith("Writing ") and message.endswith(".pkg"):
return

# Encoding a bmsad is quick, skip these
if message.endswith(".bmsad"):
return

# These can come up frequently and are benign
if message.startswith("Skipping extracted file"):
return

self.count += 1
status_update(self.count / total_logs, message)

new_handler = StatusUpdateHandler()
# new_handler.setFormatter(logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s'))
tree_editor_log = logging.getLogger("mercury_engine_data_structures.file_tree_editor")

try:
tree_editor_log.setLevel(logging.DEBUG)
tree_editor_log.handlers.insert(0, new_handler)
tree_editor_log.propagate = False
LOG.setLevel(logging.INFO)
LOG.handlers.insert(0, new_handler)
LOG.propagate = False

patch(input_path, output_path, configuration)
if new_handler.count < total_logs:
status_update(1, f"Done was {new_handler.count}")

finally:
tree_editor_log.removeHandler(new_handler)
tree_editor_log.propagate = True
LOG.removeHandler(new_handler)
LOG.propagate = True
54 changes: 54 additions & 0 deletions open_dread_rando/patch_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging
import typing
from pathlib import Path

from open_dread_rando.logger import LOG


def patch_with_status_update(input_path: Path, output_path: Path, configuration: dict,
status_update: typing.Callable[[float, str], None]):
from open_dread_rando.dread_patcher import patch_extracted
total_logs = 108

class StatusUpdateHandler(logging.Handler):
count = 0

def emit(self, record: logging.LogRecord) -> None:
message = self.format(record)

# Ignore "Writing <...>.pkg" messages, since there's also Updating...
if message.startswith("Writing ") and message.endswith(".pkg"):
return

# Encoding a bmsad is quick, skip these
if message.endswith(".bmsad"):
return

# These can come up frequently and are benign
if message.startswith("Skipping extracted file"):
return

self.count += 1
status_update(self.count / total_logs, message)

new_handler = StatusUpdateHandler()
# new_handler.setFormatter(logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s'))
tree_editor_log = logging.getLogger("mercury_engine_data_structures.file_tree_editor")

try:
tree_editor_log.setLevel(logging.DEBUG)
tree_editor_log.handlers.insert(0, new_handler)
tree_editor_log.propagate = False
LOG.setLevel(logging.INFO)
LOG.handlers.insert(0, new_handler)
LOG.propagate = False

patch_extracted(input_path, output_path, configuration)
if new_handler.count < total_logs:
status_update(1, f"Done was {new_handler.count}")

finally:
tree_editor_log.removeHandler(new_handler)
tree_editor_log.propagate = True
LOG.removeHandler(new_handler)
LOG.propagate = True

0 comments on commit eb93c55

Please sign in to comment.