<commit_before>from types import StringTypes

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from sqlalchemy import Column, Integer, String
from sqlalchemy import ForeignKey

from sfa.util.sfalogging import logger

# this module is designed to be loaded when the configured db server is reachable
# OTOH model can be loaded from anywhere including the client-side

class Alchemy:

    def __init__ (self, config):
        dbname="sfa"
        # will be created lazily on-demand
        self._session = None
        # the former PostgreSQL.py used the psycopg2 directly and was doing
        #self.connection.set_client_encoding("UNICODE")
        # it's unclear how to achieve this in sqlalchemy, nor if it's needed at all
        # http://www.sqlalchemy.org/docs/dialects/postgresql.html#unicode
        # we indeed have /var/lib/pgsql/data/postgresql.conf where
        # this setting is unset, it might be an angle to tweak that if need be
        # try a unix socket first - omitting the hostname does the trick
        unix_url = "postgresql+psycopg2://%s:%s@:%s/%s"%\
            (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_PORT,dbname)
        # the TCP fallback method
        tcp_url = "postgresql+psycopg2://%s:%s@%s:%s/%s"%\
            (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_HOST,config.SFA_DB_PORT,dbname)
        for url in [ unix_url, tcp_url ] :
            try:
                self.engine = create_engine (url)
                self.check()
                self.url=url
                return
            except:
                pass
        self.engine=None
        logger.warning("Could not connect to database: db=%s, user=%s, host=%s, port=%s" % \
                           (dbname, config.SFA_DB_USER, config.SFA_DB_HOST, config.SFA_DB_PORT))
        logger.debug("Using password=%s"%config.SFA_DB_PASSWORD)
        raise Exception,"Could not connect to database %s as %s"%(dbname,config.SFA_DB_USER)


    # expects boolean True: debug is ON or False: debug is OFF
    def debug (self, echo):
        self.engine.echo=echo

    def check (self):
        self.engine.execute ("select 1").scalar()

    def session (self):
        if self._session is None:<commit_msg>again clarify messages when db connection is failing<commit_after>from types import StringTypes

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from sqlalchemy import Column, Integer, String
from sqlalchemy import ForeignKey

from sfa.util.sfalogging import logger

# this module is designed to be loaded when the configured db server is reachable
# OTOH model can be loaded from anywhere including the client-side

class Alchemy:

    def __init__ (self, config):
        dbname="sfa"
        # will be created lazily on-demand
        self._session = None
        # the former PostgreSQL.py used the psycopg2 directly and was doing
        #self.connection.set_client_encoding("UNICODE")
        # it's unclear how to achieve this in sqlalchemy, nor if it's needed at all
        # http://www.sqlalchemy.org/docs/dialects/postgresql.html#unicode
        # we indeed have /var/lib/pgsql/data/postgresql.conf where
        # this setting is unset, it might be an angle to tweak that if need be
        # try a unix socket first - omitting the hostname does the trick
        unix_url = "postgresql+psycopg2://%s:%s@:%s/%s"%\
            (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_PORT,dbname)
        # the TCP fallback method
        tcp_url = "postgresql+psycopg2://%s:%s@%s:%s/%s"%\
            (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_HOST,config.SFA_DB_PORT,dbname)
        for url in [ unix_url, tcp_url ] :
            try:
                logger.debug("Trying db URL %s"%url)
                self.engine = create_engine (url)
                self.check()
                self.url=url
                return
            except:
                pass
        self.engine=None
        raise Exception,"Could not connect to database %s as %s with psycopg2"%(dbname,config.SFA_DB_USER)


    # expects boolean True: debug is ON or False: debug is OFF
    def debug (self, echo):
        self.engine.echo=echo

    def check (self):
        self.engine.execute ("select 1").scalar()

    def session (self):
        if self._session is None:
{'fraction_non_alphanumeric': 0.08610792192881746, 'fraction_numerical': 0.0020665901262916187, 'mean_word_length': 2.564648117839607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1742244', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1302, 'n_words': 395}