def run(self): if not is_complete(self.directory): observer = LocalReportObserver(self, self.directory, self.config) try: while not self.task_done: time.sleep(1) except (KeyboardInterrupt, SystemExit): observer.stop() observer.join() self.join() observer.stop() observer.join() self.file_watchers[self.directory] = None log('local: thread task finished') else: # Directory is complete, no need to spawn watcher while not self.task_done: # dummy url = 'https://cerebro-dev.herokuapp.com/api/v1/authenticate/report/b6b56c1090487819f0938f8de57cdfdf1ed70c72/' payload = { 'date': '{:%Y-%m-%dT%H:%M:%SZ}'.format(datetime.utcnow()), 'action': 'ALLOWED', 'door': 'Door 1A', 'details': 'Some description', 'wiegand_id': '0293204', 'user_enrollment_id': '1' } self.task_done = True
def __init__(self, directory: str, file_watchers, config: dict): super().__init__() self.file_watchers = file_watchers self.config = config self.directory = directory self.task_done = False log('local: attaching observer to {}'.format(directory))
def main(): directory = 'report/a101' log('local: watching on folder {}'.format(directory)) config = {} observer_thread = LocalObserverThread(directory, config, {}) config[directory] = observer_thread observer_thread.start() observer_thread.join()
def main(): directory = 'report' log('global: watching on folder {}'.format(directory)) observer_thread = GlobalObserverThread(directory, {}) try: observer_thread.start() except (KeyboardInterrupt, SystemExit): observer_thread.observer.stop() observer_thread.join()
def on_created(self, event): filename = event.src_path.split('/')[-1] folder = event.src_path.replace(filename, '') log('global: {0} file {2} at {1}'.format(event.event_type, folder, filename)) if folder not in self.file_watchers: self.file_watchers[folder] = LocalObserverThread( folder, self.file_watchers, self.config) self.file_watchers[folder].start()
def on_created(self, event): filename = event.src_path.split('/')[-1] folder = event.src_path.replace(filename, '') log('local: {0} file {2} at {1}'.format(event.event_type, folder, filename)) # Perform check whether this folder is complete or not if self.is_file_complete(): multipart = self.get_multipart() # Notify thread success self.thread.task_done = True log('local: finishing thread task ...')
def __init__(self, thread: LocalObserverThread, directory, config): # Properties self.directory = directory self.config = config self.thread = thread # Make folder path = Path(directory) if not path.exists(): path.mkdir(parents=True, exist_ok=True) log('local: directory {} created'.format(path)) # Make observer self.observer = Observer() self.observer.schedule(LocalReportHandler(self.thread, self.directory, config), path=directory if directory else '.', recursive=True) self.observer.start()
def stop(self): self.observer.stop() log('global: watchdog service for folder {} stopped'.format( self.directory))
def send_files(thread: LocalObserverThread, url, headers, multipart, payload): log('Sending files to {0}'.format(url)) return requests.post(url, headers=headers, data=payload, files=multipart)