Skip to content

Commit 52872c5

Browse files
committed
chore: validate presence of necessary files and some config
1 parent 5474236 commit 52872c5

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

weduc_timetable_extractor/__main__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import sys
2+
13
from playwright.sync_api import sync_playwright
24

35
from weduc_timetable_extractor import get_command_line_args
46
from weduc_timetable_extractor.config_management import (
57
get_chromium_path,
68
get_student_configs,
79
get_weduc_credentials,
10+
validate_chromium_path,
811
)
912
from weduc_timetable_extractor.google_calendar_management import (
1013
push_timetable_to_google_calendar,
@@ -20,14 +23,24 @@
2023
)
2124

2225

26+
def validate_at_least_one_student_config(student_configs):
27+
count = len(student_configs)
28+
29+
if count > 0:
30+
return count
31+
32+
sys.exit("Error: there are no student configs in config.ini")
33+
34+
2335
def main():
2436
args = get_command_line_args()
2537
weduc_credentials = get_weduc_credentials()
2638
use_headless = weduc_credentials["credentials_present"]
2739

2840
student_configs = get_student_configs()
41+
count = validate_at_least_one_student_config(student_configs)
2942

30-
print(f"Found {len(student_configs)} student config(s)")
43+
print(f"Found {count} student config(s)")
3144

3245
with sync_playwright() as p:
3346
print(
@@ -39,14 +52,15 @@ def main():
3952
print("Launching browser ...")
4053

4154
chromium_path = get_chromium_path()
42-
print("path", chromium_path)
55+
validate_chromium_path(chromium_path)
56+
print("Using browser located at:", chromium_path)
57+
4358
browser = p.chromium.launch(
4459
executable_path=chromium_path, headless=use_headless
4560
)
4661
page = browser.new_page()
4762

4863
page.goto("https://app.weduc.co.uk/")
49-
5064
login_to_weduc(page, weduc_credentials)
5165

5266
schools = extract_schools_data_from_weduc(page)

weduc_timetable_extractor/config_management.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def get_config():
2121
project_root = Path(__file__).resolve().parent.parent
2222

2323
config_file_path = project_root / "config.ini"
24+
validate_config_ini_path(config_file_path)
2425

2526
config = configparser.ConfigParser()
2627
config.read(config_file_path)
@@ -69,6 +70,18 @@ def get_chromium_path():
6970
return os_map.get(platform.system())
7071

7172

73+
def validate_config_ini_path(config_ini_file_path):
74+
if not config_ini_file_path.is_file():
75+
sys.exit(
76+
f"Error: unable to find a config.ini file at path '{config_ini_file_path}'"
77+
)
78+
79+
80+
def validate_chromium_path(chromium_path):
81+
if not Path(chromium_path).is_file():
82+
sys.exit(f"Error: unable to find a browser at path '{chromium_path}'")
83+
84+
7285
def get_student_configs():
7386
section_names = get_config().sections()
7487
student_section_names = [

weduc_timetable_extractor/google_calendar_management/get_google_calendar_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def get_credentials():
3030

3131
token_file_path = project_root / "token.json"
3232
credentials_file_path = project_root / "credentials.json"
33+
validate_credentials_path(credentials_file_path)
3334

3435
# Check if token.json exists, which stores the user's access and refresh tokens.
3536
if os.path.exists(token_file_path):
@@ -47,3 +48,11 @@ def get_credentials():
4748
with open(token_file_path, "w") as token:
4849
token.write(creds.to_json())
4950
return creds
51+
52+
53+
def validate_credentials_path(credentials_file_path):
54+
if not credentials_file_path.is_file():
55+
sys.exit(
56+
f"""Error: there is no Google Calendar API 'credentials.json' file present in the same folder as the executable.
57+
See the project documentation for info on setting this up: https://github.com/Holf/weduc-timetable-extractor/blob/main/README.md"""
58+
)

0 commit comments

Comments
 (0)