Commit 36a4d1b2 authored by Soldatoff's avatar Soldatoff

Changed the processing and formatting of project_name, prefix variables

parent 44f1a1a5
......@@ -13,14 +13,14 @@ class Redirector:
def generate(self, yaml_file, map_file):
project_name = "Error"
try:
data, project_name = self.parser.parse_map(map_file, yaml_file)
data = self.parser.read_map(map_file)
project_name, prefix = self.parser.parse_map(map_file, yaml_file)
if not data:
raise self.RedirectorInputDataError("Can\'t properly parse .map or .yaml files", Exception())
# 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)
self.generator.generate(data, project_name, map_file)
self.generator.generate(data, project_name, prefix)
class RedirectorParserError(Exception):
def __init__(self, message, errors):
......
from redirector.utils.const import MAPS_DIR, CONFIG_DIR, CONFIG
from redirector.utils.parser import ConfigReader
from redirector.utils.const import MAPS_DIR, CONFIG_DIR
class Generator:
def __init__(self):
......@@ -9,45 +8,41 @@ class Generator:
def get_conf_dirs(self):
return MAPS_DIR, CONFIG_DIR
def get_essential(self, arg_map = None):
return_dict = {"project_name" :'',"prefix" :'', "custom" : '',"dir_name" : '' }
if arg_map:
return_dict["dir_name"] = arg_map.split("/")[-2]
if len(ConfigReader.parse_yaml(CONFIG)[0][1]) > len("/"): #prefix != "/"
return_dict["prefix"] = (ConfigReader.parse_yaml(CONFIG)[0][1].split("/")[-1])
return_dict["project_name"] = (ConfigReader.parse_yaml(CONFIG)[0][1].split("/")[-1])
elif len(ConfigReader.parse_yaml(CONFIG)[0][2]) > len("/"): #name != "/" and prefix == "/"
return_dict["custom"] = (ConfigReader.parse_yaml(CONFIG)[0][2].split("/")[-1])
return_dict["project_name"] = return_dict["custom"]+"-"+return_dict["dir_name"]
else: #name == "/" and prefix == "/"
return_dict["project_name"] = return_dict["dir_name"]
return return_dict
def generate(self, redirects_data, project_name, arg_map = None):
arg = self.get_essential(arg_map)
redirects_map = self.map_gen.generate_map(redirects_data[0], arg)
redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], arg)
conf_data = self.conf_gen.generate_config(arg["project_name"], [code for code, data in redirects_with_options],arg["prefix"])
maps_dir, config_dir = self.get_conf_dirs()
def write_map(self, maps_dir, project_name, redirects_map, redirects_with_options):
try:
with open("{}/{}.map".format(maps_dir,arg["project_name"]), "w") as map_file:
with open("{}/{}.map".format(maps_dir,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(maps_dir + "/%s_%s_options.map" % (arg["project_name"], code), "w") as map_file:
with open(maps_dir + "/%s_%s_options.map" % (project_name, code), "w+") as map_file:
map_file.write(data)
with open(config_dir + "/%s.conf" % arg["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 \
raise self.GenerationError("Can\'t generate new .map file \
from %s .map file for %s project" % (map_file, project_name), e)
def write_conf(self, config_dir, project_name, conf_data):
try:
with open(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 \
from %s .conf file for %s project" % (conf_file, project_name), e)
def generate(self, redirects_data, project_name, prefix):
redirects_map = self.map_gen.generate_map(redirects_data[0], project_name, prefix)
redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], project_name, prefix)
conf_data = self.conf_gen.generate_config(project_name, [code for code, data in redirects_with_options],prefix)
maps_dir, config_dir = self.get_conf_dirs()
self.write_map(maps_dir, project_name, redirects_map, redirects_with_options)
self.write_conf(config_dir, project_name, conf_data)
class GenerationError(Exception):
def __init__(self, message, errors):
super().__init__(message)
......@@ -62,38 +57,40 @@ class MapGenerator:
self.endline = "\n}"
def generate_map(self, redirects, arg):
if arg["prefix"]:
data = self.start_line % arg["prefix"]
def generate_map(self, redirects, project_name, prefix):
if prefix:
data = self.start_line % prefix
else:
data = self.start_line % arg["project_name"]
if arg["project_name"] == arg["prefix"]:
arg["prefix"]= "/{}".format(arg["prefix"])
data = self.start_line % project_name
if project_name == prefix:
prefix = "/{}".format(prefix)
for arg1, arg2 in redirects:
# print('redirect is', arg1, arg2)
data += "\n\t%s\t%s;" % (arg["prefix"]+arg1, arg["prefix"]+arg2)
data += "\n\t%s\t%s;" % (prefix+arg1, prefix+arg2)
data += self.endline
return data
def generate_opt_map(self, redirect_dict, arg):
def generate_opt_map(self, redirect_dict, project_name, prefix):
result = []
for code, redirects in redirect_dict.items():
if project_name == prefix:
prefix = "/{}".format(prefix)
if code == "301":
code = "permanent"
data = self.opt_start_line % (arg["project_name"], code)
data = self.opt_start_line % (project_name, code)
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg["prefix"]+arg1, arg2)
data += "\n\t%s\t%s;" % (prefix+arg1, arg2)
data += self.endline
elif code == "302":
code = "redirect"
data = self.opt_start_line % (arg["project_name"], code)
data = self.opt_start_line % (project_name, code)
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg["prefix"]+arg1, arg["prefix"]+arg2)
data += "\n\t%s\t%s;" % (prefix+arg1, prefix+arg2)
data += self.endline
elif code == "status=301":
data = self.opt_start_line % (arg["project_name"], code)
data = self.opt_start_line % (project_name, code)
for arg1, arg2 in redirects:
data += "\n\t%s\t%s;" % (arg["prefix"]+arg1, arg["prefix"]+arg2)
data += "\n\t%s\t%s;" % (prefix+arg1, prefix+arg2)
data += self.endline
result.append((code, data))
return result
......@@ -105,17 +102,19 @@ class ConfigGenerator:
self.start_line = "if ($%s_redirect) {\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.opt_rewrite_line = "\trewrite ^%s/(.*)$ $%s_%s_redirect %s;\n"
self.option_line = "return %s;\n}\n"
def generate_config(self, project_name, options, prefix):
data = (self.start_line % project_name) + (self.rewrite_line % ("{}".format(prefix), project_name, "redirect")) + "}\n"
if project_name == prefix:
prefix = "/{}".format(prefix)
data = (self.start_line % project_name) + (self.rewrite_line % (prefix, project_name, "redirect")) + "}\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"
data += (self.opt_start_line % (project_name, "permanent")) + (self.opt_rewrite_line % (prefix, 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"
data += (self.opt_start_line % (project_name, "redirect")) + (self.opt_rewrite_line % (prefix, project_name, "redirect", "redirect")) + "}\n"
else:
data += (self.opt_start_line % (project_name, code)) + (self.opt_rewrite_line % (project_name, project_name, code, "redirect")) + (self.option_line % code)
data += (self.opt_start_line % (project_name, code)) + (self.opt_rewrite_line % (prefix, project_name, code, "redirect")) + (self.option_line % code)
return data
......@@ -15,7 +15,7 @@ class MapLineParser:
r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$")
self.logger = logger
def parse_line(self, line, prefix, i):
def parse_line(self, line, i):
line = line.strip()
return_code, options = 0, []
"""
......@@ -90,6 +90,20 @@ class ConfigReader:
self.logger = logger
@staticmethod
def get_projectname_prefix(map_path, prefix, custom_name):
if map_path: #path to map file
dir_name = map_path.split("/")[-2]
if len(prefix) > len("/"): #prefix != "/"
project_name = (prefix.split("/")[-1])
elif len(custom_name) > len("/"): #name != "/" and prefix == "/"
custom = (custom_name.split("/")[-1])
project_name = custom + "_" + dir_name
else: #name == "/" and prefix == "/"
project_name = dir_name
prefix = prefix.split("/")[-1]
return project_name, prefix
@staticmethod
def parse_yaml(file_dir):
return_list = []
yaml_dir = os.path.dirname(os.path.abspath(file_dir))
......@@ -111,36 +125,18 @@ class ConfigReader:
return return_list
def parse_map(self, map_file, yaml_file):
def read_map(self, map_path):
res = [[], {}]
res_prefix = None
# normalize path
yaml_dir = os.path.dirname(os.path.abspath(yaml_file)) # get yaml file directory for restore rel path to map file
map_dir = os.path.dirname(os.path.abspath(map_file))
map_file_name = os.path.basename(map_file) # get filename from arg
try:
for map_path, prefix, name in self.parse_yaml(yaml_file):
abs_map_path = get_map_path(map_path, map_dir)
#if map_file_name not in abs_map_path:
# continue
res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name
f = open(map_path, "r")
for i, line in enumerate(f):
request_url, redirect_url, option = None, None, []
try:
return_code, request_url, redirect_url, *option = \
self.line_parser.parse_line(line, prefix, i)
self.line_parser.parse_line(line, i)
except MapLineParser.RegexpTestError as e:
self.logger.log(e.message % map_file)
self.logger.log(e.message % map_path)
except MapLineParser.ParseLineError as e:
self.logger.log(e.message % map_file)
self.logger.log(e.message % map_path)
if request_url and redirect_url and return_code in [0, 1]:
if return_code == 0:
res[0].append((request_url, redirect_url))
......@@ -153,9 +149,24 @@ class ConfigReader:
res[1][opt_] = [(request_url, redirect_url)]
f.close()
return res
def parse_map(self, map_file, yaml_file):
# normalize path
yaml_dir = os.path.dirname(os.path.abspath(yaml_file)) # get yaml file directory for restore rel path to map file
map_dir = os.path.dirname(os.path.abspath(map_file))
map_file_name = os.path.basename(map_file) # get filename from arg
try:
for map_path, prefix, name in self.parse_yaml(yaml_file):
abs_map_path = get_map_path(map_path, map_dir)
if map_file_name not in abs_map_path:
continue
return res, res_prefix
project_name, prefix = self.get_projectname_prefix(map_path, prefix, name)
return project_name, prefix
except YAMLError as e:
self.logger.log("Error occurred while reading %s" % yaml_file + 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