Open Kilda Java Documentation
db.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 
16 import collections
17 import logging
18 import os
19 import pprint
20 
21 import py2neo
22 
23 from topologylistener import config
24 from topologylistener import exc
25 
26 log = logging.getLogger(__name__)
27 
28 
30  graph = py2neo.Graph("bolt://{}:{}@{}:7687".format(
31  os.environ.get('neo4juser') or config.get('neo4j', 'user'),
32  os.environ.get('neo4jpass') or config.get('neo4j', 'pass'),
33  os.environ.get('neo4jhost') or config.get('neo4j', 'host')))
34  return graph
35 
36 
37 def log_query(marker, q, p):
38  q = q.strip()
39  log.debug('NEO4J QUERY %s:\n%s\nparams:\n%s', marker, q, pprint.pformat(p))
40 
41 
42 def locate_changes(target, props):
43  origin = {}
44  update = {}
45  for field, value in props.items():
46  try:
47  current = target[field]
48  except KeyError:
49  update[field] = props[field]
50  else:
51  if current != props[field]:
52  update[field] = props[field]
53  origin[field] = current
54 
55  return origin, update
56 
57 
58 def neo_id(db_object):
59  return db_object.identity
60 
61 
62 def format_set_fields(payload, field_prefix=''):
63  return format_fields(payload, 'SET ', field_prefix=field_prefix)
64 
65 
66 def format_fields(payload, prefix, field_prefix=''):
67  if not payload:
68  return ''
69 
70  separator = ',\n' + (' ' * len(prefix)) + field_prefix
71  fields = [' = '.join(field) for field in payload]
72  return prefix + field_prefix + separator.join(fields)
73 
74 
75 def escape_fields(payload, raw_values=False):
76  result = []
77  for field, value in payload.items():
78  if value is None:
79  value = 'null'
80  elif not raw_values:
81  value = py2neo.cypher_repr(value)
82  result.append(
83  (py2neo.cypher_escape(field), value))
84  return result
85 
86 
87 def escape(identifier):
88  return py2neo.cypher_escape(identifier)
89 
90 
91 def fetch_one(cursor):
92  extra = result_set = None
93  try:
94  result_set = next(cursor)
95  extra = next(cursor)
96  except StopIteration:
97  if result_set is None:
99 
100  if extra is not None:
102 
103  return result_set
104 
105 
106 class ResponseIterator(collections.Iterator):
107  is_first = True
108 
109  def __init__(self, cursor, q, p):
110  self.cursor = cursor
112 
113  def next(self):
114  try:
115  value = next(self.cursor)
116  except StopIteration:
117  if self.is_first:
118  raise self.exc_empty
119  raise StopIteration
120  finally:
121  self.is_first = False
122 
123  return value
124 
125  def __iter__(self):
126  return self
def create_p2n_driver()
Definition: db.py:29
def escape(identifier)
Definition: db.py:87
def __init__(self, cursor, q, p)
Definition: db.py:109
def neo_id(db_object)
Definition: db.py:58
def locate_changes(target, props)
Definition: db.py:42
def format_set_fields(payload, field_prefix='')
Definition: db.py:62
def format_fields(payload, prefix, field_prefix='')
Definition: db.py:66
def fetch_one(cursor)
Definition: db.py:91
def log_query(marker, q, p)
Definition: db.py:37
def escape_fields(payload, raw_values=False)
Definition: db.py:75