[mod] introduce default settings behavior to clean the code

This commit is contained in:
Laurent Peuch 2016-04-18 19:58:43 +02:00
parent 63988533c8
commit 2a9f761584

View File

@ -10,8 +10,14 @@ from jinja2 import Environment, FileSystemLoader
from .cache import CACHE from .cache import CACHE
from .utils import error, warning, okgreen from .utils import error, warning, okgreen
SETTINGS = { DEFAULTS = {
"rss": True,
"share": False,
"settings": {},
"show_date": True, "show_date": True,
}
SETTINGS = {
"gm": { "gm": {
"quality": 75, "quality": 75,
"auto-orient": True, "auto-orient": True,
@ -97,19 +103,20 @@ def main():
settings = yaml.safe_load(open("settings.yaml", "r")) settings = yaml.safe_load(open("settings.yaml", "r"))
for key, value in DEFAULTS.items():
if key not in settings:
settings[key] = value
error(isinstance(settings, dict), "Your settings.yaml should be a dict") error(isinstance(settings, dict), "Your settings.yaml should be a dict")
error(settings.get("title"), "You should specify a title in your main settings.yaml") error(settings.get("title"), "You need to specify a title in your main settings.yaml")
if settings.get("rss") or settings.get("share"): if settings["rss"] or settings["share"]:
error(settings.get("url"), "If you want the rss and/or the social network share work, " error(settings.get("url"), "If you want the rss and/or the social network share to work, "
"you should specify url in main settings") "you need to specify the website url in root settings")
if settings.get("settings", {}).get("gm"): if settings["settings"].get("gm"):
SETTINGS["gm"].update(settings["settings"]["gm"]) SETTINGS["gm"].update(settings["settings"]["gm"])
if not "show_date" in settings:
settings["show_date"] = SETTINGS["show_date"]
front_page_galleries_cover = [] front_page_galleries_cover = []
dirs = filter(lambda x: x not in (".", "..") and os.path.isdir(x) and os.path.exists(os.path.join(os.getcwd(), x, "settings.yaml")), os.listdir(os.getcwd())) dirs = filter(lambda x: x not in (".", "..") and os.path.isdir(x) and os.path.exists(os.path.join(os.getcwd(), x, "settings.yaml")), os.listdir(os.getcwd()))
@ -121,7 +128,7 @@ def main():
if not os.path.exists("build"): if not os.path.exists("build"):
os.makedirs("build") os.makedirs("build")
theme = settings.get("settings", {}).get("theme", "exposure") theme = settings["settings"].get("theme", "exposure")
theme_path = os.path.exists(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme)) theme_path = os.path.exists(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes", theme))
available_themes = theme, "', '".join(os.listdir(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes"))) available_themes = theme, "', '".join(os.listdir(os.path.join(os.path.split(os.path.realpath(__file__))[0], "themes")))
@ -184,7 +191,7 @@ def main():
template_to_render = page_template if gallery_settings.get("static") else gallery_index_template template_to_render = page_template if gallery_settings.get("static") else gallery_index_template
open(os.path.join("build", gallery, "index.html"), "w").write(template_to_render.render(settings=settings, gallery=gallery_settings, Image=Image, link=gallery).encode("Utf-8")) open(os.path.join("build", gallery, "index.html"), "w").write(template_to_render.render(settings=settings, gallery=gallery_settings, Image=Image, link=gallery).encode("Utf-8"))
if settings.get("rss"): if settings["rss"]:
open(os.path.join("build", "feed.xml"), "w").write(feed_template.render(settings=settings, link=gallery, galleries=reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))).encode("Utf-8")) open(os.path.join("build", "feed.xml"), "w").write(feed_template.render(settings=settings, link=gallery, galleries=reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))).encode("Utf-8"))
front_page_galleries_cover = reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"])) front_page_galleries_cover = reversed(sorted(front_page_galleries_cover, key=lambda x: x["date"]))