<commit_before># -*- coding: utf-8 -*-
#
#       core.py
#
#       Copyright 2016 <NAME> <<EMAIL>>
#

# This started as an extension to the Neo4j REST client made by Versae, continued
# as an extension for the official Neo4j python bindings when they were released
# (Neo4j 1.5, python-embedded).
#
# After the release of neo4j 3.0 and the bolt protocol we replaced neo4jdb-python with
# the official Neo4j driver.
#
# The goal is to make it easier to add and retrieve data from a Neo4j database
# according to the NORDUnet Network Inventory data model.
#
# More information about NORDUnet Network Inventory:
# https://portal.nordu.net/display/NI/

from __future__ import absolute_import

import re
from six import text_type
from neo4j.v1 import GraphDatabase, basic_auth
from neo4j.v1.exceptions import ResultError, CypherError, ProtocolError

from norduniclient import exceptions
from norduniclient import models

import logging
logger = logging.getLogger(__name__)

# Load Django settings
try:
    from django.core.exceptions import ImproperlyConfigured
    from django.conf import settings as django_settings
    try:
        NEO4J_URI = django_settings.NEO4J_RESOURCE_URI
        NEO4J_USERNAME = django_settings.NEO4J_USERNAME
        NEO4J_PASSWORD = django_settings.NEO4J_PASSWORD
    except ImproperlyConfigured:
        NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD = None, None, None
except ImportError:
    NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD = None, None, None
    logger.info('Starting up without a Django environment.')
    logger.info('Initial: norduniclient.neo4jdb == None.')
    logger.info('Use norduniclient.init_db(uri) to open a database connection.')


META_TYPES = ['Physical', 'Logical', 'Relation', 'Location']


class GraphDB(object):

    _instance = None
    _manager = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    def __init__(self):
        self._manager = self.manager

    @property
    def manager(self):
        if self._manager is None:
            try:
                self._manager = init_db()
            except ProtocolError:
                self._manager = None
        return self._manager


def init_db(uri=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD, encrypted=False):
    if uri:
        try:
            from norduniclient.contextmanager import Neo4jDBSessionManager
            manager = Neo4jDBSessionManager(uri=uri, username=username, password=password, encrypted=encrypted)
            try:<commit_msg>Added a setter for Grapdb.manager<commit_after># -*- coding: utf-8 -*-
#
#       core.py
#
#       Copyright 2016 <NAME> <<EMAIL>>
#

# This started as an extension to the Neo4j REST client made by Versae, continued
# as an extension for the official Neo4j python bindings when they were released
# (Neo4j 1.5, python-embedded).
#
# After the release of neo4j 3.0 and the bolt protocol we replaced neo4jdb-python with
# the official Neo4j driver.
#
# The goal is to make it easier to add and retrieve data from a Neo4j database
# according to the NORDUnet Network Inventory data model.
#
# More information about NORDUnet Network Inventory:
# https://portal.nordu.net/display/NI/

from __future__ import absolute_import

import re
from six import text_type
from neo4j.v1 import GraphDatabase, basic_auth
from neo4j.v1.exceptions import ResultError, CypherError, ProtocolError

from norduniclient import exceptions
from norduniclient import models

import logging
logger = logging.getLogger(__name__)

# Load Django settings
try:
    from django.core.exceptions import ImproperlyConfigured
    from django.conf import settings as django_settings
    try:
        NEO4J_URI = django_settings.NEO4J_RESOURCE_URI
        NEO4J_USERNAME = django_settings.NEO4J_USERNAME
        NEO4J_PASSWORD = django_settings.NEO4J_PASSWORD
    except ImproperlyConfigured:
        NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD = None, None, None
except ImportError:
    NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD = None, None, None
    logger.info('Starting up without a Django environment.')
    logger.info('Initial: norduniclient.neo4jdb == None.')
    logger.info('Use norduniclient.init_db to open a database connection.')


META_TYPES = ['Physical', 'Logical', 'Relation', 'Location']


class GraphDB(object):

    _instance = None
    _manager = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    def __init__(self):
        self._manager = self.manager

    @property
    def manager(self):
        if self._manager is None:
            try:
                self._manager = init_db()
            except ProtocolError:
                self._manager = None
        return self._manager

    @manager.setter
    def manager(self, manager):
        self._manager = manager


def init_db(uri=NEO4J_URI, username=NEO4J_USERNAME, password=<PASSWORD>, encrypted=False):
    if uri:
        try:
            from norduniclient.contextmanager import Neo4jDBSessionManager
            manager = Neo4jDBSessionManager(uri=uri, username=username, password=password, encrypted=encrypted)
            try:
{'fraction_non_alphanumeric': 0.07403882755995432, 'fraction_numerical': 0.01427483821850019, 'mean_word_length': 3.328665568369028, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1963099', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1593, 'n_words': 475}