summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2018-09-08 19:15:48 -0700
committerzlg <zlg@zlg.space>2018-09-08 19:15:48 -0700
commitb2d47d2ca978e2cd62be4cb6fe0471598427b8b7 (patch)
treee453e5da5f934058dc65bc0b352642305773bea7
parentAdd remaining filters to vgstash package (diff)
downloadvgstash-b2d47d2ca978e2cd62be4cb6fe0471598427b8b7.tar.gz
vgstash-b2d47d2ca978e2cd62be4cb6fe0471598427b8b7.tar.bz2
vgstash-b2d47d2ca978e2cd62be4cb6fe0471598427b8b7.tar.xz
vgstash-b2d47d2ca978e2cd62be4cb6fe0471598427b8b7.zip
cli: add '--raw' option to list command
Add '--raw' option to the list command, in addition to proper note expansion. Newline characters in notes are escaped to be friendly to scripting. This option may be shortened to '-r' at the user's convenience. In raw output mode, the information is formatted in plain pipe-delimited strings, one line per row: title|system|ownership|progress|notes ownership and progress are printed in their numeric form, consistent with the OWNERSHIP and PROGRESS dictionaries in the vgstash package. An empty notes field will result in a line ending with a pipe and no whitespace following it.
Diffstat (limited to '')
-rw-r--r--src/vgstash/test_vgstash_cli.py14
-rw-r--r--src/vgstash_cli.py40
2 files changed, 45 insertions, 9 deletions
diff --git a/src/vgstash/test_vgstash_cli.py b/src/vgstash/test_vgstash_cli.py
index ac123ef..3ad661d 100644
--- a/src/vgstash/test_vgstash_cli.py
+++ b/src/vgstash/test_vgstash_cli.py
@@ -48,26 +48,36 @@ def test_add_full():
assert result.exit_code == 0
assert result.output == "Added Vectorman for Genesis. You do not own it and have beaten it. It also has notes.\n"
+def test_add_full_note_with_newline():
+ runner = CliRunner()
+ result = runner.invoke(vgstash_cli.cli, ['add', 'Vectorman 2', 'Genesis', 'p', 'p', 'beep\nboop'])
+ if verbose:
+ print(result.output)
+ assert result.exit_code == 0
+ assert result.output == "Added Vectorman 2 for Genesis. You physically own it and are playing it. It also has notes.\n"
+
def test_list():
runner = CliRunner()
- result = runner.invoke(vgstash_cli.list_games)
+ result = runner.invoke(vgstash_cli.list_games, ['--raw'])
if verbose:
print(result.output)
assert result.exit_code == 0
assert result.output == '\n'.join((
'Sonic the Hedgehog 2|Genesis|0|3|',
'Vectorman|Genesis|0|3|beep',
+ 'Vectorman 2|Genesis|1|2|beep\\nboop',
'Super Mario Bros.|NES|1|2|',
'The Legend of Zelda|NES|2|2|\n',
))
def test_list_filter():
runner = CliRunner()
- result = runner.invoke(vgstash_cli.cli, ['list', 'playlog'])
+ result = runner.invoke(vgstash_cli.cli, ['list', '-r', 'playlog'])
if verbose:
print(result.output)
assert result.exit_code == 0
assert result.output == '\n'.join((
+ 'Vectorman 2|Genesis|1|2|beep\\nboop',
'Super Mario Bros.|NES|1|2|',
'The Legend of Zelda|NES|2|2|\n',
))
diff --git a/src/vgstash_cli.py b/src/vgstash_cli.py
index 9f5eef4..3d1d1db 100644
--- a/src/vgstash_cli.py
+++ b/src/vgstash_cli.py
@@ -4,9 +4,10 @@ import click
import sys
def get_db():
- """
- Convenience function to fetch a vgstash DB object from the default location.
- """
+ """Fetch a vgstash DB object from the default location.
+
+ Change DEFAULT_CONFIG['db_location'] before calling this function
+ to alter behavior."""
return vgstash.DB(vgstash.DEFAULT_CONFIG['db_location'])
@@ -24,6 +25,7 @@ def init():
else:
raise sqlite3.OperationalError("Cannot create schema.")
+
@cli.command('add')
@click.argument('title', type=str)
@click.argument('system', type=str)
@@ -49,16 +51,40 @@ def add(title, system, ownership, progress, notes):
"have completed",
)
note_clause = "" if len(game.notes) == 0 else " It also has notes."
- click.echo("Added {} for {}. You {} it and {} it.{}".format(game.title, game.system, own_clause[game.ownership], progress_clause[game.progress], note_clause))
+ click.echo("Added {} for {}. You {} it and {} it.{}".format(
+ game.title,
+ game.system,
+ own_clause[game.ownership],
+ progress_clause[game.progress],
+ note_clause,
+ ))
except sqlite3.IntegrityError as e:
print(e)
click.echo("Couldn't add game.")
@cli.command('list')
-@click.argument('filter', required=False)
-def list_games(filter="allgames"):
+@click.argument('filter', required=False, default="allgames")
+@click.option('--raw', '-r', is_flag=True, show_default=True, default=False, help="Output raw, pipe-delimited lines")
+def list_games(filter, raw):
db = get_db()
res = db.list_games(filter)
for r in res:
- click.echo("{}|{}|{}|{}|{}".format(r['title'], r['system'], r['ownership'], r['progress'], r['notes']))
+ if 'notes' in r.keys() and len(r['notes']) > 0:
+ notes = r['notes'].replace('\n', '\\n')
+ notes = notes.replace('\r', '\\r')
+ else:
+ notes = ''
+
+ if raw:
+ click.echo("|".join((
+ r['title'],
+ r['system'],
+ str(r['ownership']),
+ str(r['progress']),
+ notes
+ )
+ ))
+ else:
+ pass
+ #pretty_print(r)
tion may be shortened to '-r' at the user's convenience. In raw output mode, the information is formatted in plain pipe-delimited strings, one line per row: title|system|ownership|progress|notes ownership and progress are printed in their numeric form, consistent with the OWNERSHIP and PROGRESS dictionaries in the vgstash package. An empty notes field will result in a line ending with a pipe and no whitespace following it. 2018-09-08Add remaining filters to vgstash packagezlg1-2/+11 2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 Whoops. 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 This commit is huge, but contains everything needed for a "proper" build system built on pytest + tox and a CLI built with click. For now, this branch will contain all new vgstash development activity until it reaches feature parity with master. The CLI is installed to pip's PATH. Only the 'init', 'add', and 'list' commands work, with only two filters. This is pre-alpha software, and is therefore not stable yet. 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 It's time for a refactor to a module; the functionality and interface are clashing. 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 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 Loops and functions -- oh my, what a useful combination. :) 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 Also correct a typo.