|
| 1 | +''' |
| 2 | +############################################################################## |
| 3 | +# Copyright 2023 IBM Corp. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +############################################################################## |
| 17 | +
|
| 18 | +Created on Sep 22, 2023 |
| 19 | +
|
| 20 | +@author: HWASSMAN |
| 21 | +''' |
| 22 | + |
| 23 | +import os |
| 24 | +import time |
| 25 | +from bridgeLogger import getBridgeLogger |
| 26 | +from messages import MSG |
| 27 | + |
| 28 | + |
| 29 | +class ConfigWatcher(object): |
| 30 | + running = False |
| 31 | + refresh_delay_secs = 30 |
| 32 | + |
| 33 | + def __init__(self, watch_paths, call_func_on_change=None, *args, **kwargs): |
| 34 | + self._cached_stamp = {} |
| 35 | + self.logger = getBridgeLogger() |
| 36 | + self.paths = watch_paths |
| 37 | + self.filenames = set() |
| 38 | + self.call_func_on_change = call_func_on_change |
| 39 | + self.args = args |
| 40 | + self.kwargs = kwargs |
| 41 | + |
| 42 | + def update_files_list(self): |
| 43 | + oldfiles = self.filenames.copy() |
| 44 | + for path in self.paths: |
| 45 | + if os.path.isfile(path): |
| 46 | + self.filenames.add(path) |
| 47 | + elif os.path.isdir(path): |
| 48 | + for root, _, files in os.walk(path): |
| 49 | + for file in files: |
| 50 | + if file.endswith(".cfg"): |
| 51 | + self.filenames.add(os.path.join(root, file)) |
| 52 | + else: |
| 53 | + self.logger.trace(MSG['PathNoCfgFiles'].format(path)) |
| 54 | + for file in self.filenames.difference(oldfiles): |
| 55 | + self.logger.debug(MSG['FileAddedToWatch'].format(file)) |
| 56 | + |
| 57 | + def look(self): |
| 58 | + """ Function to check if a file timestamp has changed""" |
| 59 | + for filename in self.filenames: |
| 60 | + stamp = os.stat(filename).st_mtime |
| 61 | + if filename not in self._cached_stamp: |
| 62 | + self._cached_stamp[filename] = stamp |
| 63 | + elif stamp != self._cached_stamp[filename]: |
| 64 | + self._cached_stamp[filename] = stamp |
| 65 | + # File has changed, so do something... |
| 66 | + self.logger.info(MSG['FileChanged'].format(filename)) |
| 67 | + if self.call_func_on_change is not None: |
| 68 | + self.call_func_on_change(*self.args, **self.kwargs) |
| 69 | + |
| 70 | + def watch(self): |
| 71 | + """ Function to keep watching in a loop """ |
| 72 | + self.running = True |
| 73 | + self.logger.debug(MSG['StartWatchingFiles'].format(self.paths)) |
| 74 | + while self.running: |
| 75 | + try: |
| 76 | + # Look for changes |
| 77 | + time.sleep(self.refresh_delay_secs) |
| 78 | + self.update_files_list() |
| 79 | + self.look() |
| 80 | + except KeyboardInterrupt: |
| 81 | + self.logger.details(MSG['StopWatchingFiles'].format(self.paths)) |
| 82 | + break |
| 83 | + except FileNotFoundError as e: |
| 84 | + # Action on file not found |
| 85 | + self.logger.warning(MSG['FileNotFound'].format(e.filename)) |
| 86 | + pass |
| 87 | + except Exception as e: |
| 88 | + self.logger.warning(MSG['StopWatchingFiles'].format(self.paths)) |
| 89 | + self.logger.details(MSG['UnhandledError'].format(type(e).__name__)) |
| 90 | + break |
| 91 | + |
| 92 | + def stop_watch(self): |
| 93 | + """ Function to break watching """ |
| 94 | + self.running = False |
0 commit comments