Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update numeric value justification, fix last updated sort order #92

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ and this project adheres to

### Fixed

- Fix crash on `meeple import -v`.
- Fix `meeple collections` update warning message from printing more than once.
- Print a more elegant error message when BGG API rate limit is exceeded.
- `meeple import --verbose` - Fix crash.
- `meeple collections` - Prevent update warning message from printing more than once.
- `meeple collections` - Fix sort order of NA Last Updated values.
- `GENERAL` - Fix justification of numeric values in tables. ([#83](https://github.com/boldandbrad/meeple-cli/issues/83))
- `GENERAL` - Print a more elegant error message when BGG API rate limit is exceeded.

## [v1.1.0] - 2023-10-18

Expand Down
2 changes: 1 addition & 1 deletion src/meeple/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Local board game collection manager. Powered by BoardGameGeek."""

# release version
__version__ = "1.2.0b2"
__version__ = "1.2.0b3"
9 changes: 3 additions & 6 deletions src/meeple/command/collections.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from meeple.util.collection_util import get_collections, update_collection
from meeple.util.fmt_util import fmt_cmd, fmt_date, fmt_headers
from meeple.util.fmt_util import fmt_cmd
from meeple.util.message_util import info_msg, no_collections_exist_error, warn_msg
from meeple.util.output_util import CollectionHeader, print_table
from meeple.util.sort_util import COLLECTION_SORT_KEYS, sort_collections
Expand Down Expand Up @@ -55,9 +55,6 @@
]
)

# format headers
headers = fmt_headers(headers, sort, sort_direction)

rows = []
for collection in collections:
cols = [collection.fmt_name(styled=False, state=True)]
Expand All @@ -67,10 +64,10 @@
[
str(len(collection.get_board_games())),
str(len(collection.get_expansions())),
fmt_date(collection.data.last_updated),
collection.data.fmt_last_updated(),
]
)

rows.append(cols)

print_table(rows, headers)
print_table(rows, headers, sort_key=sort, sort_direction=sort_direction)

Check warning on line 73 in src/meeple/command/collections.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/command/collections.py#L73

Added line #L73 was not covered by tests
6 changes: 1 addition & 5 deletions src/meeple/command/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from meeple.util.completion_util import complete_collections
from meeple.util.filter_util import filterby_players, filterby_playtime, filterby_weight
from meeple.util.fmt_util import (
fmt_headers,
fmt_item_type,
fmt_players,
fmt_playtime,
Expand Down Expand Up @@ -149,9 +148,6 @@
]
)

# format headers
headers = fmt_headers(headers, sort, sort_direction)

# prepare table data
rows = []
for item in result_items:
Expand Down Expand Up @@ -188,4 +184,4 @@
info_msg(
f"Showing {len(result_items)} of {len(total_unique_items)} unique items from collection(s) [u magenta]{'[/u magenta], [u magenta]'.join([collection.name for collection in collections])}[/u magenta]."
)
print_table(rows, headers)
print_table(rows, headers, sort_key=sort, sort_direction=sort_direction)

Check warning on line 187 in src/meeple/command/find.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/command/find.py#L187

Added line #L187 was not covered by tests
2 changes: 0 additions & 2 deletions src/meeple/command/hot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import click

from meeple.util.api_util import get_bgg_hot
from meeple.util.fmt_util import fmt_headers
from meeple.util.output_util import ItemHeader, print_table


Expand All @@ -14,7 +13,6 @@ def hot() -> None:

# prepare table data
headers = [ItemHeader.COUNT, ItemHeader.ID, ItemHeader.NAME]
headers = fmt_headers(headers, None, None)

rows = []
for idx, item in enumerate(api_result):
Expand Down
6 changes: 1 addition & 5 deletions src/meeple/command/list_.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from meeple.util.collection_util import get_collection
from meeple.util.completion_util import complete_collections
from meeple.util.fmt_util import (
fmt_headers,
fmt_item_type,
fmt_players,
fmt_playtime,
Expand Down Expand Up @@ -106,9 +105,6 @@
]
)

# format headers
headers = fmt_headers(headers, sort, sort_direction)

rows = []
for item in result_items:
cols = [str(item.id), item.name]
Expand All @@ -134,4 +130,4 @@
info_msg(
f"Showing {len(result_items)} of {len(collection.data.items)} items from collection {collection.fmt_name()}."
)
print_table(rows, headers)
print_table(rows, headers, sort_key=sort, sort_direction=sort_direction)

Check warning on line 133 in src/meeple/command/list_.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/command/list_.py#L133

Added line #L133 was not covered by tests
3 changes: 1 addition & 2 deletions src/meeple/command/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from meeple.util.api_util import search_bgg
from meeple.util.fmt_util import fmt_headers, fmt_year
from meeple.util.fmt_util import fmt_year
from meeple.util.message_util import error_msg
from meeple.util.output_util import ItemHeader, print_table

Expand All @@ -27,7 +27,6 @@ def search(query: str) -> None:

# prepare table data
headers = [ItemHeader.ID, ItemHeader.NAME, ItemHeader.YEAR]
headers = fmt_headers(headers, None, None)

rows = []
for item in result_items:
Expand Down
9 changes: 7 additions & 2 deletions src/meeple/command/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
invalid_collection_error,
warn_msg,
)
from meeple.util.output_util import print_table
from meeple.util.output_util import ItemHeader, print_table


@click.command()
Expand Down Expand Up @@ -103,7 +103,12 @@
info_msg(
f"Showing average stats for {len(result_items)} of {len(collection.data.items)} items from collection {collection.fmt_name()}."
)
headers = ["Rank", "Rating", "Weight", "Max Players"]
headers = [

Check warning on line 106 in src/meeple/command/stats.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/command/stats.py#L106

Added line #L106 was not covered by tests
ItemHeader.RANK,
ItemHeader.RATING,
ItemHeader.WEIGHT,
ItemHeader.MAX_PLAYERS,
]
rows = [
[
f"{fmt_avg_rank(avg_rank)}",
Expand Down
6 changes: 6 additions & 0 deletions src/meeple/type/collection_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

from meeple.type.item import Item
from meeple.util.fmt_util import NA_VALUE

DATA_VERSION_KEY = "version"
DATA_DATE_KEY = "date"
Expand All @@ -17,6 +18,11 @@
def __bool__(self) -> bool:
return self.last_updated or len(self.items) > 0

def fmt_last_updated(self):
if self.last_updated:
return self.last_updated
return NA_VALUE

Check warning on line 24 in src/meeple/type/collection_data.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/type/collection_data.py#L22-L24

Added lines #L22 - L24 were not covered by tests

def to_dict(self) -> dict:
return {
DATA_VERSION_KEY: "1.0", # TODO: make this dynamic
Expand Down
17 changes: 0 additions & 17 deletions src/meeple/util/fmt_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,6 @@ def fmt_cmd(command: str) -> str:
return f"[green]{command}[/green]"


def fmt_date(date: str) -> str:
if date:
return date
return NA_VALUE


def fmt_headers(headers, sort_key: str, sort_direction: str):
header_strs = []
for header in headers:
if sort_key and header.value[1] == sort_key:
header_strs.append(f"{header.value[0]} {sort_direction}")
continue
header_strs.append(header.value[0])

return header_strs


def fmt_players(minplayers: str, maxplayers: str) -> str:
if int(minplayers) == int(maxplayers) == 0:
return NA_VALUE
Expand Down
43 changes: 26 additions & 17 deletions src/meeple/util/output_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,37 @@
# tables


def _fmt_header(header, sort_direction: str):
return f"{header.value[0]} {sort_direction}"

Check warning on line 13 in src/meeple/util/output_util.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/util/output_util.py#L13

Added line #L13 was not covered by tests


class ItemHeader(Enum):
COUNT = ("#", "count")
ID = ("ID", "id")
NAME = ("Name", "name")
TYPE = ("Type", "type")
COLLECTION = ("Collection(s)", "collection")
YEAR = ("Year", "year")
RANK = ("Rank", "rank")
RATING = ("Rating", "rating")
WEIGHT = ("Weight", "weight")
PLAYERS = ("Players", "players")
TIME = ("Play Time", "time")
COUNT = ("#", "count", "right")
ID = ("ID", "id", "right")
NAME = ("Name", "name", "left")
TYPE = ("Type", "type", "left")
COLLECTION = ("Collection(s)", "collection", "left")
YEAR = ("Year", "year", "right")
RANK = ("Rank", "rank", "right")
RATING = ("Rating", "rating", "right")
WEIGHT = ("Weight", "weight", "right")
PLAYERS = ("Players", "players", "right")
MAX_PLAYERS = ("Max Players", "maxplayers", "right")
TIME = ("Play Time", "time", "right")


class CollectionHeader(Enum):
NAME = ("Name", "name")
BOARDGAMES = ("Board Games", "boardgames")
EXPANSIONS = ("Expansions", "expansions")
UPDATED = ("Last Updated", "updated")
NAME = ("Name", "name", "left")
BOARDGAMES = ("Board Games", "boardgames", "right")
EXPANSIONS = ("Expansions", "expansions", "right")
UPDATED = ("Last Updated", "updated", "left")


def print_table(
rows: list,
headers: list = [],
sort_key: str = None,
sort_direction: str = None,
dim_border: bool = False,
zebra_rows: bool = False,
row_lines: bool = False,
Expand All @@ -50,9 +57,11 @@
border_style=border_styles,
show_lines=row_lines,
)

for header in headers:
table.add_column(header)
header_title = header.value[0]
if sort_key and sort_direction and header.value[1] == sort_key:
header_title = f"{header_title} {sort_direction}"
table.add_column(header_title, justify=header.value[2])

Check warning on line 64 in src/meeple/util/output_util.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/util/output_util.py#L61-L64

Added lines #L61 - L64 were not covered by tests
for row in rows:
table.add_row(*row)

Expand Down
14 changes: 11 additions & 3 deletions src/meeple/util/sort_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re
from datetime import datetime
from sys import maxsize as MAXINT

from meeple.type.collection import Collection
Expand All @@ -8,6 +10,12 @@
ITEM_SORT_KEYS = ["rank", "rating", "weight", "year", "name", "id", "time"]


def _sort_date(date_str: str):
if not date_str:
return datetime.min
return datetime.strptime(re.sub("[-]", "/", date_str), "%Y/%m/%d")

Check warning on line 16 in src/meeple/util/sort_util.py

View check run for this annotation

Codecov / codecov/patch

src/meeple/util/sort_util.py#L16

Added line #L16 was not covered by tests


def sort_collections(collection_list: [Collection], sort_key: str) -> [Collection]:
match sort_key:
case "name":
Expand Down Expand Up @@ -36,14 +44,14 @@
return (
sorted(
collection_list,
key=lambda collection: str(collection.data.last_updated),
key=lambda collection: _sort_date(collection.data.last_updated),
reverse=True,
),
SORT_DESC_SYMBOL,
)


def _handle_item_rank(item: Item) -> int:
def _sort_rank(item: Item) -> int:
if item.rank == 0:
return MAXINT
return item.rank
Expand All @@ -61,7 +69,7 @@
"""
match sort_key:
case "rank":
return sorted(item_list, key=_handle_item_rank), SORT_ASC_SYMBOL
return sorted(item_list, key=_sort_rank), SORT_ASC_SYMBOL
case "weight":
return (
sorted(item_list, key=lambda item: item.weight, reverse=True),
Expand Down