@ -1,7 +1,7 @@
#! /usr/bi n/env python3
# -*- coding: utf-8 -*-
"""
Copyright 2020 , Nils Hilbricht , Germany ( https : / / www . hilbricht . net )
Copyright 2021 , Nils Hilbricht , Germany ( https : / / www . hilbricht . net )
This file is part of the Laborejo Software Suite ( https : / / www . laborejo . org ) ,
@ -90,7 +90,7 @@ class Watcher(object):
self . _directories = fast_scandir ( self . _nsmServerControl . sessionRoot )
logger . info ( " Requestion our own copy of the session list. Don ' t worry about the apparent redundant call :) " )
self . _lastExport = self . _nsmServerControl . exportSessionsAsDicts ( )
self . _lastExport = self . _nsmServerControl . exportSessionsAsDicts ( ) #list of dicts
self . _lastTimestamp = { d [ " nsmSessionName " ] : d [ " lastSavedDate " ] for d in self . _lastExport } #str:str
#Init all values with None will send the initial state via callback on program start, which is what the user wants to know.
self . _lastLockfile = { d [ " nsmSessionName " ] : None for d in self . _lastExport } #str:bool
@ -111,6 +111,15 @@ class Watcher(object):
self . active = False
logger . info ( " Watcher suspended " )
def _update ( self ) :
current_directories = fast_scandir ( self . _nsmServerControl . sessionRoot )
if not self . _directories == current_directories :
self . _directories = current_directories
self . _lastExport = self . sessionsChangedHook ( ) #will gather its own data, send it to api callbacks, but also return for us.
self . _lastTimestamp = { d [ " nsmSessionName " ] : d [ " lastSavedDate " ] for d in self . _lastExport } #str:str
self . _lastLockfile = { d [ " nsmSessionName " ] : None for d in self . _lastExport } #str:bool
def process ( self ) :
""" Add this to your event loop.
@ -125,13 +134,7 @@ class Watcher(object):
return
if self . sessionsChangedHook :
current_directories = fast_scandir ( self . _nsmServerControl . sessionRoot )
if not self . _directories == current_directories :
self . _directories = current_directories
self . _lastExport = self . sessionsChangedHook ( ) #will gather its own data, send it to api callbacks, but also return for us.
self . _lastTimestamp = { d [ " nsmSessionName " ] : d [ " lastSavedDate " ] for d in self . _lastExport } #str:str
self . _lastLockfile = { d [ " nsmSessionName " ] : None for d in self . _lastExport } #str:bool
self . _update ( )
#Now check the incremental hooks.
#No hooks, no reason to process
@ -142,6 +145,7 @@ class Watcher(object):
for entry in self . _lastExport :
nsmSessionName = entry [ " nsmSessionName " ]
try :
#Timestamp of session.nsm
if self . timeStampHook :
timestamp = datetime . fromtimestamp ( entry [ " sessionFile " ] . stat ( ) . st_mtime ) . isoformat ( sep = " " , timespec = ' minutes ' ) #same format as server control export
@ -155,3 +159,15 @@ class Watcher(object):
if not self . _lastLockfile [ nsmSessionName ] == lockfileState :
self . _lastLockfile [ nsmSessionName ] = lockfileState
self . lockFileHook ( nsmSessionName , lockfileState )
except PermissionError :
logger . warning ( f " File Permission error for { entry } " )
self . _lastExport . remove ( entry ) #avoid stumbling upon this again
self . _update ( )
except FileNotFoundError :
logger . warning ( f " File not found error for { entry } " )
self . _lastExport . remove ( entry ) #avoid stumbling upon this again
self . _update ( )