Skip to content

Commit 6f9198e

Browse files
ci: add test for 3.12 and remove 3.7 support
Signed-off-by: Saurav Sharma <[email protected]>
1 parent d1e10b8 commit 6f9198e

File tree

8 files changed

+303
-383
lines changed

8 files changed

+303
-383
lines changed

.github/workflows/deploy_pypi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Install Poetry
1818
uses: snok/install-poetry@v1
1919
with:
20-
version: 1.4.0
20+
version: 1.7.1
2121
virtualenvs-create: true
2222
virtualenvs-in-project: true
2323
- name: Publish

.github/workflows/python_app.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v3
16+
- uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.12"
1619
- uses: actions/setup-python@v4
1720
with:
1821
python-version: "3.11"
@@ -25,13 +28,10 @@ jobs:
2528
- uses: actions/setup-python@v4
2629
with:
2730
python-version: "3.8"
28-
- uses: actions/setup-python@v4
29-
with:
30-
python-version: "3.7"
3131
- name: Install Poetry
3232
uses: snok/install-poetry@v1
3333
with:
34-
version: 1.4.0
34+
version: 1.7.1
3535
virtualenvs-create: true
3636
virtualenvs-in-project: true
3737
- name: Install dependencies

advent_of_code_py/cache_file.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,44 @@
33
import os
44
import time
55
from pathlib import Path
6-
from typing import Optional
76

87
from platformdirs import user_cache_dir
98

9+
from enum import Enum
10+
11+
12+
class FileType(Enum):
13+
INPUT_FILE = "input.txt"
14+
SUBMISSION_FILE = "submission.txt"
15+
TIME_FILE = "time.txt"
16+
1017

1118
def input_data_is_downloaded(year: int, day: int, session: str) -> bool:
1219
"""Check if an input is downloaded and cached"""
13-
cache_file = _join_path(year, day, session, file_type="input_file")
14-
cache_file = Path(cache_file)
20+
cache_file = cache_file_path(year, day, session, file_type="input_file")
1521
return cache_file.exists()
1622

1723

1824
def save_input_to_cache(year: int, day: int, session: str, input_data: str):
1925
"""Save a input to its cache location for future reference and use"""
20-
cache_folder = _join_path(year, day, session)
21-
Path(cache_folder).mkdir(parents=True, exist_ok=True)
22-
cache_file = os.path.join(cache_folder, "input.txt")
23-
with open(cache_file, "w+") as opened_file:
26+
cache_file = cache_file_path(year, day, session, FileType.INPUT_FILE)
27+
cache_file.parent.mkdir(parents=True, exist_ok=True)
28+
with cache_file.open("w") as opened_file:
2429
opened_file.write(input_data)
2530

2631

2732
def delete_input(year: int, day: int, session: str):
2833
"""Delete input from a cache folder"""
29-
cache_file = _join_path(year, day, session, file_type="input_file")
30-
if Path(cache_file).exists():
31-
os.remove(cache_file)
34+
cache_file = cache_file_path(year, day, session, FileType.INPUT_FILE)
35+
cache_file.unlink(missing_ok=True)
3236

3337

3438
def get_cache_file_data(year: int, day: int, session: str) -> str:
3539
"""Return cache file input data from cache folder for certain problem"""
3640
server_action = importlib.import_module(".server_action")
3741
server_action.download_input(year, day, session)
38-
cache_file = _join_path(year, day, session, file_type="input_file")
39-
with open(cache_file) as opened_file:
42+
cache_file = cache_file_path(year, day, session, FileType.INPUT_FILE)
43+
with cache_file.open() as opened_file:
4044
input_data = opened_file.read()
4145
return input_data
4246

