templating - init work on templating with jinja2
This commit is contained in:
parent
ea891969c1
commit
c65dda3917
|
@ -3,8 +3,8 @@ import json, re
|
|||
class Settings :
|
||||
|
||||
def __init__(self) :
|
||||
self.settings = {}
|
||||
self.variables = {}
|
||||
self.__settings = {}
|
||||
self.__variables = {}
|
||||
|
||||
def load_settings(self, path) :
|
||||
with open(path, "r") as f :
|
||||
|
@ -16,20 +16,28 @@ class Settings :
|
|||
else :
|
||||
real_params = [param]
|
||||
for real_param in real_params :
|
||||
self.settings[real_param["env"]] = real_param
|
||||
self.settings[real_param["env"]]["category"] = cat
|
||||
self.__settings[real_param["env"]] = real_param
|
||||
self.__settings[real_param["env"]]["category"] = cat
|
||||
|
||||
def load_variables(self, vars, multisite_only=False) :
|
||||
for var, value in vars.items() :
|
||||
if self.__check_var(var, value) :
|
||||
self.variables[var] = value
|
||||
self.__variables[var] = value
|
||||
else :
|
||||
print("Problem with " + var + "=" + value)
|
||||
|
||||
def get_config(self) :
|
||||
config = {}
|
||||
for setting in self.__settings :
|
||||
config[setting] = self.__settings[setting]["default"]
|
||||
for variable, value in self.__variables.items() :
|
||||
config[variable] = value
|
||||
return config
|
||||
|
||||
def __check_var(self, var, value, multisite_only=False) :
|
||||
real_var = ""
|
||||
if var in self.settings :
|
||||
if var in self.__settings :
|
||||
real_var = var
|
||||
elif var[len(var.split("_")[0])+1:] in self.settings :
|
||||
elif var[len(var.split("_")[0])+1:] in self.__settings :
|
||||
real_var = var[len(var.split("_")[0])+1:]
|
||||
return real_var != "" and re.search(self.settings[real_var]["regex"], value) and (not multisite_only or self.settings[real_var]["context"] == "multisite")
|
||||
return real_var != "" and re.search(self.__settings[real_var]["regex"], value) and (not multisite_only or self.__settings[real_var]["context"] == "multisite")
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
import jinja2, glob, os, pathlib
|
||||
|
||||
class Templates :
|
||||
|
||||
def __init__(self, config, input_path) :
|
||||
self.__config = config
|
||||
self.__input_path = input_path
|
||||
self.__template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=input_path))
|
||||
|
||||
def render_global(self, output_path) :
|
||||
return self.__render("global", output_path)
|
||||
|
||||
def render_site(self, output_path, server_name) :
|
||||
return self.__render("site", output_path, server_name)
|
||||
|
||||
def __render(self, type, output_path, server_name=None) :
|
||||
for filename in glob.iglob(self.__input_path + "/" + type + "**/**", recursive=True) :
|
||||
if os.path.isfile(filename) :
|
||||
relative_filename = filename.replace(self.__input_path, "").replace(type + "/", "")
|
||||
template = self.__template_env.get_template(type + "/" + relative_filename)
|
||||
output = template.render(self.__config)
|
||||
if "/" in relative_filename :
|
||||
directory = relative_filename.replace(relative_filename.split("/")[-1], "")
|
||||
pathlib.Path(output_path + "/" + directory).mkdir(parents=True, exist_ok=True)
|
||||
with open(output_path + "/" + relative_filename, "w") as f :
|
||||
f.write(output)
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from Settings import Settings
|
||||
from Templates import Templates
|
||||
|
||||
if __name__ == "__main__" :
|
||||
|
||||
|
@ -10,4 +11,8 @@ if __name__ == "__main__" :
|
|||
variables["MULTISITE"] = "yes"
|
||||
variables["BLOCK_PROXIES"] = "no"
|
||||
variables["omg"] = "lol"
|
||||
variables["www.toto.com_BLOCK_PROXIES"] = "yes"
|
||||
my_settings.load_variables(variables)
|
||||
print(my_settings.get_config())
|
||||
my_templates = Templates(my_settings.get_config(), "/tmp/input")
|
||||
my_templates.render_global("/tmp/output")
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
jinja2
|
|
@ -495,7 +495,7 @@
|
|||
"label": "Use crowdsec",
|
||||
"regex": "^(yes|no)$",
|
||||
"type": "checkbox"
|
||||
}
|
||||
},
|
||||
{
|
||||
"context": "global",
|
||||
"default": "",
|
||||
|
@ -1184,6 +1184,7 @@
|
|||
"regex": "^\\S+$",
|
||||
"type": "text"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nginx": {
|
||||
"id": "nginx",
|
||||
|
@ -1269,7 +1270,8 @@
|
|||
"regex": "^[0-9]+$",
|
||||
"type": "text"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"Whitelist": {
|
||||
"id": "whitelist",
|
||||
"params": [
|
||||
|
@ -1333,7 +1335,7 @@
|
|||
"env": "WHITELIST_URI",
|
||||
"id": "whitelist-uri",
|
||||
"label": "Whitelist URI",
|
||||
"regex": "^(\S ?)*$",
|
||||
"regex": "^(\\S ?)*$",
|
||||
"type": "text"
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue