changeset 87:a96f4eb95ef0

more typing and formatting
author drewp@bigasterisk.com
date Mon, 04 Apr 2022 23:01:08 -0700
parents 5b6e90a708ce
children 219d9e89b15c
files rdfdb/patch.py rdfdb/service.py rdfdb/syncedgraph.py
diffstat 3 files changed, 38 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/patch.py	Mon Apr 04 23:00:42 2022 -0700
+++ b/rdfdb/patch.py	Mon Apr 04 23:01:08 2022 -0700
@@ -1,13 +1,12 @@
 import json
 from typing import Optional
 
-from rdflib import ConjunctiveGraph, Graph, Literal, Namespace
-from rdflib import URIRef
+from rdflib import ConjunctiveGraph, Graph, Literal, Namespace, URIRef
+
+from rdfdb.rdflibpatch import graphFromNQuad, graphFromQuads, serializeQuad
 
 XSD = Namespace("http://www.w3.org/2001/XMLSchema#")
 
-from rdfdb.rdflibpatch import graphFromNQuad, graphFromQuads, serializeQuad
-
 ALLSTMTS = (None, None, None)
 
 
@@ -29,7 +28,7 @@
 class Patch(object):
     """
     immutable
-    
+
     the json representation includes the {"patch":...} wrapper
     """
 
@@ -183,4 +182,3 @@
 
     def isNoop(self):
         return set(self.addQuads) == set(self.delQuads)
-
--- a/rdfdb/service.py	Mon Apr 04 23:00:42 2022 -0700
+++ b/rdfdb/service.py	Mon Apr 04 23:01:08 2022 -0700
@@ -4,22 +4,23 @@
 import optparse
 import os
 import sys
-import time
-from typing import Dict, List, Optional
+from typing import Dict, List, Optional, cast
 
 import cyclone.web
 import cyclone.websocket
 import twisted.internet.error
 from rdflib import ConjunctiveGraph, Graph, URIRef
-from twisted.internet import reactor, task
+import twisted.internet.reactor
 from twisted.internet.inotify import IN_CREATE, INotify
 from twisted.python.failure import Failure
 from twisted.python.filepath import FilePath
+from twisted.internet.interfaces import IReactorCore
 
 from rdfdb.file_vs_uri import (DirUriMap, correctToTopdirPrefix, fileForUri, uriFromFile)
 from rdfdb.graphfile import GetSubgraph, GraphFile, PatchCb
 from rdfdb.patch import ALLSTMTS, Patch
 from rdfdb.rdflibpatch import patchQuads
+reactor = cast(IReactorCore, twisted.internet.reactor)
 
 # gatherProcessStats()
 # stats = scales.collection(
@@ -358,20 +359,21 @@
 
     reactor.listenTCP(
         port,
-        cyclone.web.Application(handlers=[
-            (r'/graph', GraphResource),
-            (r'/syncedGraph', WebsocketClient),
-            (r'/prefixes', Prefixes),
-            # (r'/stats/(.*)', StatsHandler, {
-            #     'serverName': 'rdfdb'
-            # }),
-            (r'/(.*)', NoExts, {
-                "path": FilePath(__file__).sibling("web").path,
-                "default_filename": "index.html"
-            }),
-        ],
-                                debug=True,
-                                db=db))
+        cyclone.web.Application(
+            handlers=[
+                (r'/graph', GraphResource),
+                (r'/syncedGraph', WebsocketClient),
+                (r'/prefixes', Prefixes),
+                # (r'/stats/(.*)', StatsHandler, {
+                #     'serverName': 'rdfdb'
+                # }),
+                (r'/(.*)', NoExts, {
+                    "path": FilePath(__file__).sibling("web").path,
+                    "default_filename": "index.html"
+                }),
+            ],
+            debug=True,
+            db=db))
     log.info("serving on %s" % port)
     reactor.run()
 
--- a/rdfdb/syncedgraph.py	Mon Apr 04 23:00:42 2022 -0700
+++ b/rdfdb/syncedgraph.py	Mon Apr 04 23:01:08 2022 -0700
@@ -18,12 +18,14 @@
 import logging
 import traceback
 import urllib.parse
-from typing import Optional
+from typing import Any, Optional, cast
 
 import autobahn.twisted.websocket
 import treq
+import twisted.internet.reactor
 from rdflib import ConjunctiveGraph, URIRef
-from twisted.internet import defer, reactor
+from twisted.internet import defer
+from twisted.internet.interfaces import IReactorCore
 
 from rdfdb.autodepgraphapi import AutoDepGraphApi
 from rdfdb.currentstategraphapi import CurrentStateGraphApi
@@ -34,6 +36,7 @@
 from rdfdb.rdflibpatch_literal import patch
 
 patch()
+reactor = cast(IReactorCore, twisted.internet.reactor)
 
 log = logging.getLogger('syncedgraph')
 
@@ -87,7 +90,7 @@
 class SyncedGraph(CurrentStateGraphApi, AutoDepGraphApi, GraphEditApi):
     """
     graph for clients to use. Changes are synced with the master graph
-    in the rdfdb process. 
+    in the rdfdb process.
 
     self.patch(p: Patch) is the only way to write to the graph.
 
@@ -139,11 +142,11 @@
             # Don't know if this is required by spec, but
             # cyclone.websocket breaks with no origin header.
             origin='foo')
-        factory.protocol = lambda: WsClientProtocol(self)
+        factory.protocol = cast(Any, lambda: WsClientProtocol(self))
 
         rr = urllib.parse.urlparse(self.rdfdbRoot)
-        conn = reactor.connectTCP(rr.hostname.encode('ascii'), rr.port, factory)
-        #WsClientProtocol sets our currentClient. Needs rewrite using agents.
+        reactor.connectTCP(rr.hostname, rr.port, factory)
+        # WsClientProtocol sets our currentClient. Needs rewrite using agents.
 
     def resync(self):
         """
@@ -160,14 +163,16 @@
         UIs who want to show that we're doing a resync.
         """
         log.info('resync')
-        self.currentClient.dropConnection()
+        if self.currentClient:
+            self.currentClient.dropConnection()
 
     def _resyncGraph(self, response):
         log.warn("new graph in")
 
-        self.currentClient.dropConnection()
-        #diff against old entire graph
-        #broadcast that change
+        if self.currentClient:
+            self.currentClient.dropConnection()
+        # diff against old entire graph
+        # broadcast that change
 
     def patch(self, p: Patch) -> None:
         """send this patch to the server and apply it to our local