-
Notifications
You must be signed in to change notification settings - Fork 2
/
flask_neomodel.py
93 lines (77 loc) · 2.66 KB
/
flask_neomodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
Neo4j GraphDB flask connector through OGM library 'neomodel'
"""
import time
import logging
import neomodel as neomodel_package
from flask import _app_ctx_stack as stack
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
class NeoModel(object):
def __init__(self, app=None, variables=None, exit_if_fails=True):
self.neo = neomodel_package
self.app = app
if variables is None:
variables = {}
self.variables = variables
self.exit = exit_if_fails
if app is not None:
self.init_app(app)
self.connect()
log.info('connected')
def init_app(self, app):
# TODO: set any default to flask config?
# app.config.setdefault('GRAPH_DATABASE', ':memory:')
app.teardown_appcontext(self.teardown)
def connect(self):
# Set URI
self.uri = "bolt://%s:%s@%s:%s" % \
(
# User:Password
self.variables.get('user', 'neo4j'),
self.variables.get('password', 'test'),
# Host:Port
self.variables.get('host', 'localhost'),
self.variables.get('port', 7687),
)
log.debug("Connection uri: %s" % self.uri)
# Try until it's connected
self.retry()
self.graph_db = self.neo.db
log.info("Connected! %s" % self.graph_db)
return self.graph_db
def retry(self, retry_interval=2, max_retries=-1):
retry_count = 0
while max_retries != 0 or retry_count < max_retries:
retry_count += 1
if self.test_connection():
break
else:
log.info("Service not available")
if self.exit:
raise ValueError('No connection available')
time.sleep(retry_interval)
def test_connection(self, retry_interval=5, max_retries=0):
try:
self.neo.config.DATABASE_URL = self.uri
self.neo.db.url = self.uri
self.neo.db.set_connection(self.uri)
return True
except BaseException as e:
log.warning("Failed: %s", e)
return False
def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'graph_db'):
log.info("Tearing down")
# neo does not have an 'open' connection that needs closing
# ctx.graphdb.close()
ctx.graph_db = None
@property
def connection(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'graphdb'):
ctx.graphdb = self.connect()
return ctx.graphdb