@@ -45,8 +49,8 @@ def save_submitted_answer(
4549
year: int, day: int, part: int, session: str, answer: str, message: str
4650
):
4751
"""Save submitted input to file of problem"""
48-
submitted_file = _join_path(year, day, session, file_type="submission_file")
49-
with open(submitted_file, "a") as opened_file:
52+
submitted_file = cache_file_path(year, day, session, FileType.SUBMISSION_FILE)
53+
with submitted_file.open("a") as opened_file:
5054
opened_file.write("{}!{}:{}\n".format(part, answer, message))
5155

5256

@@ -57,9 +61,9 @@ def last_submitted_answer_message(
5761
Check if answer is already submitted by user if submitted return message of last
5862
submission
5963
"""
60-
submission_file = _join_path(year, day, session, file_type="submission_file")
64+
submission_file = cache_file_path(year, day, session, FileType.SUBMISSION_FILE)
6165
last_answer_message = ""
62-
with open(submission_file, "r") as opened_file:
66+
with submission_file.open() as opened_file:
6367
lines = opened_file.read()
6468
for line in lines:
6569
separate_part = line.split("!", 1)
@@ -72,33 +76,27 @@ def last_submitted_answer_message(
7276

7377
def save_last_submission_time(year: int, day: int, session: str):
7478
"""Save a time where a request is performed for last submission"""
75-
last_time_file = _join_path(year, day, session, file_type="last_time_file")
76-
with open(last_time_file, "w") as opened_file:
79+
last_time_file = cache_file_path(year, day, session, FileType.TIME_FILE)
80+
with last_time_file.open("w") as opened_file:
7781
opened_file.write(str(time.time()))
7882

7983

8084
def check_less_than_one_min_submission(year: int, day: int, session: str) -> bool:
8185
"""
8286
Check last submission time for solution return true if time is less than 60 second
8387
"""
84-
last_time_file = _join_path(year, day, session, file_type="last_time_file")
85-
with open(last_time_file, "r") as opened_file:
88+
last_time_file = cache_file_path(year, day, session, FileType.TIME_FILE)
89+
with last_time_file.open() as opened_file:
8690
last_time = float(opened_file.read())
8791
current_time = time.time()
8892
early_submission = current_time - last_time < 60.0
8993
return early_submission
9094

9195

92-
def _join_path(
93-
year: int, day: int, session: str, file_type: Optional[str] = None
94-
) -> str:
96+
def cache_file_path(year: int, day: int, session: str, file_type: FileType) -> Path:
9597
"""Return desire path for a cache folders or files"""
9698
cache_location = user_cache_dir(appname="advent-of-code")
97-
cache_file = os.path.join(cache_location, str(session), str(year), str(day))
98-
if file_type == "input_file":
99-
cache_file = os.path.join(cache_file, "input.txt")
100-
if file_type == "submission_file":
101-
cache_file = os.path.join(cache_file, "submission.txt")
102-
if file_type == "last_time_file":
103-
cache_file = os.path.join(cache_file, "time.txt")
104-
return cache_file
99+
cache_file = os.path.join(
100+
cache_location, str(session), str(year), str(day), file_type.value
101+
)
102+
return Path(cache_file)

advent_of_code_py/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import click
55

66
from .cache_file import delete_input
7-
from .config_file import add_to_json, delete_from_json, get_all_session, list_from_json
7+
from .config_file import (
8+
add_to_json,
9+
delete_from_json,
10+
get_all_session,
11+
print_all_session,
12+
)
813
from .server_action import download_input
914
from .utils import get_current_year, get_day
1015

@@ -29,7 +34,7 @@ def add(name: str, session_value: str):
2934

3035
@config.command("list", help="list all session present in config")
3136
def list_config_session():
32-
list_from_json()
37+
print_all_session()
3338

3439

3540
@config.command("remove", help="remove session from config")

advent_of_code_py/config_file.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_json(self) -> str:
1919
return json.dumps(self.__dict__, indent=4, sort_keys=True)
2020

2121
@classmethod
22-
def from_json(cls, json_str):
22+
def from_json(cls, json_str: str):
2323
"""Create Data type from JSON"""
2424
json_dict = json.loads(json_str)
2525
json_data = json_dict.get("session_list")
@@ -37,48 +37,43 @@ def delete_session(self, session: str):
3737
raise Exception("Value with name {} not found over config".format(session))
3838

3939

40-
def _read_json_file(config_file: str) -> Data:
40+
def read_json_file(config_file: Path) -> Data:
4141
"""Read JSON file to return Data type"""
42-
if not config_file.exists():
43-
with open(config_file, "a+") as opened_file:
44-
val = Data()
45-
opened_file.write(val.to_json())
46-
with open(config_file) as opened_file:
47-
val = opened_file.read()
48-
data = Data.from_json(val)
42+
with config_file.open("a+") as opened_file:
43+
opened_file.seek(0)
44+
data = Data.from_json(opened_file.read())
4945
return data
5046

5147

52-
def _config_file_data() -> str:
48+
def config_file_path() -> Path:
5349
"""Get Config file location"""
5450
config_location = user_config_dir()
5551
config_file = os.path.join(config_location, "aoc-config.json")
56-
config_file = Path(config_file)
57-
return config_file
52+
return Path(config_file)
5853

5954

6055
def add_to_json(**kwargs: str):
6156
"""Add new session to json config file"""
62-
config_file = _config_file_data()
63-
data = _read_json_file(config_file)
57+
config_file = config_file_path()
58+
data = read_json_file(config_file)
6459
data.add_session(**kwargs)
65-
with open(config_file, "w") as opened_file:
60+
with config_file.open("w") as opened_file:
6661
opened_file.write(data.to_json())
6762

6863

6964
def delete_from_json(session: str):
7065
"""Delete session from JSON config file"""
71-
config_file = _config_file_data()
72-
data = _read_json_file(config_file)
66+
config_file = config_file_path()
67+
data = read_json_file(config_file)
7368
data.delete_session(session)
7469
with open(config_file, "w") as opened_file:
7570
opened_file.write(data.to_json())
7671

7772

78-
def list_from_json():
73+
def print_all_session():
7974
"""List all session from a JSON file along with value"""
80-
config_file = _config_file_data()
81-
data = _read_json_file(config_file)
75+
config_file = config_file_path()
76+
data = read_json_file(config_file)
8277
json_dict = json.loads(data.to_json())
8378
json_data = json_dict.get("session_list")
8479
for key, val in json_data.items():
@@ -87,8 +82,8 @@ def list_from_json():
8782

8883
def get_session_value(session_name: str) -> str:
8984
"""Return session name value from JSON config file"""
90-
config_file = _config_file_data()
91-
data = _read_json_file(config_file)
85+
config_file = config_file_path()
86+
data = read_json_file(config_file)
9287
json_dict = json.loads(data.to_json())
9388
json_data = json_dict.get("session_list")
9489
session_value = json_data.get(session_name)
@@ -99,8 +94,8 @@ def get_session_value(session_name: str) -> str:
9994

10095
def get_all_session() -> List[str]:
10196
"""Return all session name"""
102-
config_file = _config_file_data()
103-
data = _read_json_file(config_file)
97+
config_file = config_file_path()
98+
data = read_json_file(config_file)
10499
json_dict = json.loads(data.to_json())
105100
json_data = json_dict.get("session_list")
106101
session_list = list(json_data.keys())

noxfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import nox_poetry
1+
from nox_poetry import session
22
from nox_poetry.sessions import Session
33

4-
python_versions = ["3.7", "3.8", "3.9", "3.10", "3.11"]
4+
python_versions = ["3.8", "3.9", "3.10", "3.11", "3.12"]
55

66

7-
@nox_poetry.session(python=python_versions)
7+
@session(python=python_versions)
88
def lint(session: Session):
99
session.install("black", "isort", "safety", "pytest", ".")
1010
session.run("black", ".", "--check")

0 commit comments

Comments
 (0)