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)[source]

Centre string for a logger

Parameters:
  • str_to_centre – String to centre

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

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

Post a message to Discord

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_file_pattern(str_to_match)[source]
romsearch.util.get_file_time(f, datetime_format)[source]

Get created file time from the file itself

romsearch.util.get_game_name(f)[source]

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

romsearch.util.get_parent_name(game_name, dupe_dict)[source]

Get the parent name recursively searching through a dupe dict

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)[source]

Left-align string for a logger

Parameters:
  • str_to_align – String to align

  • total_length – Total length of the string

romsearch.util.load_json(file)[source]

Load json file

romsearch.util.load_yml(f)[source]

Load YAML file

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

Save json in a pretty way

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