Skip to content

Commit

Permalink
Merge pull request #31 from bbtufty/romchooser-logging
Browse files Browse the repository at this point in the history
Improved ROMChooser logging
  • Loading branch information
bbtufty authored Aug 28, 2024
2 parents f265c56 + 52abe0b commit 5cc6768
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
75 changes: 47 additions & 28 deletions romsearch/modules/romchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def __init__(self,
default_config=None,
regex_config=None,
logger=None,
log_line_sep="=",
log_line_length=100,
):
"""ROM choose tool
Expand All @@ -299,6 +301,7 @@ def __init__(self,
default_config (dict, optional): Default configuration dictionary. Defaults to None.
regex_config (dict, optional): Configuration dictionary. Defaults to None.
logger (logging.Logger, optional): Logger instance. Defaults to None.
log_line_length (int, optional): Line length of log. Defaults to 100
"""

if platform is None:
Expand Down Expand Up @@ -381,17 +384,28 @@ def __init__(self,

self.dry_run = self.config.get("romchooser", {}).get("dry_run", False)

self.log_line_sep = log_line_sep
self.log_line_length = log_line_length

def run(self,
rom_dict):
"""Run the ROM chooser"""

self.logger.info(f"{self.log_line_sep * self.log_line_length}")
self.logger.info(centred_string(f"Running ROMChooser for {self.game}",
total_length=self.log_line_length)
)
self.logger.info(f"{self.log_line_sep * self.log_line_length}")

rom_dict = self.run_chooser(rom_dict)

self.print_summary(rom_dict)

# Filter out excluded ROMs before we return
rom_dict = {key: rom_dict[key] for key in rom_dict if not rom_dict[key]["excluded"]}

self.logger.info(f"{self.log_line_sep * self.log_line_length}")

return rom_dict

def run_chooser(self,
Expand All @@ -418,7 +432,9 @@ def run_chooser(self,
rom_dict[r]["excluded_reason"] = []

for f in self.dat_filters:
self.logger.debug(f"Filtering {f}")
self.logger.debug(left_aligned_string(f"Filtering {f}",
total_length=self.log_line_length)
)
if f in DAT_FILTERS:
rom_dict = remove_rom_dict_entries(rom_dict,
f,
Expand All @@ -430,7 +446,9 @@ def run_chooser(self,

# Language
if self.filter_languages:
self.logger.debug("Filtering languages")
self.logger.debug(left_aligned_string(f"Filtering languages",
total_length=self.log_line_length)
)
rom_dict = remove_rom_dict_entries(rom_dict,
"languages",
remove_type="list",
Expand All @@ -439,7 +457,9 @@ def run_chooser(self,

# Regions
if self.filter_regions:
self.logger.debug("Filtering regions")
self.logger.debug(left_aligned_string(f"Filtering regions",
total_length=self.log_line_length)
)
rom_dict = remove_rom_dict_entries(rom_dict,
"regions",
remove_type="list",
Expand All @@ -448,7 +468,9 @@ def run_chooser(self,

# Remove versions we potentially don't want around
if self.use_best_version:
self.logger.debug("Getting best version")
self.logger.debug(left_aligned_string(f"Getting best version",
total_length=self.log_line_length)
)
rom_dict = get_best_version(rom_dict)
rom_dict = remove_unwanted_roms(rom_dict, key_to_check="improved_version", check_type="include")
rom_dict = remove_unwanted_roms(rom_dict, key_to_check="budget_edition", check_type="include")
Expand All @@ -460,7 +482,9 @@ def run_chooser(self,
)

if not self.allow_multiple_regions:
self.logger.debug("Trimming down to a single region")
self.logger.debug(left_aligned_string(f"Trimming down to a single region",
total_length=self.log_line_length)
)
rom_dict = filter_by_list(rom_dict,
"regions",
self.region_preferences,
Expand All @@ -470,49 +494,44 @@ def run_chooser(self,

def print_summary(self,
rom_dict,
total_length=100,
line_sep="=",
):
"""Log out a nice summary of what ROM has been chosen here
Args:
rom_dict (dict): the ROM dictionary to summarize
total_length (int): Total length for the logger line
line_sep (str): Line separator. Defaults to "="
"""

self.logger.info(f"{line_sep * total_length}")

# Game name
self.logger.info(centred_string(f"Running ROMChooser for {self.game}",
total_length=total_length)
)

self.logger.info(f"{line_sep * total_length}")

if len(rom_dict) == 0:
# Just say nothing found
self.logger.info(centred_string("No ROMs found", total_length=total_length))
self.logger.info(centred_string("No ROMs found",
total_length=self.log_line_length)
)
else:

# Start with found ROMs, then excluded ROMs and reasons
if np.sum([not rom_dict[r]["excluded"] for r in rom_dict]) > 0:
self.logger.info(centred_string("Included ROMs:", total_length=total_length))
self.logger.info(centred_string("Included ROMs:",
total_length=self.log_line_length)
)

for r in rom_dict:

# Don't include the excluded ones here
if rom_dict[r]["excluded"]:
continue

self.logger.info(left_aligned_string(f"-> {r}", total_length=total_length))
self.logger.info(left_aligned_string(f"-> {r}",
total_length=self.log_line_length)
)

if (np.sum([rom_dict[r]["excluded"] for r in rom_dict]) > 0
and np.sum([not rom_dict[r]["excluded"] for r in rom_dict]) > 0):
self.logger.info(f"{'-' * total_length}")
self.logger.info(f"{'-' * self.log_line_length}")

if np.sum([rom_dict[r]["excluded"] for r in rom_dict]) > 0:
self.logger.info(centred_string("Excluded ROMs:", total_length=total_length))
self.logger.info(centred_string("Excluded ROMs:",
total_length=self.log_line_length)
)

for r in rom_dict:

Expand All @@ -524,12 +543,12 @@ def print_summary(self,
exclusion_reason = rom_dict[r]["excluded_reason"]
exclusion_reason = [e.capitalize().replace("_", " ") for e in exclusion_reason]

self.logger.info(left_aligned_string(f"-> {r}", total_length=total_length))
self.logger.info(left_aligned_string(f"---> Reason(s): {', '.join(exclusion_reason)}",
total_length=total_length)
self.logger.info(left_aligned_string(f"-> {r}",
total_length=self.log_line_length)
)
self.logger.info(left_aligned_string(f"--> Reason(s): {', '.join(exclusion_reason)}",
total_length=self.log_line_length)
)

self.logger.info(f"{line_sep * total_length}")

return True

Expand Down
1 change: 1 addition & 0 deletions romsearch/modules/romsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def run(self,
regex_config=self.regex_config,
default_config=self.default_config,
logger=self.logger,
log_line_length=log_line_length,
)
rom_dict = chooser.run(rom_dict)

Expand Down

0 comments on commit 5cc6768

Please sign in to comment.