#!/usr/bin/env python3 import os import sys import pyinotify import json import syslog import codecs import n4d.client 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 identifier.open('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.MonitorSettings.updateResolution( 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 masterkeypath.exists(): sys.exit(0) client = n4d.client.Client('https://localhost:9779') try: result = client.get_variable("SRV_IP") if result is not None: client = n4d.client.Client('https://{server}:9779'.format(server=result)) except: pass with masterkeypath.open('r',encoding='utf-8') as fd: masterkey = fd.readline().strip() wm = pyinotify.WatchManager() handler = EventHandler( n4d=client, 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()