Debugged project files and introduced Redirector in redirector.py file, updated…

Debugged project files and introduced Redirector in redirector.py file, updated setup.py console entry points
parent 405757dd
REDIRECTOR_DIR = "/var/lib/redirector"
MAPS_DIR = REDIRECTOR_DIR + "/maps"
CONFIG_DIR = REDIRECTOR_DIR + "/location-includes"
\ No newline at end of file
class MapGenerator: class MapGenerator:
def __init__(self, logger=None): def __init__(self):
self.logger = logger
self.start_lines = [ self.start_lines = [
"map $uri $%s-redirect {\n", "map $uri $%s-redirect {\n",
"\nmap $uri $%s-option-redirect {\n", "\nmap $uri $%s-option-redirect {\n",
......
...@@ -20,7 +20,7 @@ class RedirectorParser: ...@@ -20,7 +20,7 @@ class RedirectorParser:
if len(line) == 0: if len(line) == 0:
return -1, None, None return -1, None, None
elif len(line) < 2: elif len(line) < 2:
raise self.ParseLineError("Error on %s:{line};\nNot enough arguments to parse!".format(line=i)) raise self.ParseLineError("Error on %s:{line};\nNot enough arguments to parse!".format(line=i), None)
elif len(line) > 2: elif len(line) > 2:
return_code = 1 return_code = 1
options = [option[1:-1] for option in line[2:]] options = [option[1:-1] for option in line[2:]]
...@@ -32,7 +32,7 @@ class RedirectorParser: ...@@ -32,7 +32,7 @@ class RedirectorParser:
except re.error: except re.error:
raise self.RegexpTestError("Can\'t compile regular expressions {expression1} {expression2} in " raise self.RegexpTestError("Can\'t compile regular expressions {expression1} {expression2} in "
"%s:{line_num}".format(expression1=line[0], expression2=line[1], "%s:{line_num}".format(expression1=line[0], expression2=line[1],
line_num=i)) line_num=i), re.error)
# if new URI relative to the root # if new URI relative to the root
elif line[0].startswith("//"): elif line[0].startswith("//"):
line[0] = line[0][1:] # cutting out extra '/' at the beginning line[0] = line[0][1:] # cutting out extra '/' at the beginning
...@@ -70,7 +70,7 @@ class RedirectorParser: ...@@ -70,7 +70,7 @@ class RedirectorParser:
else: else:
print(level.upper() + ":\n" + message) print(level.upper() + ":\n" + message)
def _parse_map(self, map_file, yaml_file): def parse_map(self, map_file, yaml_file):
res = [[], [], []] res = [[], [], []]
res_prefix = None res_prefix = None
try: try:
...@@ -107,8 +107,11 @@ class RedirectorParser: ...@@ -107,8 +107,11 @@ class RedirectorParser:
message -- expanation where in .yaml config file aprser didn't find enough arguments message -- expanation where in .yaml config file aprser didn't find enough arguments
""" """
def __init__(self, message): def __init__(self, message, errors):
super().__init__(message)
self.message = message self.message = message
self.errors = errors
class RegexpTestError(Exception): class RegexpTestError(Exception):
"""Raised when parser fails to compile url containing regular expression """Raised when parser fails to compile url containing regular expression
...@@ -117,5 +120,7 @@ class RedirectorParser: ...@@ -117,5 +120,7 @@ class RedirectorParser:
message -- explanation what regexp failed to compile and where message -- explanation what regexp failed to compile and where
""" """
def __init__(self, message): def __init__(self, message, errors):
self.message = messages super().__init__(message)
self.message = message
self.errors = errors
import sys
import generators
import parser
import const
class Redirector:
def __init__(self, logger=None):
self.parser = parser.RedirectorParser(logger=logger)
self.map_generator = generators.MapGenerator()
self.config_generator = generators.ConfigGenerator()
def log(self, message, level="critical"):
if self.logger:
if level == "critical":
self.logger.critical(message)
elif level == "debug":
self.logger.debug(message)
elif level == "info":
self.logger.info(message)
else:
print(level.upper() + ":\n" + message)
def generate(self, map_file, yaml_file):
project_name = "Error"
try:
data, project_name = self.parser.parse_map(map_file, yaml_file)
# 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)
try:
with open(const.MAPS_DIR + "/%s.map" % project_name, "w") as map_file:
map_file.write(self.map_generator.generate_map(data, project_name))
with open(const.CONFIG_DIR + "/%s.conf" % project_name, "w") as conf_file:
conf_file.write(self.config_generator.generate_conf(project_name))
except Exception as e:
raise self.RedirectorGenerationError("Can\'t generate new .conf or new .map file \
from %s .map file for %s project" % (map_file, project_name). e)
class RedirectorParserError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class RedirectorGenerationError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class RedirectorWatch:
def __init__(self):
pass
def main(args=None):
if not args:
args = sys.argv[1:]
redirector = Redirector()
try:
redirector.generate(*args)
except (Redirector.RedirectorGenerationError, redirector.RedirectorParserError) as e:
print("CRITICAL:\n" + str(e))
def watch(args=None):
if not args:
args = sys.argv[1:]
print(args)
...@@ -7,6 +7,8 @@ setup( ...@@ -7,6 +7,8 @@ setup(
packages=find_packages(), packages=find_packages(),
entry_points=""" entry_points="""
[redirector] [redirector]
redirector = core.parser:main redirector = core.redirector:main
""" [redirector-watch]
redirector-watch = core.redirector:watch
"""
) )
\ No newline at end of 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