Removed regex handler from Parser and fixed bugs in Generators

parent d2d01ce9
......@@ -13,6 +13,10 @@ class Generator:
with open(const.MAPS_DIR + "/%s.map" % project_name, "w") as map_file:
map_file.write(redirects_map)
for code, data in redirects_with_options:
if code == "301":
code = "permanent"
elif code == "302":
code = "redirect"
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:
......@@ -46,6 +50,10 @@ class MapGenerator:
def generate_opt_map(self, redirect_dict, project_name):
result = []
for code, redirects in redirect_dict.items():
if code == "301":
code = "permanent"
elif code == "302":
code = "redirect"
data = self.opt_start_line % (project_name, code)
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg1, arg2)
......@@ -66,6 +74,11 @@ class ConfigGenerator:
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:
if code == "permanent":
data += (self.opt_start_line % (project_name, "permanent")) + (self.opt_rewrite_line % (project_name, project_name, "permanent", "permanent")) + "}\n"
elif code == "redirect":
data += (self.opt_start_line % (project_name, "redirect")) + (self.opt_rewrite_line % (project_name, project_name, "redirect", "redirect")) + "}\n"
else:
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,8 +17,14 @@ class MapLineParser:
def parse_line(self, line, prefix, i):
line = line.strip()
return_code, options = 0, []
import pdb
pdb.set_trace()
"""
return_codes:
-1: blank line
0 : default redirect
1 : option redirect
2 : absolute url
3 : regexp
"""
if line.startswith("#") or len(line) == 0:
return -1, None, None
......@@ -31,29 +37,26 @@ class MapLineParser:
elif len(line) > 2:
return_code = 1
options = [option[1:-1] for option in line[2:]]
print(line[0])
import pdb
pdb.set_trace()
if line[0].startswith("~"):
line[0] = "~" + prefix + line[0][1:]
if not self.url_regexp.fullmatch(line[0]): # not url - regexp
try:
re.compile(line[0])
re.compile(line[1])
except re.error:
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
if 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]
line[0] = prefix + line[0] if not line[0].startswith("~") else line[0]
elif self.url_absolute.fullmatch(line[1]):
line[0] = prefix + line[0]
options = ['301']
return_code = 1
else: # default url
line[0] = prefix + line[0]
line[0] = prefix + line[0] if not line[0].startswith("~") else line[0]
line[1] = prefix + line[1]
return return_code, line[0], line[1], options
......@@ -126,7 +129,7 @@ class ConfigReader:
elif return_code == 1:
opt_ = option[0][0] if option else option
if opt_ in res[1].keys():
res[1][opt_].append[(request_url, redirect_url)]
res[1][opt_].append([request_url, redirect_url])
else:
res[1][opt_] = [(request_url, redirect_url)]
......
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