Skip to content

Commit

Permalink
Merge branch 'command_reorg'
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Jul 1, 2016
2 parents 05da7c8 + 41a5689 commit d31fd10
Show file tree
Hide file tree
Showing 20 changed files with 720 additions and 465 deletions.
33 changes: 24 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@ card data:

.. code:: bash
mtg-ssm collection.xlsx
mtg-ssm create collection.xlsx
In the future, when new sets are released, running the same command will
update your collection spreadsheet while keeping existing counts:
In the future, when new sets are released, you will want to update your
collection spreadsheet while keeping existing counts:

.. code:: bash
mtg-ssm collection.xlsx
mtg-ssm update collection.xlsx
Existing collections
--------------------

If you already have your cards in another collection store, you might
want to import that collection into your card spreadsheet.

First create an example csv file:
First create an input csv file:

.. code:: bash
mtg-ssm --format csv collection.csv.example
mtg-ssm create input_data.csv
Then modify the template to match your counts and import into your
spreadsheet:

.. code:: bash
mtg-ssm collection.xlsx collection.csv
mtg-ssm merge collection.xlsx input_data.csv
Export / import to deckbox
--------------------------
Expand All @@ -97,15 +97,15 @@ merge from an existing collection using the "deckbox" import format:

.. code:: bash
mtg-ssm --import_format collection.xlsx Inventory_username_2016.March.10.csv
mtg-ssm --dialect csv deckbox merge collection.xlsx Inventory_username_2016.March.10.csv
Alternatively, if you have your collection in a spreadsheet already and would
like to load your data into deckbox to check prices or share with other people,
just go the other direction.

.. code:: bash
mtg-ssm --format inventory.csv collection.xlsx
mtg-ssm --dialect csv deckbox merge inventory.csv collection.xlsx
Deckbox Warning
~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -149,6 +149,21 @@ Acknowledgments
Changelog
=========

1.3.0
-----

- Complete rework of cli (see `--help` for details)

- cli is **NOT** the same; old commands will **NOT** work
- new global argument flags and dialect selection mechanisms
- create: create a new collection
- update: update an existing collection
- merge: merge multiple collections
- diff: get a diff of two collections

- Lots of under the hood changes and performance improvements
- Files are still compatible

1.2.4
-----

Expand Down
6 changes: 5 additions & 1 deletion check.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

CI="$CI"

set -e
set -o nounset

Expand All @@ -11,7 +13,9 @@ EXIT=0
if [ "$SUITE" = "test" ]; then
echo "Running test suite"
py.test --cov=mtg_ssm --strict -r w tests || EXIT=$?
coveralls || EXIT=$?
if [ "$CI" = "true" ]; then
coveralls || EXIT=$?
fi

elif [ "$SUITE" = "lint" ]; then
echo "Running lint suite"
Expand Down
118 changes: 0 additions & 118 deletions mtg_ssm/manager.py

This file was deleted.

22 changes: 20 additions & 2 deletions mtg_ssm/mtg/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class CountTypes(enum.Enum):
foils = 'foils'


def new_print_counts():
"""Get an appropriate defaultdict set up for use ase print counts."""
return collections.defaultdict(
lambda: collections.defaultdict(int))


def find_printing(cdb, set_code, name, set_number, multiverseid, strict=True):
"""Attempt to find a CardPrinting from given parameters."""
name = name or ''
Expand Down Expand Up @@ -109,10 +115,22 @@ def aggregate_print_counts(cdb, card_rows, strict=True):

def merge_print_counts(*print_counts_args):
"""Merge two sets of print_counts."""
print_counts = collections.defaultdict(
lambda: collections.defaultdict(int))
print_counts = new_print_counts()
for in_print_counts in print_counts_args:
for printing, counts in in_print_counts.items():
for key, value in counts.items():
print_counts[printing][key] += value
return print_counts


def diff_print_counts(left, right):
"""Subtract right print counts from left print counts."""
print_counts = new_print_counts()
for printing in left.keys() | right.keys():
left_counts = left.get(printing, {})
right_counts = right.get(printing, {})
for key in left_counts.keys() | right_counts.keys():
value = left_counts.get(key, 0) - right_counts.get(key, 0)
if value:
print_counts[printing][key] = value
return print_counts
40 changes: 22 additions & 18 deletions mtg_ssm/mtgjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,31 @@ def fetch_mtgjson(data_path):
local_version = (0, 0, 0)

