summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/vgstash_cli.py88
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?")