<commit_before>'''
Copyright 2013 <NAME><<EMAIL>>

This file is part of ddserver.

ddserver is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.

ddserver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ddserver.  If not, see <http://www.gnu.org/licenses/>.
'''

import contextlib
import threading

import MySQLdb



class DatabaseManager(object):

  thread_local = threading.local()


  def __init__(self):
    pass


  def setup(self,
            dbhost,
            dbport,
            dbname,
            dbuser,
            dbpass):
    self.dbhost = dbhost
    self.dbport = dbport
    self.dbname = dbname
    self.dbuser = dbuser
    self.dbpass = dbpass


  @contextlib.contextmanager
  def cursor(self):
    if not hasattr(DatabaseManager.thread_local, 'db_connection'):
        setattr(DatabaseManager.thread_local, 'db_connection',
                MySQLdb.connect(self.dbhost,
                                self.dbuser,
                                self.dbpass,
                                self.dbname,
                                self.dbport,
                                cursorclass = MySQLdb.cursors.DictCursor))

    connection = getattr(DatabaseManager.thread_local, 'db_connection')

    cursor = connection.cursor()

    try:
      yield cursor

    except:
      connection.rollback()

    else:<commit_msg>Pass through database exceptions<commit_after>'''
Copyright 2013 <NAME><<EMAIL>>

This file is part of ddserver.

ddserver is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.

ddserver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ddserver.  If not, see <http://www.gnu.org/licenses/>.
'''

import contextlib
import threading

import MySQLdb



class DatabaseManager(object):

  thread_local = threading.local()


  def __init__(self):
    pass


  def setup(self,
            dbhost,
            dbport,
            dbname,
            dbuser,
            dbpass):
    self.dbhost = dbhost
    self.dbport = dbport
    self.dbname = dbname
    self.dbuser = dbuser
    self.dbpass = dbpass


  @contextlib.contextmanager
  def cursor(self):
    if not hasattr(DatabaseManager.thread_local, 'db_connection'):
        setattr(DatabaseManager.thread_local, 'db_connection',
                MySQLdb.connect(self.dbhost,
                                self.dbuser,
                                self.dbpass,
                                self.dbname,
                                self.dbport,
                                cursorclass = MySQLdb.cursors.DictCursor))

    connection = getattr(DatabaseManager.thread_local, 'db_connection')

    cursor = connection.cursor()

    try:
      yield cursor

    except:
      connection.rollback()
      raise

    else:
{'fraction_non_alphanumeric': 0.0667601683029453, 'fraction_numerical': 0.002805049088359046, 'mean_word_length': 2.262580054894785, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4773234', 'n_tokens_mistral': 973, 'n_tokens_neox': 914, 'n_words': 329}