Split out config file handling into its own function

This commit is contained in:
A.M. Rowsell 2025-02-25 18:14:37 -05:00
parent 8229a14cfe
commit a1a6998e52
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9

View file

@ -56,30 +56,43 @@ def get_description(feed):
return desc return desc
def main(): def setupPaths():
os.environ["TZ"] = "America/Toronto" global app_config
time.tzset()
# Check for log and config files/paths, create empty directories if needed # Check for log and config files/paths, create empty directories if needed
# TODO: make this cleaner # TODO: make this cleaner
try: if not Path(log_file_path).exists():
Path(log_file_path).mkdir(parents=True, exist_ok=True) print("No log file path exists. Yark! We'll try and make {}...", log_dir)
except FileExistsError: try:
print( Path(log_dir).mkdir(parents=True, exist_ok=True)
"The logfile path {} already exists and is not a directory!".format( except FileExistsError:
log_file_path print("The path {} already exists and is not a directory!".format(log_dir))
)
)
if not Path(config_file_path).exists(): if not Path(config_file_path).exists():
print( print(
"No config file at {}! Snarf.\n{} was created for you.".format( "No config file at {}! Snarf. We'll try and make {}...".format(
config_file_path, config_dir config_file_path, config_dir
) )
) )
Path(config_dir).mkdir(parents=True, exist_ok=True) try:
Path(config_dir).mkdir(parents=True, exist_ok=True)
except FileExistsError:
print(
"The config dir {} already exists and is not a directory! Please fix manually.".format(
config_dir
)
)
sys.exit(255)
return return
# Loading the config file
with open(config_file_path, "r") as config_file: with open(config_file_path, "r") as config_file:
app_config = json.load(config_file) app_config = json.load(config_file)
return
def main():
os.environ["TZ"] = "America/Toronto"
time.tzset()
now = time.mktime(time.localtime()) now = time.mktime(time.localtime())
setupPaths() # Handle the config and log paths
try: try:
last_check = app_config["lastupdate"] last_check = app_config["lastupdate"]
except KeyError: except KeyError: