-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
63 lines (45 loc) · 1.68 KB
/
config.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
from typing import Any, Dict, Union
import yaml
import utils
import websites
class Config:
def __init__(self, book: str, values: Dict[str, str]):
self.book = book
self.website = websites.from_config(values["website"])
self.values = values
def __getattr__(self, name):
return self.values.get(name)
def _get_website(config: dict):
print("[0] Custom")
for i, website in enumerate(websites.WEBSITES, 1):
print("[{}] {}".format(i, website.name))
website_index = utils.input_int("Website: ", 0, i)
if website_index > 0:
website = websites.WEBSITES[website_index - 1]
config["website"] = website.name
config.update(website.create_config())
else:
config["website"] = {
"toc_url": input("TOC url: "),
"toc_start": input("TOC start: "),
"toc_end": input("TOC end: "),
"toc_link": input("TOC link regex (optional): ") or 'href="(.*?)"',
"chapter_url": input("Chapter url: "),
"chapter_start": input("Chapter start: "),
"chapter_end": input("Chapter end: "),
}
def create_config(book: str):
config: Dict[str, Any] = {}
print("Creating new config for {}:".format(book))
_get_website(config)
name = input("Name? (optional) ")
if name:
config["name"] = name
with open(utils.get_config_file(book), "w") as f:
yaml.dump(config, f, default_flow_style=False)
print("Config created at:", utils.get_config_file(book))
print()
def load_config(book: str) -> Config:
with open(utils.get_config_file(book)) as f:
values = yaml.safe_load(f)
return Config(book, values)