3
3
import os
4
4
import time
5
5
from pathlib import Path
6
- from typing import Optional
7
6
8
7
from platformdirs import user_cache_dir
9
8
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
+
10
17
11
18
def input_data_is_downloaded (year : int , day : int , session : str ) -> bool :
12
19
"""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" )
15
21
return cache_file .exists ()
16
22
17
23
18
24
def save_input_to_cache (year : int , day : int , session : str , input_data : str ):
19
25
"""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 :
24
29
opened_file .write (input_data )
25
30
26
31
27
32
def delete_input (year : int , day : int , session : str ):
28
33
"""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 )
32
36
33
37
34
38
def get_cache_file_data (year : int , day : int , session : str ) -> str :
35
39
"""Return cache file input data from cache folder for certain problem"""
36
40
server_action = importlib .import_module (".server_action" )
37
41
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 :
40
44
input_data = opened_file .read ()
41
45
return input_data
42
46
@@ -45,8 +49,8 @@ def save_submitted_answer(
45
49
year : int , day : int , part : int , session : str , answer : str , message : str
46
50
):
47
51
"""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 :
50
54
opened_file .write ("{}!{}:{}\n " .format (part , answer , message ))
51
55
52
56
@@ -57,9 +61,9 @@ def last_submitted_answer_message(
57
61
Check if answer is already submitted by user if submitted return message of last
58
62
submission
59
63
"""
60
- submission_file = _join_path (year , day , session , file_type = "submission_file" )
64
+ submission_file = cache_file_path (year , day , session , FileType . SUBMISSION_FILE )
61
65
last_answer_message = ""
62
- with open (submission_file , "r" ) as opened_file :
66
+ with submission_file . open () as opened_file :
63
67
lines = opened_file .read ()
64
68
for line in lines :
65
69
separate_part = line .split ("!" , 1 )
@@ -72,33 +76,27 @@ def last_submitted_answer_message(
72
76
73
77
def save_last_submission_time (year : int , day : int , session : str ):
74
78
"""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 :
77
81
opened_file .write (str (time .time ()))
78
82
79
83
80
84
def check_less_than_one_min_submission (year : int , day : int , session : str ) -> bool :
81
85
"""
82
86
Check last submission time for solution return true if time is less than 60 second
83
87
"""
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 :
86
90
last_time = float (opened_file .read ())
87
91
current_time = time .time ()
88
92
early_submission = current_time - last_time < 60.0
89
93
return early_submission
90
94
91
95
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 :
95
97
"""Return desire path for a cache folders or files"""
96
98
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 )
0 commit comments