From cee2897b1e5ace4a697d40450d87d53d0ef3a853 Mon Sep 17 00:00:00 2001 From: zlg Date: Tue, 30 Apr 2019 20:34:15 -0700 Subject: Release version 0.3 beta 4 This release brings JSON import and export support. --- src/vgstash_cli.py | 88 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/vgstash_cli.py b/src/vgstash_cli.py index 7996086..b92201f 100644 --- a/src/vgstash_cli.py +++ b/src/vgstash_cli.py @@ -6,6 +6,7 @@ import subprocess import sys import tempfile import yaml +import json # Click also has this, but it doesn't support a fallback value. from shutil import get_terminal_size @@ -207,7 +208,7 @@ def notes(title, system, edit): @cli.command("import") -@click.option("--format", "-f", type=click.Choice(["yaml"]), required=False, default="yaml") +@click.option("--format", "-f", type=click.Choice(["yaml", "json"]), required=False, default="yaml") @click.option("--update", "-u", is_flag=True, default=False, help="Overwrite existing games with the file's data") @click.argument("filepath", type=click.Path( @@ -223,34 +224,42 @@ def import_file(format, filepath, update): Import game data from an external file matching the chosen format. The default format is YAML. + + Available formats: + + * JSON + * YAML """ - if format == "yaml": - with open(filepath) as fp: + with open(filepath) as fp: + if format == "yaml": data = yaml.safe_load(fp) - db = get_db() - count = len(data) - for game in data: - try: - db.add_game( - vgstash.Game( - game["title"], - game["system"], - game["ownership"], - game["progress"], - game["notes"] - ), - update=update - ) - except sqlite3.IntegrityError as e: - count -= 1 - if count > 0: - click.echo("Successfully imported {} games from {}.".format(count, filepath)) - else: - click.echo("Couldn't import any games. Is the YAML file formatted correctly?") + if format == "json": + data = json.load(fp) + db = get_db() + count = len(data) + for game in data: + try: + db.add_game( + vgstash.Game( + game["title"], + game["system"], + game["ownership"], + game["progress"], + game["notes"] + ), + update=update + ) + except sqlite3.IntegrityError as e: + # skip games that already exist + count -= 1 + if count > 0: + click.echo("Successfully imported {} games from {}.".format(count, filepath)) + else: + click.echo("Couldn't import any games. Is the file formatted correctly?") @cli.command("export") -@click.option("--format", "-f", type=click.Choice(["yaml"]), required=False, default="yaml") +@click.option("--format", "-f", type=click.Choice(["yaml", "json"]), required=False, default="yaml") @click.argument("filepath", type=click.Path( exists=False, @@ -267,22 +276,29 @@ def export_file(format, filepath): Export the game database to a file written in the chosen format. The default format is YAML. + + Available formats: + + * JSON + * YAML """ db = get_db() data = db.list_games() game_set = [] - # Time to re-read master's code - if format == "yaml": - for game in data: - g = {} - for field in game.keys(): - g.update({field: game[field]}) - game_set.append(g) - with open(filepath, "w") as fp: + # Time to re-read the master branch's code + for game in data: + g = {} + for field in game.keys(): + g.update({field: game[field]}) + game_set.append(g) + with open(filepath, "w") as fp: + if format == "yaml": yaml.dump(game_set, fp, default_flow_style=False, indent=4, allow_unicode=True) - if len(game_set) > 0: - click.echo("Successfully exported {} games to {}.".format(len(game_set), filepath)) - else: - click.echo("Could not export any games; have you made sure your collection has games in it?") + if format == "json": + json.dump(game_set, fp, allow_nan=False, indent=1, skipkeys=True, sort_keys=True) + if len(game_set) > 0: + click.echo("Successfully exported {} games to {}.".format(len(game_set), filepath)) + else: + click.echo("Could not export any games; have you made sure your collection has games in it?") -- cgit v1.2.3-54-g00ecf 00'>2018-03-13Update helpers a bitzlg1-2/+9 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4 2016-11-13DerpZe Libertine Gamer1-0/+1 2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23 2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4 2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1 2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101 2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9 2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5