Skip to content

Commit

Permalink
Attempting to provide more context for some PyOpenWorm connection fai…
Browse files Browse the repository at this point in the history
…lures (#370, #371)
  • Loading branch information
mwatts15 committed Aug 6, 2018
1 parent 5951e33 commit 477ae0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions PyOpenWorm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ def loadData(
config('rdf.graph').parse(data, format=dataFormat)


class ConnectionFailError(Exception):
def __init__(self, cause, *args):
if args:
super(ConnectionFailError, self).__init__('PyOpenWorm connection failed: {}. {}'.format(cause, *args))
else:
super(ConnectionFailError, self).__init__('PyOpenWorm connection failed: {}'.format(cause))


def connect(configFile=False,
conf=False,
do_logging=False,
Expand All @@ -207,7 +215,7 @@ def connect(configFile=False,
"""
import logging
import atexit
from .data import Data
from .data import Data, ZODBSourceOpenFailError
m = __import__('__main__')
if m.connected:
print ("PyOpenWorm already connected")
Expand All @@ -232,7 +240,17 @@ def connect(configFile=False,
})

Configureable.default = conf
conf.openDatabase()
try:
conf.init_database()
except ZODBSourceOpenFailError as e:
# Special handling for a common user error with pow which, nonetheless,
# may be encontered when *not* using pow
if e.openstr.endswith('.pow/worm.db'):
raise ConnectionFailError(e, 'Perhaps you need to do a `pow clone`?')
raise ConnectionFailError(e)
except Exception as e:
raise ConnectionFailError(e)

logging.getLogger('PyOpenWorm').info("Connected to database")

atexit.register(disconnect)
Expand Down
24 changes: 22 additions & 2 deletions PyOpenWorm/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
_B_UNSET = object()


class OpenFailError(Exception):
pass


class _B(ConfigValue):

def __init__(self, f):
Expand Down Expand Up @@ -259,7 +263,12 @@ def init_database(self):
""" Open the configured database """
self._init_rdf_graph()
L.debug("opening " + str(self.source))
self.source.open()
try:
self.source.open()
except OpenFailError as e:
L.error('Failed to open the data source because: %s', e)
raise

nm = NamespaceManager(self['rdf.graph'])
self['rdf.namespace_manager'] = nm
self['rdf.graph'].namespace_manager = nm
Expand Down Expand Up @@ -577,6 +586,13 @@ def open(self):
self.graph.open(self.conf['rdf.store_conf'], create=True)


class ZODBSourceOpenFailError(OpenFailError):
def __init__(self, openstr, *args):
super(ZODBSourceOpenFailError, self).__init__('Could not open the database file "{}"'.format(openstr),
*args)
self.openstr = openstr


class ZODBSource(RDFSource):

""" Reads from and queries against a configured Zope Object Database.
Expand All @@ -601,7 +617,11 @@ def open(self):
self.path = self.conf['rdf.store_conf']
openstr = os.path.abspath(self.path)

fs = FileStorage(openstr)
try:
fs = FileStorage(openstr)
except FileNotFoundError:
raise ZODBSourceOpenFailError(openstr)

self.zdb = ZODB.DB(fs, cache_size=1600)
self.conn = self.zdb.open()
root = self.conn.root()
Expand Down

0 comments on commit 477ae0b

Please sign in to comment.