try:
print('Checking remove vs local version of mtgjson data.')
ver_req = requests.get(MTGJSON_ADDRESS + VERSION_FILENAME)
ver_req.raise_for_status()

remote_version_data = ver_req.json()
remote_version = tuple(
int(v) for v in remote_version_data['version'].split('.'))

if local_version >= remote_version:
print('Mtgjson data is already up to date.')
return False

print('Downloading mtgjson data.')
allsets_filename = os.path.join(data_path, ALLSETS_FILENAME)
mtg_req = requests.get(MTGJSON_ADDRESS + ALLSETS_FILENAME)
mtg_req.raise_for_status()
with open(allsets_filename, 'wb') as allsets_file:
allsets_file.write(mtg_req.content)
with open(version_filename, 'wb') as version_file:
version_file.write(ver_req.content)
return True
except requests.ConnectionError as err:
raise DownloadError('Could not connect to mtgjson') from err
if ver_req.status_code != 200:
raise DownloadError(
'Could not fetch version: {}'.format(ver_req.reason))
remote_version_data = ver_req.json()
remote_version = tuple(
int(v) for v in remote_version_data['version'].split('.'))

if local_version >= remote_version:
return False

print('Downloading mtgjson data.')
allsets_filename = os.path.join(data_path, ALLSETS_FILENAME)
mtg_req = requests.get(MTGJSON_ADDRESS + ALLSETS_FILENAME)
with open(allsets_filename, 'wb') as allsets_file:
allsets_file.write(mtg_req.content)
with open(version_filename, 'wb') as version_file:
version_file.write(ver_req.content)
return True
except requests.exceptions.HTTPError as err:
raise DownloadError('Could not retrieve mtgjson data') from err


def read_mtgjson(data_path):
Expand Down
29 changes: 20 additions & 9 deletions mtg_ssm/serialization/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
] + [ct.name for ct in counts.CountTypes]


def row_for_printing(printing, print_counts):
def row_for_printing(printing, printing_counts):
"""Given a CardPrinting and counts, return a csv row."""
csv_row = {
'set': printing.set_code,
Expand All @@ -23,35 +23,46 @@ def row_for_printing(printing, print_counts):
'multiverseid': printing.multiverseid,
'id': printing.id_,
}
for counttype, count in print_counts.get(printing, {}).items():
for counttype, count in printing_counts.items():
if count:
csv_row[counttype.name] = count
return csv_row


def rows_for_printings(cdb, print_counts):
def rows_for_printings(cdb, print_counts, verbose):
"""Generator that yields csv rows from a card_db."""
for card_set in cdb.card_sets:
for printing in card_set.printings:
yield row_for_printing(printing, print_counts)
printing_counts = print_counts.get(printing, {})
if verbose or any(printing_counts):
yield row_for_printing(printing, printing_counts)


class MtgCsvSerializer(interface.MtgSsmSerializer):
"""MtgSsmSerializer for reading and writing csv files."""

format = 'csv'
class CsvFullDialect(interface.SerializationDialect):
"""csv collection writing a row for every printing"""
extension = 'csv'
dialect = 'csv'

verbose = True

def write(self, filename: str, print_counts) -> None:
"""Write print counts to a file."""
with open(filename, 'w') as csv_file:
writer = csv.DictWriter(csv_file, CSV_HEADER)
writer.writeheader()
for row in rows_for_printings(self.cdb, print_counts):
for row in rows_for_printings(
self.cdb, print_counts, self.verbose):
writer.writerow(row)

def read(self, filename: str):
"""Read print counts from file."""
with open(filename, 'r') as csv_file:
return counts.aggregate_print_counts(
self.cdb, csv.DictReader(csv_file))


class CsvTerseDialect(CsvFullDialect):
"""csv collection writing only rows that have counts"""
dialect = 'terse'

verbose = False
Loading

0 comments on commit d31fd10

Please sign in to comment.