Skip to content

Commit f88d25b

Browse files
committed
CLI: Add cli interface.
1 parent 61dd583 commit f88d25b

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

cli.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import logging
2+
import os
3+
4+
import click
5+
6+
from ioccc_winners_db import IOCCCWinnersDB, build_sqllite_db
7+
8+
DEFAULT_IOCCC_WINNERS_DIRECTORY = os.path.join(os.path.dirname(__file__), "winner")
9+
DEFAULT_IOCCC_SQLITE_DB_PATH = os.path.join(os.path.dirname(__file__), "ioccc_winners.sqlite")
10+
11+
12+
@click.command()
13+
@click.option(
14+
'--ioccc_winners_directory',
15+
default=DEFAULT_IOCCC_WINNERS_DIRECTORY,
16+
help='IOCCC winners directory, should be a clone of the following github: https://github.com/ioccc-src/winner.git.'
17+
)
18+
@click.option(
19+
'--output_file',
20+
default=DEFAULT_IOCCC_SQLITE_DB_PATH,
21+
help='Path to the output db.'
22+
)
23+
@click.option(
24+
'--force',
25+
is_flag=True,
26+
default=False,
27+
help='Overrides output_file if its already exists.'
28+
)
29+
@click.option(
30+
'--verbose',
31+
is_flag=True,
32+
default=False,
33+
help='Verbose mode.'
34+
)
35+
def build_db(ioccc_winners_directory, output_file, force, verbose):
36+
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
37+
if os.path.exists(output_file):
38+
if force:
39+
logging.debug(f"File {output_file} already exists! removing it...")
40+
os.remove(output_file)
41+
else:
42+
logging.error(f"File {output_file} already exists! Use --force to override it.")
43+
exit(1)
44+
45+
logging.info(f"Building DB: {output_file}")
46+
winners_db = IOCCCWinnersDB(ioccc_winners_directory)
47+
build_sqllite_db(winners_db.get_all_entries(), output_file)
48+
logging.info("Done!")
49+
50+
51+
if __name__ == '__main__':
52+
build_db()

ioccc_winners_db.py

-9
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class _EntryDetails:
138138

139139

140140
def build_sqllite_db(entries: Iterator[IOCCCWinnerEntry], output_file: str):
141-
# Connect to the SQLite database (or create it if it doesn't exist)
142141
with sqlite3.connect(output_file) as conn:
143142
# Create the "winners" table
144143
conn.execute("""
@@ -164,11 +163,3 @@ def build_sqllite_db(entries: Iterator[IOCCCWinnerEntry], output_file: str):
164163
""",
165164
(entry.name, entry.year, entry.spoiler, entry.prog(), hint)
166165
)
167-
168-
169-
if __name__ == "__main__":
170-
logging.basicConfig(level=logging.DEBUG)
171-
_logger.info("Building DB...")
172-
winners_db = IOCCCWinnersDB()
173-
build_sqllite_db(winners_db.get_all_entries(), "ioccc_winners.sqlite")
174-
_logger.info("Done!")

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
click==8.1.4

0 commit comments

Comments
 (0)