Updated Generators, parser code options and Redirector\'s use of Generators

parent 1462c067
......@@ -6,13 +6,15 @@ class Generator:
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)
redirects_map = self.map_gen.generate_map(redirects_data[0], project_name)
redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], project_name)
conf_data = self.conf_gen.generate_config(project_name, [code for code, data in redirects_with_options])
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)
for code, data in redirects_with_options:
with open(const.MAPS_DIR + "/%s_%s_options.map" % (project_name, code), "w") as map_file:
map_file.write(data)
with open(const.CONFIG_DIR + "/%s.conf" % project_name, "w") as conf_file:
conf_file.write(conf_data)
except Exception as e:
......@@ -29,30 +31,42 @@ class Generator:
class MapGenerator:
def __init__(self):
self.start_line = ["map $uri $%s_redirect {", "map $uri $%s_redirect_option {"]
self.status_codes = {'301':"redirect"}
self.start_line = "map $uri $%s_redirect {"
self.opt_start_line = "map $uri $%s_%s_redirect {" # (project_name, redirect_option)
self.endline = "\n}"
def generate_map(self, redirects_sorted, project_name):
data = self.start_line[0] % project_name
data2 = self.start_line[1] % project_name
for arg1, arg2 in redirects_sorted[0]:
def generate_map(self, redirects, project_name):
data = self.start_line % project_name
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg1, arg2)
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
data += self.endline
return data
def generate_opt_map(self, redirect_dict, project_name):
result = []
for code, redirects in redirect_dict.items():
data = self.opt_start_line % (project_name, code)
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg1, arg2)
data += self.endline
result.append((code, data))
return result
class ConfigGenerator:
def __init__(self):
self.start_line = "if ($%s_redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.*)$ $%s_redirect $%s_redirect_option;\n}"
self.opt_start_line = "if ($%s_%s_redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.*)$ $%s_redirect %s;\n"
self.opt_rewrite_line = "\trewrite ^/%s/(.*)$ $%s_%s_redirect %s;\n"
self.option_line = "return %s;\n}\n"
def generate_conf(self, project_name):
return (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name, project_name))
def generate_config(self, project_name, options):
data = (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name, "break")) + "}\n"
for code in options:
data += (self.opt_start_line % (project_name, code)) + (self.opt_rewrite_line % (project_name, project_name, code, "break")) + (self.option_line % code)
return data
......@@ -17,6 +17,8 @@ class MapLineParser:
def parse_line(self, line, prefix, i):
line = line.strip()
return_code, options = 0, []
import pdb
pdb.set_trace()
if line.startswith("#") or len(line) == 0:
return -1, None, None
......@@ -38,14 +40,17 @@ class MapLineParser:
raise self.RegexpTestError("Can\'t compile regular expressions {expression1} {expression2} in "
"%s:{line_num}".format(expression1=line[0], expression2=line[1],
line_num=i), re.error)
elif line[0].startswith("//"): # if new URI relative to the root
line[0] = line[0][1:] # cutting out extra '/' at the beginning
line[1] = prefix + line[1]
elif line[1].startswith("//"):
line[1] = line[1][1:]
line[0] = prefix + line[0]
elif self.url_absolute.fullmatch(line[1]):
pass
line[0] = prefix + line[0]
else: # default url
line[0] = prefix + line[0]
......@@ -96,17 +101,17 @@ class ConfigReader:
return return_list
def parse_map(self, map_file, yaml_file):
res = [[], [], []]
res = [[], {}]
res_prefix = None
try:
for map_path, prefix in self.parse_yaml(yaml_file):
if map_path == map_file or os.getcwd() + "/" + map_path == map_file:
res_prefix = prefix.split("/")[-1] # ???: Is the last directory of map_path a project's name?
res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name
with open(map_file, "r") as file:
for i, line in enumerate(file):
request_url, redirect_url, options = None, None, []
request_url, redirect_url, option = None, None, []
try:
return_code, request_url, redirect_url, *options = \
return_code, request_url, redirect_url, *option = \
self.line_parser.parse_line(line, prefix, i)
except MapLineParser.RegexpTestError as e:
self.logger.log(e.message % map_file)
......@@ -119,8 +124,12 @@ class ConfigReader:
if return_code == 0:
res[0].append((request_url, redirect_url))
elif return_code == 1:
res[1].append((request_url, redirect_url))
res[2].append((request_url, options))
opt_ = option[0][0] if option else option
if opt_ in res[1].keys():
res[1][opt_].append[(request_url, redirect_url)]
else:
res[1][opt_] = [(request_url, redirect_url)]
return res, res_prefix
except YAMLError as e:
self.logger.log("Error occurred while reading %s" % yaml_file + str(e))
......
......@@ -17,8 +17,6 @@ class Redirector:
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)
......
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