<commit_before># Copyright 2012 Managed I.T.
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker import exceptions
from moniker.database import BaseDatabase
from moniker.database.sqlalchemy import models
from moniker.database.sqlalchemy.session import get_session

LOG = logging.getLogger(__name__)

cfg.CONF.register_opts([
    cfg.StrOpt('sql-connection', default='sqlite:///test.sqlite',
               help='The database connection string'),
])


class Sqlalchemy(BaseDatabase):
    def __init__(self):
        self.session = get_session()
        models.Base.metadata.create_all(self.session.bind)  # HACK: Remove me

    # Server Methods
    def create_server(self, context, values):
        server = models.Server()

        server.update(values)

        try:
            server.save()
        except IntegrityError, e:
            if ' unique ' in str(e):
                raise exceptions.DuplicateServer()
            else:
                raise

        return dict(server)

    def get_servers(self, context):
        query = self.session.query(models.Server)

        try:
            result = query.all()
        except NoResultFound:
            LOG.debug('No results found')
            return []
        else:
            return [dict(o) for o in result]<commit_msg>Move create schema hack into into it's own method.<commit_after># Copyright 2012 Managed I.T.
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker import exceptions
from moniker.database import BaseDatabase
from moniker.database.sqlalchemy import models
from moniker.database.sqlalchemy.session import get_session

LOG = logging.getLogger(__name__)

cfg.CONF.register_opts([
    cfg.StrOpt('sql-connection', default='sqlite:///test.sqlite',
               help='The database connection string'),
])


class Sqlalchemy(BaseDatabase):
    def __init__(self):
        self.session = get_session()
        self._initialize_database()  # HACK: Remove me

    def _initialize_database(self):
        """ Semi-Private Method to create the database schema """
        models.Base.metadata.create_all(self.session.bind)

    # Server Methods
    def create_server(self, context, values):
        server = models.Server()

        server.update(values)

        try:
            server.save()
        except IntegrityError, e:
            if ' unique ' in str(e):
                raise exceptions.DuplicateServer()
            else:
                raise

        return dict(server)

    def get_servers(self, context):
        query = self.session.query(models.Server)

        try:
            result = query.all()
        except NoResultFound:
            LOG.debug('No results found')
            return []
        else:
            return [dict(o) for o in result]
{'fraction_non_alphanumeric': 0.08507109004739337, 'fraction_numerical': 0.0037914691943127963, 'mean_word_length': 2.9265116279069767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4110449', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1091, 'n_words': 387}