Commit e9910f24 authored by Soldatoff's avatar Soldatoff

remove the extra slash, which works without a prefix. Fixed .conf generation without prefix

parent 24a459b0
......@@ -3,7 +3,7 @@
%define biname nginx-redirector
Name: nginx-redirector
Version: 0.2.0
Version: 0.2.0.1
Release: alt1
Summary: CLI-utility for building nginx redirects
......
......@@ -14,13 +14,13 @@ class Redirector:
project_name = "Error"
try:
data, project_name = self.parser.parse_map(map_file, yaml_file)
if not project_name:
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)
self.generator.generate(data, project_name, map_file)
class RedirectorParserError(Exception):
def __init__(self, message, errors):
......
from redirector.utils.const import MAPS_DIR, CONFIG_DIR
from redirector.utils.const import MAPS_DIR, CONFIG_DIR, CONFIG
from redirector.utils.parser import ConfigReader
class Generator:
def __init__(self):
......@@ -9,15 +10,20 @@ class Generator:
return MAPS_DIR, CONFIG_DIR
def generate(self, redirects_data, project_name):
redirects_map = self.map_gen.generate_map(redirects_data[0], project_name)
def generate(self, redirects_data, project_name, arg_map = None):
if len(ConfigReader.parse_yaml(CONFIG)[0][1]) > len("/"):
prefix = ConfigReader.parse_yaml(CONFIG)[0][1].split("/")[-1]
redirects_map = self.map_gen.generate_map(redirects_data[0], "/{}".format(project_name))
else:
prefix = arg_map.split("/")[-2]
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])
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()
try:
with open("{}/{}.map".format(maps_dir,project_name), "w") as map_file:
with open("{}/{}.map".format(maps_dir,prefix), "w") as map_file:
map_file.write(redirects_map)
for code, data in redirects_with_options:
if code == "301":
......@@ -26,7 +32,7 @@ class Generator:
code = "redirect"
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" % project_name, "w") as conf_file:
with open(config_dir + "/%s.conf" % prefix, "w") as conf_file:
conf_file.write(conf_data)
except Exception as e:
print(e)
......@@ -51,7 +57,7 @@ class MapGenerator:
data = self.start_line % project_name
for arg1, arg2 in redirects:
# print('redirect is', arg1, arg2)
data += "\n\t/%s\t/%s;" % (project_name+arg1, project_name+arg2)
data += "\n\t%s\t%s;" % (project_name+arg1, project_name+arg2)
data += self.endline
return data
......@@ -75,12 +81,15 @@ class ConfigGenerator:
def __init__(self):
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.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_config(self, project_name, options):
data = (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name, "redirect")) + "}\n"
def generate_config(self, project_name, options, prefix):
if len(ConfigReader.parse_yaml(CONFIG)[0][1]) > len("/"):
data = (self.start_line % project_name) + (self.rewrite_line % ("/{}".format(prefix), project_name, "redirect")) + "}\n"
else:
data = (self.start_line % project_name) + (self.rewrite_line % ("", project_name, "redirect")) + "}\n"
for code in options:
if code == "permanent":
......
......@@ -123,8 +123,8 @@ class ConfigReader:
for map_path, prefix 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
#if map_file_name not in abs_map_path:
# continue
res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name
......
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