aboutsummaryrefslogtreecommitdiff
path: root/src/vgstash_cli.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/vgstash_cli.py')
-rw-r--r--src/vgstash_cli.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/vgstash_cli.py b/src/vgstash_cli.py
index 6289dba..5f69c47 100644
--- a/src/vgstash_cli.py
+++ b/src/vgstash_cli.py
@@ -5,6 +5,7 @@ import os
import subprocess
import sys
import tempfile
+import yaml
# Click also has this, but it doesn't support a fallback value.
from shutil import get_terminal_size
@@ -196,3 +197,42 @@ def notes(title, system, edit):
click.echo("Notes for {} on {}:".format(target_game.title, target_game.system))
click.echo()
click.echo(target_game.notes)
+
+
+@cli.command("import")
+@click.option("--format", "-f", type=click.Choice(["yaml"]), 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(
+ readable=True,
+ resolve_path=True,
+ dir_okay=False,
+ file_okay=True),
+ default=sys.stdin,
+ )
+def import_file(format, filepath, update):
+ """
+ Import game data from an external file matching the chosen format.
+
+ The default format is YAML.
+ """
+ if format == "yaml":
+ with open(filepath) as fp:
+ 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
+ click.echo("Successfully imported {} games from {}.".format(count, filepath))