#!/usr/bin/env python3 from xmlrpc.client import ServerProxy import ssl import os import sys import pyinotify import json import syslog import codecs from pathlib import Path class EventHandler(pyinotify.ProcessEvent): def my_init(self, n4d, masterkey): self.n4d = n4d self.masterkey = masterkey def process_IN_CLOSE_WRITE(self, event): self.saveConfig(Path(event.pathname)) def saveConfig(self,identifier): config = {} try : with codecs.open(identifier,'r',encoding='utf-8') as fd: config = json.load(fd) filetype = "config" if "output" in str(identifier): filetype = "outputs" if "control" in str(identifier): filetype = "control" result = self.n4d.updateResolution('','MonitorSettings',config, identifier.name, self.masterkey, filetype) if result['status']: self.masterkey = result['msg'] with codecs.open(masterkeypath,'w',encoding='utf-8') as fd: fd.write(self.masterkey) except FileNotFoundError: pass except Exception as e: syslog.syslog(str(e)) if __name__ == "__main__": home = os.environ['HOME'] masterkeypath = Path('{home}/.config/kscreensystem'.format(home=home)) if not os.path.exists(masterkeypath): sys.exit(0) n4d = ServerProxy('https://localhost:9779', context=ssl._create_unverified_context(), allow_none=True) result = n4d.get_variable("","VariablesManager","SRV_IP") if result != None: n4d = ServerProxy('https://{server}:9779'.format(server=result),context=ssl._create_unverified_context(),allow_none=True) with masterkeypath.open('r',encoding='utf-8') as fd: masterkey = fd.readline().strip() wm = pyinotify.WatchManager() handler = EventHandler(n4d=n4d, masterkey=masterkey) notifier = pyinotify.Notifier(wm, default_proc_fun=handler) wm.add_watch('{home}/.local/share/kscreen'.format(home=home), pyinotify.IN_CLOSE_WRITE, rec=True, auto_add=True) notifier.loop()