Added .conf and .map file generators and updated RedirectorParser

parent 578c37f3
class MapGenerator:
def __init__(self, logger=None):
self.logger = logger
self.start_lines = [
"map $uri $%s-redirect {\n",
"\nmap $uri $%s-option-redirect {\n",
"\nmap $uri $%s-option {\n"
]
self.endline = "\n}"
def generate_map(self, redirects_sorted, project_name):
data = ""
for i, map_type in enumerate(self.start_lines):
data += map_type % project_name
for arg1, arg2 in redirects_sorted[i]:
data += "\t%s\t%s;" % (arg1, arg2)
data += self.endline
return data
class ConfigGenerator:
def __init__(self):
self.start_line = "if($%s-redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.^)$ %s redirect;\n}"
def generate_conf(self, project_name):
return self.start_line % project_name + self.rewrite_line % project_name
......@@ -16,22 +16,19 @@ class RedirectorParser:
def _parse_line(self, line, prefix, i):
if not line.startswith("#"):
line = line.split()
url_type, options = [], []
return_code, options = 0, []
if len(line) == 0:
return ["skip"], None, None
return -1, None, None
elif len(line) < 2:
raise self.ParseLineError("Error on %s:{line};\nNot enough arguments to parse!".format(line=i))
elif len(line) > 2:
url_type.append("options")
return_code = 1
options = [option[1:-1] for option in line[2:]]
else:
url_type.append("no-options")
# not url - regexp
if not self.url_regexp.fullmatch(line[0]):
try:
re.compile(line[0])
re.compile(line[1])
url_type.append("regexp")
except re.error:
raise self.RegexpTestError("Can\'t compile regular expressions {expression1} {expression2} in "
"%s:{line_num}".format(expression1=line[0], expression2=line[1],
......@@ -39,20 +36,17 @@ class RedirectorParser:
# if new URI relative to the root
elif line[0].startswith("//"):
line[0] = line[0][1:] # cutting out extra '/' at the beginning
url_type.append("root")
elif line[1].startswith("//"):
line[1] = line[1][1:]
url_type.append("root")
elif self.url_absolute.fullmatch(line[1]):
url_type.append("absolute")
pass
# default url
else:
line[0] = prefix + line[0]
line[1] = prefix + line[1]
url_type.append('plain')
return url_type, line[0], line[1], options
return return_code, line[0], line[1], options
else:
return ["skip"], None, None
return -1, None, None
@staticmethod
def _parse_yaml(file_dir):
......@@ -77,39 +71,32 @@ class RedirectorParser:
print(level.upper() + ":\n" + message)
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:
# ???: Is the last directory of map_path a project's name?
res_prefix = prefix.split("/")[-1]
with open(map_file, "r") as file:
for i, line in enumerate(file):
request_url, redirect_url, options = None, None, []
try:
type_, request_url, redirect_url, *options = self._parse_line(line, prefix, i)
return_code, request_url, redirect_url, *options = self._parse_line(line, prefix, i)
except self.RegexpTestError as e:
self.log(e.message % map_file)
except self.ParseLineError as e:
self.log(e.message % map_file)
if not request_url or not redirect_url or type_[0] == "skip":
if not request_url or not redirect_url or return_code == -1:
continue
else:
if type_[1] == "plain":
if type_[0] == "no-options":
if return_code == 0:
res[0].append((request_url, redirect_url))
else:
res[1].append((request_url, redirect_url, *options))
elif type_[1] == "regexp":
if type_[0] == "no-options":
res[2].append((request_url, redirect_url))
else:
res[3].append((request_url, redirect_url, *options))
elif type_[1] == "absolute":
if type_[0] == "no-options":
res[4].append((request_url, redirect_url))
else:
res[5].append((request_url, redirect_url, *options))
return res
elif return_code == 1:
res[1].append((request_url, redirect_url))
res[2].append((request_url, options))
return res, res_prefix
except yaml.YAMLError as e:
self.log("Error occured while reading %s" % yaml_file + str(e))
......@@ -131,13 +118,4 @@ class RedirectorParser:
"""
def __init__(self, message):
self.message = message
def main():
parser = RedirectorParser()
pp(parser._parse_map("tests/test.map", "tests/test.yaml"))
if __name__ == "__main__":
main()
self.message = messages
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