diff options
author | zlg <zlg@zlg.space> | 2018-10-10 19:44:18 -0700 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2018-10-10 19:44:18 -0700 |
commit | 669aed9ec7261c49c49ed26c31f218e42faa491a (patch) | |
tree | f1f3588bd8430bee3d98d745db720da981e43d06 /src | |
parent | Bump to 0.3alpha6 for PyPI (diff) | |
download | vgstash-669aed9ec7261c49c49ed26c31f218e42faa491a.tar.gz vgstash-669aed9ec7261c49c49ed26c31f218e42faa491a.tar.bz2 vgstash-669aed9ec7261c49c49ed26c31f218e42faa491a.tar.xz vgstash-669aed9ec7261c49c49ed26c31f218e42faa491a.zip |
cli: Add "import" command
Currently the import command will only accept YAML files, but is ready
for expansion to other formats as needed.
Diffstat (limited to '')
-rw-r--r-- | src/vgstash/test_vgstash_cli.py | 34 | ||||
-rw-r--r-- | src/vgstash_cli.py | 40 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/vgstash/test_vgstash_cli.py b/src/vgstash/test_vgstash_cli.py index ecb23c1..941179a 100644 --- a/src/vgstash/test_vgstash_cli.py +++ b/src/vgstash/test_vgstash_cli.py @@ -6,7 +6,7 @@ import vgstash_cli from click.testing import CliRunner -verbose = False +verbose = True interactive = False # Change this to suit your testing environment @@ -211,3 +211,35 @@ def test_notes_edit(): if verbose: print(list_result.output) assert list_result.exit_code == 0 + + +def test_import_file_yaml(): + runner = CliRunner() + result = runner.invoke(vgstash_cli.cli, ["import", "test.yml"]) + if verbose: + print(result.output) + assert result.exit_code == 0 + assert result.output == "Successfully imported 2 games from {}.\n".format(os.path.join(os.getcwd(), "test.yml")) + + # List the results to make sure they match what the editor has. + list_runner = CliRunner() + list_result = runner.invoke(vgstash_cli.cli, ['list', '-w', '40']) + if verbose: + print(list_result.output) + assert list_result.exit_code == 0 + + +def test_import_file_yaml_update(): + runner = CliRunner() + result = runner.invoke(vgstash_cli.cli, ["import", "test.yml", "-u"]) + if verbose: + print(result.output) + assert result.exit_code == 0 + assert result.output == "Successfully imported 3 games from {}.\n".format(os.path.join(os.getcwd(), "test.yml")) + + # List the results to make sure they match what the editor has. + list_runner = CliRunner() + list_result = runner.invoke(vgstash_cli.cli, ['list', '-w', '40']) + if verbose: + print(list_result.output) + assert list_result.exit_code == 0 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)) |