Updated RedirectorWatch class and watch() function

parent 81010cad
...@@ -21,7 +21,7 @@ class MapGenerator: ...@@ -21,7 +21,7 @@ class MapGenerator:
class ConfigGenerator: class ConfigGenerator:
def __init__(self): def __init__(self):
self.start_line = "if($%s-redirect) {\n" self.start_line = "if($%s-redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.^)$ $%s redirect;\n}" self.rewrite_line = "\trewrite ^/%s/(.^)$ $%s-redirect redirect;\n}"
def generate_conf(self, project_name): def generate_conf(self, project_name):
return (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name)) return (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name))
......
import yaml import os
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import re import re
...@@ -83,7 +88,7 @@ class ConfigReader: ...@@ -83,7 +88,7 @@ class ConfigReader:
def parse_yaml(file_dir): def parse_yaml(file_dir):
return_list = [] return_list = []
with open(file_dir, 'r') as stream: with open(file_dir, 'r') as stream:
data = yaml.safe_load(stream) data = load(stream, Loader=Loader)
for project in data.get("projects"): for project in data.get("projects"):
map_path = project.get("map") map_path = project.get("map")
project_prefix = project.get("prefix") project_prefix = project.get("prefix")
...@@ -95,7 +100,7 @@ class ConfigReader: ...@@ -95,7 +100,7 @@ class ConfigReader:
res_prefix = None res_prefix = None
try: try:
for map_path, prefix in self.parse_yaml(yaml_file): for map_path, prefix in self.parse_yaml(yaml_file):
if map_path == map_file: if map_path == map_file or os.getcwd() + "/" + map_path == map_file:
res_prefix = prefix.split("/")[-1] # ???: Is the last directory of map_path a project's name? res_prefix = prefix.split("/")[-1] # ???: Is the last directory of map_path a project's name?
with open(map_file, "r") as file: with open(map_file, "r") as file:
for i, line in enumerate(file): for i, line in enumerate(file):
......
import sys import sys
import os
import pyinotify import pyinotify
import asyncore
from core import generators from core import generators
from core import logger from core import logger
...@@ -17,7 +19,8 @@ class Redirector: ...@@ -17,7 +19,8 @@ class Redirector:
project_name = "Error" project_name = "Error"
try: try:
data, project_name = self.parser.parse_map(map_file, yaml_file) data, project_name = self.parser.parse_map(map_file, yaml_file)
if not project_name:
raise self.RedirectorInputDataError("Can\'t properly parse .map or .yaml files", Exception())
# FIXME: what is the better way to do so? # FIXME: what is the better way to do so?
except Exception as e: except Exception as e:
raise self.RedirectorParserError("Can\'t parse .map file %s" % map_file, e) raise self.RedirectorParserError("Can\'t parse .map file %s" % map_file, e)
...@@ -40,40 +43,44 @@ class Redirector: ...@@ -40,40 +43,44 @@ class Redirector:
def __init__(self, message, errors): def __init__(self, message, errors):
super().__init__(message) super().__init__(message)
self.errors = errors self.errors = errors
class RedirectorInputDataError(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
"""
Need to take care of unusual behavior of server - lagging, reboots, etc -> need some kind of saving file
"""
class RedirectorWatch: class RedirectorWatch:
def __init__(self): def __init__(self):
self.logger = logger.Logger("redirector-watch.log") self.logger = logger.Logger("redirector-watch.log")
self.redirector = Redirector() self.redirector = Redirector()
self.mask = pyinotify.IN_MODIFY self.parser = parser.ConfigReader(logger=self.logger)
watch_manager = pyinotify.WatchManager() self.mask = pyinotify.IN_MODIFY | pyinotify.IN_DELETE
self.notifier = pyinotify.AsyncioNotifier(watch_manager, self.Watch()) self.watch_manager = pyinotify.WatchManager()
@staticmethod
def parse_many(filename): def watch(self, yaml_file):
with open(filename, "r") as file_: class Watch(pyinotify.ProcessEvent):
return (line.split() for line in file_.readlines()) def __init__(self, *args, **kwargs):
self.redirector = kwargs.pop("redirector")
def add_files(self, map_file, yaml_file, filename=None): super().__init__(*args, **kwargs)
if filename:
for map_, yaml_ in filename: def process_IN_MODIFY(self, event):
pass self.redirector.generate(yaml_file, event.pathname)
else:
pass self.notifier = pyinotify.AsyncNotifier(self.watch_manager, Watch(redirector=self.redirector))
try: file_list = self.parser.parse_yaml(yaml_file)
self.notifier.loop(daemonize=True, callback=self.watch, for map_path, _ in file_list:
pid_file='/tmp/pyinotify.pid', stdout='/tmp/pyinotify.log') cwd = os.getcwd()
except pyinotify.NotifierError as err: wdd = self.watch_manager.add_watch(cwd + "/" + map_path, self.mask, rec=True)
self.logger.log(err) asyncore.loop()
@staticmethod
def watch(*args):
print(args)
def main(args=None): def main(args=None):
if not args: if not args:
args = sys.argv[1:] args = sys.argv[1:]
redirector = Redirector() redirector = Redirector()
try: try:
...@@ -86,4 +93,7 @@ def watch(args=None): ...@@ -86,4 +93,7 @@ def watch(args=None):
if not args: if not args:
args = sys.argv[1:] args = sys.argv[1:]
print(args) print(args)
watch_ = RedirectorWatch()
watch_.watch(args)
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