-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfiguration.py
77 lines (57 loc) · 2.57 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Module handles configuration of the application."""
from typing import List
import os
import yaml
import logging
from sys import stdout
log = logging.getLogger("zdf-download")
class FilterConfiguration():
"""Configures filters within a show."""
def __setup_logging(self) -> None:
"""Configure logging for the application"""
log = logging.getLogger("zdf-download")
log.setLevel(logging.DEBUG)
log_formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
console_handler = logging.StreamHandler(stdout)
console_handler.setFormatter(log_formatter)
log.addHandler(console_handler)
def __init__(self, min_date: str) -> None:
self.__setup_logging()
self.min_date: str = min_date
class DownloadConfiguration():
"""Configures where a show is downloaded to."""
def __init__(self, folder: str, filename: str) -> None:
self.folder: str = folder
self.filename: str = filename
class ShowConfiguration():
"""Configures a show."""
def __init__(self, canonical_id: str, filter: FilterConfiguration, download: DownloadConfiguration) -> None:
self.canonical_id: str = canonical_id
self.filter: FilterConfiguration = filter
self.download: DownloadConfiguration = download
class Configuration():
"""Configures an application."""
def __init__(self, interval: int, shows: List[ShowConfiguration]) -> None:
self.interval: int = interval
self.shows: List[ShowConfiguration] = shows
def load_configuration_from_yaml(filename: str) -> Configuration:
"""Create a configuration-object from a yaml-file."""
if not os.path.isfile(filename):
print("Terminating... No configuration found")
exit()
with open(filename, "r") as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
interval: int = config["interval"]
shows: List[ShowConfiguration] = []
for show in config["shows"]:
show_filter: FilterConfiguration = FilterConfiguration(
min_date=show.get("filter").get("minDate"))
show_download: DownloadConfiguration = DownloadConfiguration(
folder=show.get("download").get("folder"),
filename=show.get("download").get("filename"))
show: ShowConfiguration = ShowConfiguration(
filter=show_filter,
download=show_download,
canonical_id=show.get("canonical-id"))
shows.append(show)
return Configuration(interval=interval, shows=shows)