<commit_before># encoding: utf-8
from __future__ import unicode_literals, division, print_function, absolute_import

from marrow.wsgi.exceptions import HTTPException
from marrow.util.compat import basestring

try:
    from sqlalchemy import MetaData, create_engine
    from sqlalchemy.orm import scoped_session
    from sqlalchemy.orm.session import sessionmaker
    from sqlalchemy.orm.scoping import ScopedSession
except ImportError:
    raise ImportError('You need to install SQLAlchemy to use this extension')


class SQLAlchemyExtension(object):
    uses = ['transaction']
    provides = ['sqlalchemy', 'database']

    def __init__(self, url, contextvar='dbsession', session=None, metadata=None, session_opts={}, **kwargs):
        super(SQLAlchemyExtension, self).__init__()

        if not isinstance(contextvar, basestring):
            raise TypeError('The "contextvar" option needs to be a string')
        if session is not None and not isinstance(session, ScopedSession):
            raise TypeError('The "session" option needs to be a reference to a ScopedSession instance')
        if metadata is not None and not isinstance(metadata, MetaData):
            raise TypeError('The "metadata" option needs to be a reference to a MetaData instance')
        if not isinstance(session_opts, dict):
            raise TypeError('The "session_opts" option needs to be a dictionary')

        self._contextvar = contextvar
        self._engine = create_engine(url, **kwargs)
        self._session = session
        self._metadata = metadata
        self._session_opts = session_opts

    def start(self, context):
        # Test the validity of the URL by attempting to make a connection to the target database
        self._engine.connect().close()

        # Create a new ScopedSession if none was given
        self._session = scoped_session(sessionmaker(**self._session_opts))

        # Bind the engine to the session to enable execution of raw SQL
        self._session.configure(bind=self._engine)

        # Bind the engine to the given metadata to facilitate operations like metadata.create_all()
        if self._metadata is not None:
            self._metadata.bind = self._engine

        # Add the session to the context
        if hasattr(context, self._contextvar):
            raise Exception('The context already has a variable named "%s"' % self._contextvar)
        setattr(context, self._contextvar, self._session)

    def after(self, context, exc=None):
        try:
            if self._session.is_active:
                if exc is None or isinstance(exc, HTTPException):
                    self._session.commit()
                else:
                    self._session.rollback()
        finally:
            # Return the context-local connection to the connection pool
            self._session.close()<commit_msg>Code style update and extension provides simplification.<commit_after># encoding: utf-8
from __future__ import unicode_literals, division, print_function, absolute_import

from marrow.wsgi.exceptions import HTTPException
from marrow.util.compat import basestring

try:
	from sqlalchemy import MetaData, create_engine
	from sqlalchemy.orm import scoped_session
	from sqlalchemy.orm.session import sessionmaker
	from sqlalchemy.orm.scoping import ScopedSession
except ImportError:
	raise ImportError('You need to install SQLAlchemy to use this extension')


class SQLAlchemyExtension(object):
	uses = ['transaction']
	provides = ['sqlalchemy']
	
	def __init__(self, url, contextvar='dbsession', session=None, metadata=None, session_opts={}, **kwargs):
		super(SQLAlchemyExtension, self).__init__()
		
		if not isinstance(contextvar, basestring):
			raise TypeError('The "contextvar" option needs to be a string')
		if session is not None and not isinstance(session, ScopedSession):
			raise TypeError('The "session" option needs to be a reference to a ScopedSession instance')
		if metadata is not None and not isinstance(metadata, MetaData):
			raise TypeError('The "metadata" option needs to be a reference to a MetaData instance')
		if not isinstance(session_opts, dict):
			raise TypeError('The "session_opts" option needs to be a dictionary')
		
		self._contextvar = contextvar
		self._engine = create_engine(url, **kwargs)
		self._session = session
		self._metadata = metadata
		self._session_opts = session_opts
	
	def start(self, context):
		# Test the validity of the URL by attempting to make a connection to the target database.
		self._engine.connect().close()
		
		# Create a new ScopedSession if none was given.
		self._session = scoped_session(sessionmaker(**self._session_opts))
		
		# Bind the engine to the session to enable execution of raw SQL.
		self._session.configure(bind=self._engine)
		
		# Bind the engine to the given metadata to facilitate operations like metadata.create_all()
		if self._metadata is not None:
			self._metadata.bind = self._engine
		
		# Add the session to the context
		if hasattr(context, self._contextvar):
			raise Exception('The context already has a variable named "%s"' % self._contextvar)
		setattr(context, self._contextvar, self._session)
	
	def after(self, context, exc=None):
		try:
			if self._session.is_active:
				if exc is None or isinstance(exc, HTTPException):
					self._session.commit()
				else:
					self._session.rollback()
		finally:
			# Return the context-local connection to the connection pool.
			self._session.close()
{'fraction_non_alphanumeric': 0.0779555065269351, 'fraction_numerical': 0.00036771465342893915, 'mean_word_length': 3.590717299578059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '2206329', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1402, 'n_words': 518}