def tail(file, manager, tail=False): #notifier = inotify.adapters.Inotify() while True: try: # File might not be present on rotation or to be created if not os.path.exists(file): warn('File {} does not exist.'.format(file)) time.sleep(1) continue #notifier.add_watch(logfile) with opened_w_error(file, "r") as (f, err): if err: error("IOError: + {}".format(err)) else: if tail: f.seek(0, 2) tail = False for line in f.readlines(): # Sends the message to all registered plugs manager.send_to_plugs(parse_line(line)) #except inotify.calls.InotifyError: # sleep(1) # Avoid errors on interrupt except (KeyboardInterrupt, SystemExit): manager.stop_plugins() break
def _get(self): """ Read data from the file. """ with self.__class__._Locks[self.id]: try: with opened_w_error(self._infos_path(), "r") as f: return json.load(f) except IOError as e: raise ObjectNotFound(data_object=self, message="Can't open the file", original_message=e) except ValueError as e: raise ObjectMalformed(data_object=self, message="Invalid Json Document", original_message=e)
def update(self): self._check_valid_dict() """Save the new data in the data file""" with self.__class__._Locks[self.id]: try: with opened_w_error(self._infos_path(), "w") as f: # The id information is already stored in the file path. d = {k: value for k, value in self.infos.items() if k != "id"} json.dump(d, f, indent=4) except IOError as e: raise ObjectNotFound(data_object=self, message="Can't save data.", original_message=e) except (ValueError, TypeError) as e: raise ObjectMalformed(data_object=self, message="Can't serialize data.", original_message=e) # Also update the cache of self.__get self._get.__func__.cache[(self,)] = self.infos