# HG changeset patch # User drewp@bigasterisk.com # Date 1653968030 25200 # Node ID 0f921dd06887a91de03397bd2166221919609222 # Parent 05492457f04b36f1924af82364306d7d233d32d0 (rough) remove WatchFiles in favor of watched_graphs.py diff -r 05492457f04b -r 0f921dd06887 rdfdb/service.py --- a/rdfdb/service.py Mon May 30 20:32:08 2022 -0700 +++ b/rdfdb/service.py Mon May 30 20:33:50 2022 -0700 @@ -53,124 +53,6 @@ CtxPrefixes = Dict[Optional[URIRef], Dict[str, URIRef]] -class WatchedFiles(object): - """ - find files, notice new files. - - This object watches directories. Each GraphFile watches its own file. - """ - - def __init__(self, dirUriMap: DirUriMap, patch: PatchCb, getSubgraph: GetSubgraph, addlPrefixes: CtxPrefixes): - self.dirUriMap = dirUriMap # {abspath : uri prefix} - self.patch, self.getSubgraph = patch, getSubgraph - self.addlPrefixes = addlPrefixes - - self.graphFiles: Dict[URIRef, GraphFile] = {} # context uri : GraphFile - - self.notifier = INotify() - self.notifier.startReading() - - self.findAndLoadFiles() - - def findAndLoadFiles(self) -> None: - self.initialLoad = True - try: - for topdir in self.dirUriMap: - for dirpath, dirnames, filenames in os.walk(topdir): - dirpath = Path(dirpath) - for base in filenames: - p = dirpath / base - # why wasn't mypy catching this? - self.watchFile(p) - self.notifier.watch(FilePath(dirpath), autoAdd=True, callbacks=[self.dirChange]) - finally: - self.initialLoad = False - - def dirChange(self, watch, path: FilePath, mask): - if mask & IN_CREATE: - if cast(str, path.path).endswith(('~', '.swp', 'swx', '.rdfdb-temp')): - return - - log.debug("%s created; consider adding a watch", path) - self.watchFile(Path(cast(str, path.path))) - - def watchFile(self, inFile: Path): - """ - consider adding a GraphFile to self.graphFiles - - inFile needs to be a relative path, not an absolute (e.g. in a - FilePath) because we use its exact relative form in the - context URI - """ - if not os.path.isfile(inFile): - return - - inFile = correctToTopdirPrefix(self.dirUriMap, inFile) - if os.path.splitext(inFile)[1] not in ['.n3']: - return - - if '/capture/' in str(inFile): - # smaller graph for now - return - - # an n3 file with rules makes it all the way past this reading - # and the serialization. Then, on the receiving side, a - # SyncedGraph calls graphFromNQuad on the incoming data and - # has a parse error. I'm not sure where this should be fixed - # yet. - if '-rules' in str(inFile): - return - - # for legacy versions, compile all the config stuff you want - # read into one file called config.n3. New versions won't read - # it. - if inFile.name == "config.n3": - return - - ctx = uriFromFile(self.dirUriMap, inFile) - gf = self._addGraphFile(ctx, inFile) - log.info("%s do initial read", inFile) - gf.reread() - - def aboutToPatch(self, ctx: URIRef): - """ - warn us that a patch is about to come to this context. it's more - straightforward to create the new file now - - this is meant to make the file before we add triples, so we - wouldn't see the blank file and lose those triples. But it - didn't work, so there are other measures that make us not lose - the triples from a new file. Calling this before patching the - graph is still a reasonable thing to do, though. - """ - if ctx not in self.graphFiles: - outFile = fileForUri(self.dirUriMap, ctx) - assert '//' not in str(outFile), (outFile, self.dirUriMap, ctx) - log.info("starting new file %r", outFile) - self._addGraphFile(ctx, outFile) - - def _addGraphFile(self, ctx: URIRef, path: Path): - self.addlPrefixes.setdefault(ctx, {}) - self.addlPrefixes.setdefault(None, {}) - gf = GraphFile(self.notifier, path, ctx, self.patch, self.getSubgraph, globalPrefixes=self.addlPrefixes[None], ctxPrefixes=self.addlPrefixes[ctx]) - self.graphFiles[ctx] = gf - # fileStats.mappedGraphFiles = len(self.graphFiles) - return gf - - def dirtyFiles(self, ctxs): - """mark dirty the files that we watch in these contexts. - - the ctx might not be a file that we already read; it might be - for a new file we have to create, or it might be for a - transient context that we're not going to save - - if it's a ctx with no file, error - """ - for ctx in ctxs: - g = self.getSubgraph(ctx) - self.graphFiles[ctx].dirty(g) - - _wsClientSerial = itertools.count(0)