Mercurial > code > home > repos > homeauto
changeset 1647:34eb87f68ab8
WIP rules reloader (doesn't reload yet)
author | drewp@bigasterisk.com |
---|---|
date | Fri, 17 Sep 2021 11:07:21 -0700 |
parents | af136cf6dd26 |
children | 3059f31b2dfa |
files | service/mqtt_to_rdf/mqtt_to_rdf.py |
diffstat | 1 files changed, 37 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/service/mqtt_to_rdf/mqtt_to_rdf.py Fri Sep 17 11:06:52 2021 -0700 +++ b/service/mqtt_to_rdf/mqtt_to_rdf.py Fri Sep 17 11:07:21 2021 -0700 @@ -347,6 +347,33 @@ self.loop.stop() +@dataclass +class WatchFiles: + # could be merged with rdfdb.service and GraphFile code + globPattern: str + outGraph: Graph + + def __post_init__(self): + self.lastUpdate = 0 + task.LoopingCall(self.refresh).start(1) + log.info(f'start watching {self.globPattern}') + + def refresh(self): + files = glob.glob(self.globPattern) + for fn in files: + if os.path.getmtime(fn) > self.lastUpdate: + break + else: + return + self.lastUpdate = time.time() + self.outGraph.remove((None, None, None)) + log.info('reread config') + for fn in files: + # todo: handle read errors + self.outGraph.parse(fn, format='n3') + # and notify this change,so we can recalc the latest output + + if __name__ == '__main__': arg = docopt(""" Usage: mqtt_to_rdf.py [options] @@ -358,15 +385,18 @@ logging.getLogger('mqtt').setLevel(logging.INFO) logging.getLogger('mqtt_client').setLevel(logging.INFO) logging.getLogger('infer').setLevel(logging.INFO) + logging.getLogger('cbind').setLevel(logging.INFO) + # log.setLevel(logging.DEBUG) log.info('log start') - config = Graph() - for fn in Path('.').glob('conf/*.n3'): - if not arg['--cs'] or str(arg['--cs']) in str(fn): - log.debug(f'loading {fn}') - config.parse(str(fn), format='n3') - else: - log.debug(f'skipping {fn}') + config = ConjunctiveGraph() + watcher = WatchFiles('conf/rules.n3', config) + # for fn in Path('.').glob('conf/*.n3'): + # if not arg['--cs'] or str(arg['--cs']) in str(fn): + # log.debug(f'loading {fn}') + # config.parse(str(fn), format='n3') + # else: + # log.debug(f'skipping {fn}') masterGraph = PatchableGraph()