Utils

This simply contains a number of useful utilities that are shared between the various ROMSearch modules. In particular, we use centralised code for reading of files, logging, string matching using regex, and Discord posting.

API

romsearch.util.centred_string(str_to_centre, total_length=80, str_prefix='')[source]

Centre string for a logger

Parameters:
  • str_to_centre – String to centre

  • total_length – Total length of the string. Defaults to 80.

  • str_prefix – Will include this at the start of any string. Defaults to “”

romsearch.util.discord_push(url, name, fields)[source]

Post a message to Discord

romsearch.util.format_dat(dat)[source]

Format dat into a nice dictionary

romsearch.util.get_bracketed_file_pattern(str_to_match)[source]

Get file pattern to match bracketed things

This is a little tricky since sometimes things can be matched a little unusually. What we do here is allow for matches of letters, commas or spaces before and after, but ensure there’s no text immediately after the match. So “De” will match for a language but not “Demo”

romsearch.util.get_dat(dat_file_name)[source]

Parse the dat file to a raw dictionary from a zip file

romsearch.util.get_directory_name(f)[source]

Get the output directory name, which is just everything up to the first bracket

romsearch.util.get_disc_free_name(f, regex_config=None, default_config=None)[source]

Get disc-free game name from the ROM file naming convention

romsearch.util.get_file_pattern(str_to_match)[source]
romsearch.util.get_file_time(f, datetime_format='%Y/%m/%d, %H:%M:%S', return_as_str=True)[source]

Get created file time from the file itself

Parameters:
  • f (str) – Filename

  • datetime_format (str, optional) – Date and time format. Defaults to “%Y/%m/%d %H:%M:%S”

  • return_as_str (bool, optional) – Return string or full datetime. Defaults to True

romsearch.util.get_region_free_name(f, regex_config=None, default_config=None)[source]

Get region-free game name from the ROM file naming convention

romsearch.util.get_sanitized_version(ver)[source]

Get a sanitized version for potentially weird versioning

Parameters:

ver – version to sanitize

romsearch.util.get_short_name(f, regex_config=None, default_config=None)[source]

Get short game name from the ROM file naming convention

romsearch.util.left_aligned_string(str_to_align, total_length=80, str_prefix='')[source]

Left-align string for a logger

Parameters:
  • str_to_align – String to align

  • total_length – Total length of the string

  • str_prefix – Will include this at the start of any string. Defaults to “”

romsearch.util.load_json(file)[source]

Load json file

romsearch.util.load_yml(f)[source]

Load YAML file

romsearch.util.match_retool_search_terms(full_name, search_term, short_name=None, region_free_name=None, match_type=None)[source]

Match a name against a search term, given retool’s matching rules

Parameters:
  • full_name (str) – Full name for the ROM

  • search_term (str) – Search term to match against

  • short_name (str) – Short name to match against. Defaults to None, which inherits the full name

  • region_free_name (str) – Region free name to match against. Defaults to None, which inherits the full name

  • match_type (str) – Type of matching. Defaults to None, which will match against short name

romsearch.util.normalize_name(f, disc_rename=None)[source]

Normalize a name to standard form

Currently, just normalizes the disc name

Parameters:
  • f (str) – Name to normalize

  • disc_rename (dict, optional) – Disc rename mappings. Defaults to None.

romsearch.util.remove_case_insensitive_matches(file_to_match, pattern, path='.')[source]

Remove files that match, aside from upper/lowercase

Filesystems can be a bit flaky here about compatibility, so force this through

Parameters:
  • file_to_match (str) – Path to file to match, without extension

  • pattern (str) – glob pattern to match

  • path (str) – directory to look for files in. Defaults to ‘.’

romsearch.util.save_json(data, out_file, sort_key=None)[source]

Save json in a pretty way

Parameters:
  • data (dict) – Data to be saved

  • out_file (str) – Path to JSON file

  • sort_key (str) – Key within each dictionary entry to sort by. Default is None, which will not sort.

romsearch.util.save_yml(f, data)[source]

Save YAML file

romsearch.util.setup_logger(log_level, script_name, log_dir, additional_dir='', max_logs=9)[source]

Set up the logger.

Parameters:
  • log_level (str) – The log level to use

  • script_name (str) – The name of the script

  • log_dir (str) – The directory to save logs to

  • additional_dir (str) – Any additional directories to keep log files tidy

  • max_logs (int) – Maximum number of log files to keep

Returns:

A logger object for logging messages.

romsearch.util.split(full_list, chunk_size=10)[source]

Split a list in chunks of size chunk_size

Parameters:
  • full_list (list) – list to split

  • chunk_size (int, optional) – size of each chunk. Defaults to 10

romsearch.util.unzip_file(zip_file_name, out_dir)[source]

Unzip a file