Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Add JSON output option to getpapers command #46

Merged
merged 2 commits into from
Dec 19, 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
25 changes: 24 additions & 1 deletion npbc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"""


from sqlite3 import DatabaseError, connect, Connection
from argparse import ArgumentParser
from argparse import Namespace as ArgNamespace
from collections.abc import Generator
from datetime import datetime
from json import dumps
from sqlite3 import Connection, DatabaseError, connect
from sys import argv

from colorama import Fore, Style
Expand Down Expand Up @@ -134,6 +135,8 @@ def define_and_read_args(arguments: list[str]) -> ArgNamespace:
getpapers_parser.add_argument('-n', '--names', help="Get the names of the newspapers.", action='store_true')
getpapers_parser.add_argument('-d', '--delivered', help="Get the days the newspapers are delivered. All seven weekdays are required. A 'Y' means it is delivered, and an 'N' means it isn't.", action='store_true')
getpapers_parser.add_argument('-c', '--cost', help="Get the daywise prices of the newspapers. Values must be separated by semicolons.", action='store_true')
getpapers_parser.add_argument('-j', '--json', help="Get the papers as JSON.", action='store_true')


# get undelivered logs subparser
getlogs_parser = functions.add_parser(
Expand Down Expand Up @@ -623,6 +626,26 @@ def getpapers(parsed_arguments: ArgNamespace, connection: Connection) -> None:
days[paper_data.paper_id][paper_data.day_id]['delivery'] = paper_data.delivered
days[paper_data.paper_id][paper_data.day_id]['cost'] = paper_data.cost

# if the user wants the data as json, print it and return. include all the data
if parsed_arguments.json:
json_data = {
paper_id: {
'name': name,
'days': [
{
'delivery': days[paper_id][day_id]['delivery'],
'cost': days[paper_id][day_id]['cost']
}
for day_id in range(len(npbc_core.WEEKDAY_NAMES))
]
}
for paper_id, name in zip(ids, names)
}

print(dumps(json_data))

return

# if the user wants the delivery data, add it to the headers and the data to the list
if parsed_arguments.delivered:
headers.append('days')
Expand Down
6 changes: 3 additions & 3 deletions npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ def delete_existing_paper(connection: Connection, paper_id: int) -> None:
# check if the paper exists
if not connection.execute(
"SELECT EXISTS (SELECT 1 FROM papers WHERE paper_id = ?);",
(paper_id,)).fetchone()[0]:
raise npbc_exceptions.PaperNotExists(f"Paper with ID {paper_id} does not exist."
)
(paper_id,)
).fetchone()[0]:
raise npbc_exceptions.PaperNotExists(f"Paper with ID {paper_id} does not exist.")

# delete the paper
connection.execute(
Expand Down
Loading