<commit_before># encoding: utf-8

"""Database connection handling extension."""

from functools import partial

from ..core.context import ContextGroup


class DBExtension(object):
	__slots__ = ('engines', 'uses', 'needs', 'provides')
	
	_provides = {'db'}
	
	def __init__(self, default=None, **engines):
		engines['default'] = default
		self.engines = engines
		
		self.uses = set()
		self.needs = set()
		self.provides = set(self._provides)
		
		for name, engine in engines.items():
			if engine is None: continue
			engine.__name__ = name  # Inform the engine what its name is.
			self.uses.update(getattr(engine, 'uses', ()))
			self.needs.update(getattr(engine, 'needs', ()))
			self.provides.update(getattr(engine, 'provides', ()))
		
		super().__init__()
	
	def start(self, context):
		context.db = ContextGroup(**self.engines)
		self._handle_event('start', context)
	
	def prepare(self, context):
		context.db = context.db._promote('DBGroup')
		context.db['_ctx'] = context
		
		self._handle_event('prepare', context)
	
	def _handle_event(self, event, *args, **kw):
		for engine in self.engines.values():
			if engine is not None and hasattr(engine, event):
				getattr(engine, event)(*args, **kw)
	
	def __getattr__(self, name):
		if name not in ('stop', 'graceful', 'dispatch', 'before', 'after', 'done', 'interactive', 'inspect'):
			raise AttributeError()
		
		return partial(self._handle_event, name)

<commit_msg>Slight rename.<commit_after># encoding: utf-8

"""Database connection handling extension."""

from functools import partial

from ..core.context import ContextGroup


class DatabaseExtension(object):
	__slots__ = ('engines', 'uses', 'needs', 'provides')
	
	_provides = {'db'}
	
	def __init__(self, default=None, **engines):
		engines['default'] = default
		self.engines = engines
		
		self.uses = set()
		self.needs = set()
		self.provides = set(self._provides)
		
		for name, engine in engines.items():
			if engine is None: continue
			engine.__name__ = name  # Inform the engine what its name is.
			self.uses.update(getattr(engine, 'uses', ()))
			self.needs.update(getattr(engine, 'needs', ()))
			self.provides.update(getattr(engine, 'provides', ()))
		
		super().__init__()
	
	def start(self, context):
		context.db = ContextGroup(**self.engines)
		self._handle_event('start', context)
	
	def prepare(self, context):
		context.db = context.db._promote('DBGroup')
		context.db['_ctx'] = context
		
		self._handle_event('prepare', context)
	
	def _handle_event(self, event, *args, **kw):
		for engine in self.engines.values():
			if engine is not None and hasattr(engine, event):
				getattr(engine, event)(*args, **kw)
	
	def __getattr__(self, name):
		if name not in ('stop', 'graceful', 'dispatch', 'before', 'after', 'done', 'interactive', 'inspect'):
			raise AttributeError()
		
		return partial(self._handle_event, name)


class DBExtension(DatabaseExtension):
	def __init__(self, *args, **kw):
		super(DBExtension, self).__init__(*args, **kw)
		
		from warnings import warn
		warn('DBExtension is deprecated, use DatabaseExtension instead.', DeprecationWarning)


{'fraction_non_alphanumeric': 0.15178859168546568, 'fraction_numerical': 0.0006445375443119562, 'mean_word_length': 4.532976827094474, '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': '1622407', 'n_tokens_mistral': 1112, 'n_tokens_neox': 971, 'n_words': 261}