Remade and debugged .conf and .map files generation

parent a62218c4
from dev import const
class Generator:
def __init__(self):
self.map_gen = MapGenerator()
self.conf_gen = ConfigGenerator()
def generate(self, redirects_data, project_name):
redirects_map, options_map = self.map_gen.generate_map(redirects_data, project_name)
conf_data = self.conf_gen.generate_conf(project_name)
try:
with open(const.MAPS_DIR + "/%s.map" % project_name, "w") as map_file:
map_file.write(redirects_map)
with open(const.MAPS_DIR + "/%s_options.map" % project_name, "w") as map_file:
map_file.write(options_map)
with open(const.CONFIG_DIR + "/%s.conf" % project_name, "w") as conf_file:
conf_file.write(conf_data)
except Exception as e:
print(e)
raise self.GenerationError("Can\'t generate new .conf or new .map file \
from %s .map file for %s project" % (map_file, project_name), e)
class GenerationError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class MapGenerator:
def __init__(self):
self.start_line = "map $uri $%s_redirect {"
self.codes_dict = {'301':"permanent", '302':'redirect'}
self.start_line = ["map $uri $%s_redirect {", "map $uri $%s_redirect_option {"]
self.status_codes = {'301':"redirect"}
self.endline = "\n}"
def generate_map(self, redirects_sorted, project_name):
data = self.start_line % project_name
data = self.start_line[0] % project_name
data2 = self.start_line[1] % project_name
for arg1, arg2 in redirects_sorted[0]:
data += "\n\t%s\t%s;" % (arg1, arg2)
for arg1, arg2 in redirects_sorted[1]:
data += "\n\t%s\t%s;" % (arg1, arg2)
"""
for i, args in enumerate(redirects_sorted[1]):
try:
key_ = redirects_sorted[2][i]
except KeyError:
key_ = None
data = "\n\t%s\t%s;" % (args[0], args[1]) if not key_
else "\n\t%s\t%s\t%s;" % (args[0], args[1], key_)
"""
data += self.endline
return data
data2 += "\n\t%s\t%s;" % (arg1, "redirect")
for i, item in enumerate(redirects_sorted[1]):
data += "\n\t%s\t%s;" % (item[0], item[1])
data2 += "\n\t%s\t%s;" % (item[0],self.status_codes[redirects_sorted[2][i][1][0][0]])
data += self.endline + "\n"
data2 += self.endline
return data, data2
class ConfigGenerator:
def __init__(self):
self.start_line = "if ($%s_redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.^)$ $%s_redirect redirect;\n}"
self.rewrite_line = "\trewrite ^/%s/(.*)$ $%s_redirect $%s_redirect_option;\n}"
def generate_conf(self, project_name):
return (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name))
return (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name, project_name))
......@@ -14,10 +14,11 @@ redirector_watch = None
class Redirector:
def __init__(self, logger=None):
self.parser = parser.ConfigReader(logger=logger)
self.map_generator = generators.MapGenerator()
self.config_generator = generators.ConfigGenerator()
self.generator = generators.Generator()
def generate(self, yaml_file, map_file):
import pdb
pdb.set_trace()
project_name = "Error"
try:
data, project_name = self.parser.parse_map(map_file, yaml_file)
......@@ -26,26 +27,13 @@ class Redirector:
# FIXME: what is the better way to do so?
except Exception as e:
raise self.RedirectorParserError("Can\'t parse .map file %s" % map_file, e)
try:
with open(const.MAPS_DIR + "/%s.map" % project_name, "w") as map_file:
map_file.write(self.map_generator.generate_map(data, project_name))
with open(const.CONFIG_DIR + "/%s.conf" % project_name, "w") as conf_file:
conf_file.write(self.config_generator.generate_conf(project_name))
except Exception as e:
print(e)
raise self.RedirectorGenerationError("Can\'t generate new .conf or new .map file \
from %s .map file for %s project" % (map_file, project_name), e)
self.generator.generate(data, project_name)
class RedirectorParserError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class RedirectorGenerationError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class RedirectorInputDataError(Exception):
def __init__(self, message, errors):
super().__init__(message)
......@@ -121,7 +109,7 @@ def main(args=None):
redirector = Redirector()
try:
redirector.generate(args.yaml_file[0], args.map_file[0])
except (Redirector.RedirectorGenerationError, redirector.RedirectorParserError) as e:
except (generators.Generator.GenerationError, redirector.RedirectorParserError) as e:
print("CRITICAL:\n" + str(e))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment