def add_puzzles(puzzles): """ Add puzzles to the settings file. Creates a confirmation prompt if any duplicates are detected. `puzzles` is a dict of {name: puzzle dict} pairs. See :func:`add_puzzle` for a description of puzzle dicts. :param puzzles: A dict containing puzzles :type puzzles: dict :return: None """ existing_puzzles = read_puzzles() duplicates = {} not_duplicates = {} for name, puzzle in puzzles.items(): if name in existing_puzzles: duplicates.update({name: puzzle}) else: not_duplicates.update({name: puzzle}) def overwrite(): """ Write all puzzles, overwriting duplicates. :return: None """ existing_puzzles.update(puzzles) update_variables({'puzzles': existing_puzzles}) def no_overwrite(): """ Write all puzzles, but ignore duplicates. :return: None """ existing_puzzles.update(not_duplicates) update_variables({'puzzles': existing_puzzles}) if duplicates: prompts.YesNoPrompt(strings.label_names_exist.format( '\n'.join(duplicates)), yes_callback=overwrite, no_callback=no_overwrite, title=strings.title_names_exist).open() else: no_overwrite()
def export_game(filename, game): """ Export the game list `game` to the file `filename`. A game list is a list of dicts, each with the keys 'round_type', 'round_reward', and 'puzzle'. The values of 'round_type' and 'round_reward' are strings, and the value of 'puzzle' is a puzzle dict. See :func:`add_puzzle` for a description of puzzle dicts. :param filename: A filename :type filename: str :param game: A game list :type game: list :return: None """ if '.' not in filename: filename += '.txt' def write(): """ Write the game to the file. """ with open(filename, 'w') as f: for puzzle in game: f.write('{}\t{}\t{}\t{}\t{}\n'.format( puzzle['round_type'], puzzle['round_reward'], puzzle['puzzle']['puzzle'], puzzle['puzzle']['category'], puzzle['puzzle']['clue'])) if os.path.exists(filename): prompts.YesNoPrompt(strings.label_file_exists.format(filename), yes_callback=write, title=strings.title_file_exists).open() else: write()
def export_puzzles_by_name(filename, puzzle_names): """ Export puzzles with names in `puzzle_names` to the file `filename`. If `puzzle_names` is empty, ask the user if they would like to export all files. :param filename: A filename :type filename: str :param puzzle_names: A list of puzzle names :type puzzle_names: list :return: None """ if not puzzle_names: prompts.YesNoPrompt( strings.label_no_export_selected, yes_callback=lambda: export_puzzles(filename, read_puzzles()), title=strings.title_no_export_selected).open() else: puzzles = read_puzzles() export_puzzles( filename, OrderedDict((name, puzzles[name]) for name in puzzle_names))
def add_puzzle(name, puzzle_dict): """ Add a puzzle to the settings file. Creates a confirmation prompt if a puzzle with the given name already exists. A `puzzle_dict` is a dict with the keys 'puzzle', 'category', and 'clue', all of which are strings. This is stored in the settings file as a dict where `name` is a key and `puzzle_dict` is its value. :param name: A name for the puzzle :type name: str :param puzzle_dict: A puzzle dict :type puzzle_dict: dict :return: None """ puzzles = read_puzzles() def write_puzzle(): """ Add `puzzle_dict` to `puzzles` and write `puzzles` to the settings file. :return: None """ puzzles[name] = puzzle_dict update_variables({'puzzles': puzzles}) if name in puzzles.keys(): prompts.YesNoPrompt(strings.label_name_exists.format(name), yes_callback=write_puzzle, title=strings.title_name_exists).open() else: write_puzzle()
def export_puzzles(filename, puzzles): """ Export `puzzles` to the file `filename`. `puzzles` should be a dict of {name: puzzle dict} pairs. See :func:`add_puzzle` for a description of puzzle dicts. :param filename: A filename :type filename: str :param puzzles: A dict containing puzzles. :type puzzles: dict :return: None """ if '.' not in filename: filename += '.txt' def write(): """ Write the puzzles to the file `filename`. :return: None """ with open(filename, 'w') as f: for puzzle in puzzles.values(): f.write('{}\t{}\t{}\n'.format(puzzle['puzzle'], puzzle['category'], puzzle['clue'])) if os.path.exists(filename): prompts.YesNoPrompt(strings.label_file_exists.format(filename), yes_callback=write, title=strings.title_file_exists).open() else: write()