Refactoring, add test_suite to setup.py

parent ffa0e145
include redirector/conf/config.ini
include redirector/tests/*.ini
include redirector/tests/*.map
include redirector/tests/*.yaml
include LICENSE
include README.md
import sys
import os
import pyinotify
import asyncore
import argparse as ap
from redirector.utils import generators
......
NAME = "nginx-redirector"
VERSION = "1.0"
AUTHOR = "Nikita Yefremov"
EMAIL = "enk@etersoft.ru"
PYTHON_VERSION = "3.6"
VERSION = "1.0.0"
AUTHOR = "Nikita Yefremov, David Dobryakov"
EMAIL = "kantegory@etersoft.ru"
PYTHON_VERSION = "3.8.1"
VERSION_STATUS = "alpha"
......@@ -19,8 +19,6 @@ class Generator:
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])
#config = Config()
#config.read_config()
maps_dir, config_dir = self.get_conf_dirs()
......
......@@ -5,6 +5,7 @@ try:
except ImportError:
from yaml import Loader, Dumper
import re
from redirector.utils.utils import get_map_path
class MapLineParser:
......@@ -96,16 +97,20 @@ class ConfigReader:
return_list = []
yaml_dir = os.path.dirname(os.path.abspath(file_dir))
with open(file_dir, 'r') as stream:
data = load(stream, Loader=Loader)
for project in data.get("projects"):
map_path = project.get("map")
rel_map_path = os.path.relpath(map_path, start=yaml_dir)
abs_map_path = os.path.join(rel_map_path, yaml_dir, os.path.basename(map_path))
abs_map_path = os.path.normpath(abs_map_path)
stream = open(file_dir, 'r')
data = load(stream, Loader=Loader)
for project in data.get("projects"):
map_path = project.get("map")
abs_map_path = get_map_path(map_path, yaml_dir)
project_prefix = project.get("prefix")
return_list.append((abs_map_path, project_prefix))
stream.close()
project_prefix = project.get("prefix")
return_list.append((abs_map_path, project_prefix))
return return_list
def parse_map(self, map_file, yaml_file):
......@@ -118,36 +123,40 @@ class ConfigReader:
try:
for map_path, prefix in self.parse_yaml(yaml_file):
rel_map_path = os.path.relpath(map_path, start=yaml_dir) # restore rel path for map file
abs_map_path = os.path.join(rel_map_path, yaml_dir, os.path.basename(map_path)) # join rel path with yaml dir and map filename
abs_map_path = os.path.normpath(abs_map_path) # normalize path for removing "../", "./" and etc.
if map_file_name in abs_map_path:
res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name
with open(abs_map_path, "r") as file:
for i, line in enumerate(file):
request_url, redirect_url, option = None, None, []
try:
return_code, request_url, redirect_url, *option = \
self.line_parser.parse_line(line, prefix, i)
except MapLineParser.RegexpTestError as e:
self.logger.log(e.message % map_file)
except MapLineParser.ParseLineError as e:
self.logger.log(e.message % map_file)
if not request_url or not redirect_url or return_code == -1:
continue
abs_map_path = get_map_path(map_path, yaml_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(abs_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)
except MapLineParser.RegexpTestError as e:
self.logger.log(e.message % map_file)
except MapLineParser.ParseLineError as e:
self.logger.log(e.message % map_file)
if request_url and redirect_url and return_code in [0, 1]:
if return_code == 0:
res[0].append((request_url, redirect_url))
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])
else:
if return_code == 0:
res[0].append((request_url, redirect_url))
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])
else:
res[1][opt_] = [(request_url, redirect_url)]
res[1][opt_] = [(request_url, redirect_url)]
f.close()
return res, res_prefix
except YAMLError as e:
self.logger.log("Error occurred while reading %s" % yaml_file + str(e))
import os
def get_map_path(filename, dirname):
rel_map_path = os.path.relpath(filename, start=dirname)
abs_map_path = os.path.join(rel_map_path, dirname, os.path.basename(filename))
abs_map_path = os.path.normpath(abs_map_path)
return abs_map_path
import sys
import os
import pyinotify
import asyncore
import argparse as ap
from redirector.utils import generators
......@@ -104,6 +102,6 @@ def watch(args=None):
redirector_watch.watch(args.filename[0])
#if __name__ == "__main__":
# watch()
if __name__ == "__main__":
watch()
......@@ -25,7 +25,7 @@ setup(
long_description=open('README.md').read(),
author="Nikita Efremov, Dobryakov David",
author_email="kantegory@etersoft.ru",
url="https://gitlab.eterfund.ru/eterfund/nginx-redirector/tree/dev-enhancement",
url="https://gitlab.eterfund.ru/eterfund/nginx-redirector/",
license="ISC",
packages=['redirector', 'redirector.utils', 'redirector.tests', 'redirector.conf'],
entry_points={
......@@ -41,6 +41,7 @@ setup(
'pyinotify',
'pyyaml'
],
include_package_data=True
include_package_data=True,
test_suite="redirector.tests"
)
projects:
- map: ./test2.map
prefix: /test3
- map: ./test.map
prefix: /test
- map: /home/kantegory/pvt/nginx-redirector/tests/test2.map
prefix: /test2
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