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

Commit

Permalink
Merge pull request #46 from eccentricOrange:random-updates
Browse files Browse the repository at this point in the history
Add JSON output option to getpapers command
  • Loading branch information
eccentricOrange authored Dec 19, 2023
2 parents 32fe8dc + c468bac commit 0a43f37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
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

0 comments on commit 0a43f37

Please sign in to comment.