Separate files for watcher and main script

parent acd0d7e1
......@@ -9,8 +9,6 @@ import logger
import parser
import const
redirector_watch = None
class Redirector:
def __init__(self, logger=None):
self.parser = parser.ConfigReader(logger=logger)
......@@ -38,65 +36,6 @@ class Redirector:
self.errors = errors
class RedirectorWatch:
def __init__(self):
self.logger = logger.Logger("redirector-watch.log")
self.redirector = Redirector()
self.parser = parser.ConfigReader(logger=self.logger)
self.mask = pyinotify.IN_MODIFY
self.pool = {}
def watch(self, yaml_file):
if not yaml_file:
raise self.RedirectorWatchException("yaml_file list is empty", Exception)
Watch = self.create_watch_class(yaml_file)
for map_path, _ in self.parser.parse_yaml(yaml_file):
self.redirector.generate(yaml_file, map_path)
try:
self.set_inotify(yaml_file, Watch)
except pyinotify.PyinotifyError as e:
raise self.RedirectorWatchException("Can\'t set inotify for yamll file %s" % yaml_file, e)
# creating class Watch with custom static 'yaml_file' argument
def create_watch_class(self, yaml_file):
global redirector_watch
class Watch(pyinotify.ProcessEvent):
def __init__(self, *args, **kwargs):
self.redirector = kwargs.pop("redirector")
super().__init__(*args, **kwargs)
def process_IN_MODIFY(self, event):
try:
redirector_watch.pool[yaml_file][1].stop()
except RuntimeError as e:
print("hello there")
redirector_watch.pool[yaml_file] = None
print("Proccessing...")
self.redirector.generate(yaml_file, event.pathname)
redirector_watch.watch(yaml_file)
return Watch
# we suggest that self.watch_manager already exists
def set_inotify(self, yaml_file, Watch_class):
wm = pyinotify.WatchManager()
self.add_files(yaml_file, wm)
notifier = pyinotify.ThreadedNotifier(wm, Watch_class(redirector=self.redirector))
notifier.start()
self.pool[yaml_file] = [wm, notifier]
def add_files(self, file_, wm):
file_list = self.parser.parse_yaml(file_)
for map_path, _ in file_list:
cwd = os.getcwd()
wdd = wm.add_watch(cwd + "/" + map_path, self.mask, rec=True)
class RedirectorWatchException(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
def main(args=None):
parser = ap.ArgumentParser(description='Redirector: nginx config generator')
parser.add_argument('yaml_file', metavar='Y', type=str, nargs=1, help='.yaml file that defines project and it\'s content')
......@@ -111,18 +50,6 @@ def main(args=None):
print("CRITICAL:\n" + str(e))
def watch(args=None):
parser = ap.ArgumentParser(description='Redirector watchdog utility')
parser.add_argument('filename', metavar='Y', type=str, nargs=1,
help='Sets watch on the content of .yaml file')
if not args:
args = parser.parse_args()
global redirector_watch
redirector_watch = RedirectorWatch()
redirector_watch.watch(args.filename[0])
if __name__ == "__main__":
main()
import sys
import os
import pyinotify
import asyncore
import argparse as ap
import generators
import logger
import parser
import const
redirector_watch = None
class RedirectorWatch:
def __init__(self):
self.logger = logger.Logger("redirector-watch.log")
self.redirector = Redirector()
self.parser = parser.ConfigReader(logger=self.logger)
self.mask = pyinotify.IN_MODIFY
self.pool = {}
def watch(self, yaml_file):
if not yaml_file:
raise self.RedirectorWatchException("yaml_file list is empty", Exception)
Watch = self.create_watch_class(yaml_file)
for map_path, _ in self.parser.parse_yaml(yaml_file):
self.redirector.generate(yaml_file, map_path)
try:
self.set_inotify(yaml_file, Watch)
except pyinotify.PyinotifyError as e:
raise self.RedirectorWatchException("Can\'t set inotify for yamll file %s" % yaml_file, e)
# creating class Watch with custom static 'yaml_file' argument
def create_watch_class(self, yaml_file):
global redirector_watch
class Watch(pyinotify.ProcessEvent):
def __init__(self, *args, **kwargs):
self.redirector = kwargs.pop("redirector")
super().__init__(*args, **kwargs)
def process_IN_MODIFY(self, event):
try:
redirector_watch.pool[yaml_file][1].stop()
except RuntimeError as e:
print("hello there")
redirector_watch.pool[yaml_file] = None
print("Proccessing...")
self.redirector.generate(yaml_file, event.pathname)
redirector_watch.watch(yaml_file)
return Watch
# we suggest that self.watch_manager already exists
def set_inotify(self, yaml_file, Watch_class):
wm = pyinotify.WatchManager()
self.add_files(yaml_file, wm)
notifier = pyinotify.ThreadedNotifier(wm, Watch_class(redirector=self.redirector))
notifier.start()
self.pool[yaml_file] = [wm, notifier]
def add_files(self, file_, wm):
file_list = self.parser.parse_yaml(file_)
for map_path, _ in file_list:
cwd = os.getcwd()
wdd = wm.add_watch(cwd + "/" + map_path, self.mask, rec=True)
class RedirectorWatchException(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
def watch(args=None):
parser = ap.ArgumentParser(description='Redirector watchdog utility')
parser.add_argument('filename', metavar='Y', type=str, nargs=1,
help='Sets watch on the content of .yaml file')
if not args:
args = parser.parse_args()
global redirector_watch
redirector_watch = RedirectorWatch()
redirector_watch.watch(args.filename[0])
if __name__ == "__main__":
watch()
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