<commit_before>import logging
log = logging.getLogger(__name__)

from nose.plugins.skip import SkipTest
import re
from unittest import TestCase

from testconfig import config as tc


class AssertHandler(logging.handlers.BufferingHandler):
	"""
	A logging handler that allows making assertions based on its contents.
	"""

	def __init__(self, capacity=100, *args, **kwargs):
		"""
		Add ourselves to the main logger.
		"""

		logging.handlers.BufferingHandler.__init__(self, capacity, *args, **kwargs)

		logging.getLogger().addHandler(self)

	def assert_logged(self, level, msg, ignore_case=True):
		"""
		Assert that a message matching the level and regular expression has been logged.
		"""

		level = level.lower()

		re_flags = 0
		if ignore_case:
			re_flags |= re.IGNORECASE

		for record in self.buffer:
			if record.levelname.lower() == level and re.search(msg, record.msg, re_flags):
				return

		assert False, 'Log message not found at level "{0}": {1}'.format(level, msg)


class DeviceServerTestCase(TestCase):
	"""
	Class for a device server test.
	"""<commit_msg>Added "literal" option to AssertHandler<commit_after>import logging
log = logging.getLogger(__name__)

from nose.plugins.skip import SkipTest
import re
from unittest import TestCase

from testconfig import config as tc


class AssertHandler(logging.handlers.BufferingHandler):
	"""
	A logging handler that allows making assertions based on its contents.
	"""

	def __init__(self, capacity=100, *args, **kwargs):
		"""
		Add ourselves to the main logger.
		"""

		logging.handlers.BufferingHandler.__init__(self, capacity, *args, **kwargs)

		logging.getLogger().addHandler(self)

	def assert_logged(self, level, msg, ignore_case=True, literal=False):
		"""
		Assert that a message matching the level and regular expression has been logged.
		"""

		level = level.lower()

		re_flags = 0
		if ignore_case:
			re_flags |= re.IGNORECASE

		for record in self.buffer:
			if record.levelname.lower() == level:
				if (literal and msg == record.msg or
						not literal and re.search(msg, record.msg, re_flags)):
					return

		assert False, 'Log message not found at level "{0}": {1}'.format(level, msg)


class DeviceServerTestCase(TestCase):
	"""
	Class for a device server test.
	"""
{'fraction_non_alphanumeric': 0.10801239486498451, 'fraction_numerical': 0.005312084993359893, 'mean_word_length': 4.342789598108747, 'pattern_counts': {'":': 2, '<': 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': '6577344', 'n_tokens_mistral': 742, 'n_tokens_neox': 666, 'n_words': 225}