<commit_before>import logging
import weakref

import telepathy


_moduleLogger = logging.getLogger("handle")


class MetaMemoize(type):
	"""
	Allows a class to cache off instances for reuse
	"""

	def __call__(cls, connection, *args):
		obj, newlyCreated = cls.__new__(cls, connection, *args)
		if newlyCreated:
			obj.__init__(connection, connection.get_handle_id(), *args)
			_moduleLogger.info("New Handle %r" % obj)
		return obj


class TheOneRingHandle(telepathy.server.Handle):
	"""
	Instances are memoized
	"""

	__metaclass__ = MetaMemoize

	_instances = weakref.WeakValueDictionary()

	def __new__(cls, connection, *args):
		key = cls, connection.username, args
		if key in cls._instances.keys():
			return cls._instances[key], False
		else:
			instance = object.__new__(cls, connection, *args)
			cls._instances[key] = instance # TRICKY: instances is a weakdict
			return instance, True

	def __init__(self, connection, id, handleType, name):
		telepathy.server.Handle.__init__(self, id, handleType, name)
		self._conn = weakref.proxy(connection)

	def __repr__(self):
		return "<%s id=%u name='%s'>" % (
			type(self).__name__, self.id, self.name
		)

	id = property(telepathy.server.Handle.get_id)
	type = property(telepathy.server.Handle.get_type)
	name = property(telepathy.server.Handle.get_name)


class ConnectionHandle(TheOneRingHandle):

	instance = None

	def __init__(self, connection, id):
		handleType = telepathy.HANDLE_TYPE_CONTACT
		handleName = connection.username
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)

		self.profile = connection.username


class ContactHandle(TheOneRingHandle):

	def __init__(self, connection, id, contactId):
		handleType = telepathy.HANDLE_TYPE_CONTACT
		handleName = contactId
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)

		self._id = contactId

	@property
	def contactID(self):
		return self._id

	@property
	def contactDetails(self):
		return self._conn.addressbook.get_contact_details(self._id)


class ListHandle(TheOneRingHandle):

	def __init__(self, connection, id, listName):
		handleType = telepathy.HANDLE_TYPE_LIST
		handleName = listName
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)


_HANDLE_TYPE_MAPPING = {
	'connection': ConnectionHandle,
	'contact': ContactHandle,
	'list': ListHandle,
}


def create_handle(connection, type, *args):
	handle = _HANDLE_TYPE_MAPPING[type](connection, *args)
	connection._handles[handle.get_type(), handle.get_id()] = handle
	return handle<commit_msg>Fixing a deprecation warning and some bugs<commit_after>import logging
import weakref

import telepathy


_moduleLogger = logging.getLogger("handle")


class TheOneRingHandle(telepathy.server.Handle):
	"""
	Instances are memoized
	"""

	def __init__(self, connection, id, handleType, name):
		telepathy.server.Handle.__init__(self, id, handleType, name)
		self._conn = weakref.proxy(connection)

	def __repr__(self):
		return "<%s id=%u name='%s'>" % (
			type(self).__name__, self.id, self.name
		)

	id = property(telepathy.server.Handle.get_id)
	type = property(telepathy.server.Handle.get_type)
	name = property(telepathy.server.Handle.get_name)


class ConnectionHandle(TheOneRingHandle):

	def __init__(self, connection, id):
		handleType = telepathy.HANDLE_TYPE_CONTACT
		handleName = connection.username
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)

		self.profile = connection.username


class ContactHandle(TheOneRingHandle):

	def __init__(self, connection, id, contactId):
		handleType = telepathy.HANDLE_TYPE_CONTACT
		handleName = contactId
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)

		self._id = contactId

	@property
	def contactID(self):
		return self._id

	@property
	def contactDetails(self):
		return self._conn.addressbook.get_contact_details(self._id)


class ListHandle(TheOneRingHandle):

	def __init__(self, connection, id, listName):
		handleType = telepathy.HANDLE_TYPE_LIST
		handleName = listName
		TheOneRingHandle.__init__(self, connection, id, handleType, handleName)


_HANDLE_TYPE_MAPPING = {
	'connection': ConnectionHandle,
	'contact': ContactHandle,
	'list': ListHandle,
}


def create_handle_factory():

	cache = weakref.WeakValueDictionary()

	def create_handle(connection, type, *args):
		Handle = _HANDLE_TYPE_MAPPING[type]
		key = Handle, connection.username, args
		try:
			handle = cache[key]
			isNewHandle = False
		except KeyError:
			handle = Handle(connection, connection.get_handle_id(), *args)
			cache[key] = handle
			isNewHandle = True
		connection._handles[handle.get_type(), handle.get_id()] = handle
		handleStatus = "Is New!" if isNewHandle else "From Cache"
		_moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
		return handle

	return create_handle


create_handle = create_handle_factory()
{'fraction_non_alphanumeric': 0.10767022149302707, 'fraction_numerical': 0.0, 'mean_word_length': 5.442536327608983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5673236', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1527, 'n_words': 352}