diff options
author | zlg <zlg@zlg.space> | 2019-04-30 20:34:15 -0700 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2019-04-30 20:34:15 -0700 |
commit | cee2897b1e5ace4a697d40450d87d53d0ef3a853 (patch) | |
tree | 2c69d4e0d3f6764b6d243983394fc34a22d79a15 /src | |
parent | README.md: Clarify a few details (diff) | |
download | vgstash-cee2897b1e5ace4a697d40450d87d53d0ef3a853.tar.gz vgstash-cee2897b1e5ace4a697d40450d87d53d0ef3a853.tar.bz2 vgstash-cee2897b1e5ace4a697d40450d87d53d0ef3a853.tar.xz vgstash-cee2897b1e5ace4a697d40450d87d53d0ef3a853.zip |
Release version 0.3 beta 4
This release brings JSON import and export support.
Diffstat (limited to '')
-rw-r--r-- | src/vgstash_cli.py | 88 |
1 files changed, 52 insertions, 36 deletions
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?") |