Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def getDb(dbName='db.tests/demo1'):
if (os.path.isdir(dbName)):
logging.info('GraphDb found, loading...')
return GraphDatabase(dbName)
else:
logging.info('GraphDb not found, generating...')
db = GraphDatabase(dbName)
setUpDB(db)
return db
def getDb(dbName='db.tests/demo1'):
if (os.path.isdir(dbName)):
logging.info('GraphDb found, loading...')
return GraphDatabase(dbName)
else:
logging.info('GraphDb not found, generating...')
db = GraphDatabase(dbName)
setUpDB(db)
return db
def _run_cypher_query(cypher_query: str, kg='KG2') -> List[Dict[str, any]]:
# This function sends a cypher query to neo4j (either KG1 or KG2) and returns results
rtxc = RTXConfiguration()
if kg == 'KG2':
rtxc.live = 'KG2'
try:
driver = GraphDatabase.driver(rtxc.neo4j_bolt, auth=(rtxc.neo4j_username, rtxc.neo4j_password))
with driver.session() as session:
print(f"Sending cypher query to {kg} neo4j")
query_results = session.run(cypher_query).data()
print(f"Got {len(query_results)} results back from neo4j")
driver.close()
except Exception:
tb = traceback.format_exc()
error_type, error, _ = sys.exc_info()
print(f"Encountered an error interacting with {kg} neo4j. {tb}")
return []
else:
return query_results
log_to_postgres('The user parameter is required and the default is "neo4j"', ERROR)
self.user = options.get("user", "neo4j")
if 'password' not in options:
log_to_postgres('The password parameter is required for Neo4j', ERROR)
self.password = options.get("password", "")
if 'cypher' not in options:
log_to_postgres('The cypher parameter is required', ERROR)
self.cypher = options.get("cypher", "MATCH (n) RETURN n LIMIT 100")
# Setting table columns
self.columns = columns
# Create a neo4j driver instance
self.driver = GraphDatabase.driver( self.url, auth=(self.user, self.password))
self.columns_stat = self.compute_columns_stat()
self.table_stat = int(options.get("estimated_rows", -1))
if(self.table_stat < 0):
self.table_stat = self.compute_table_stat()
log_to_postgres('Table estimated rows : ' + str(self.table_stat), DEBUG)
def to_bolt_driver(driver=None):
if driver is None:
return None
try:
from neo4j import GraphDatabase, Driver
if isinstance(driver, Driver):
return driver
return GraphDatabase.driver(**driver)
except ImportError:
raise BoltSupportModuleNotFound()
def init_globals():
'''Restore global varibales on a running db'''
global db
global ROOT, PAPER, AUTHOR
global author_idx, source_idx, label_idx, search_idx
db = GraphDatabase(neo4j_db_folder)
label_idx = db.node.indexes.get('label_idx')
source_idx = db.node.indexes.get('source_idx')
author_idx = db.node.indexes.get('author_idx')
search_idx = db.node.indexes.get('search_idx')
AUTHOR = label_idx['label']['AUTHOR'].single
PAPER = label_idx['label']['PAPER'].single
ROOT = db.reference_node
def __init__(self, params):
self.dbPath = params["dbPath"]
self.graphdb = neo4j.GraphDatabase(self.dbPath)
self.dataFiles = []
dfs = params["dataFiles"]
for df in dfs: self.dataFiles.append(dataUtils.getDataFileNameOnly(df))
self.cleanRegex = re.compile('[\!\*\?]+')
self.pprintr = pprint.PrettyPrinter(indent=4)
@implementer(IGraph)
def graph_factory():
"""Utility to store the db graph conneciton object
"""
settings = get_current_registry().settings
connection_string = settings['graphdb_connection_string']
import os
os.environ['NEO4J_PYTHON_JVMARGS'] = '-Xms128M -Xmx512M'
from neo4j import GraphDatabase
db = GraphDatabase(connection_string)
def close_db():
"""Make sure to always close the database
"""
try:
db.shutdown()
print("db shut down")
except NameError:
print 'Could not shutdown Neo4j database.'
atexit.register(close_db)
return EmbeddedGraph(db)
def isavailable():
try:
GraphDatabase.driver(
Neo4j.connection,
auth=(Neo4j.username, Neo4j.password)
).session()
except exceptions.ServiceUnavailable:
return False
return True