<commit_before>"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  <NAME>
    This file is part of SleekXMPP.

    See the file license.txt for copying permission.
"""
from __future__ import with_statement, unicode_literals


from xml.etree import cElementTree as ET
from . xmlstream.xmlstream import XMLStream
from . xmlstream.matcher.xmlmask import MatchXMLMask
from . xmlstream.matcher.many import MatchMany
from . xmlstream.handler.xmlcallback import XMLCallback
from . xmlstream.handler.xmlwaiter import XMLWaiter
from . xmlstream.handler.waiter import Waiter
from . xmlstream.handler.callback import Callback
from . import plugins
from . stanza.message import Message
from . stanza.iq import Iq
from . stanza.presence import Presence
from . stanza.roster import Roster
from . stanza.nick import Nick
from . stanza.htmlim import HTMLIM
from . stanza.error import Error

import logging
import threading

import sys

if sys.version_info < (3,0):
	reload(sys)
	sys.setdefaultencoding('utf8')


def stanzaPlugin(stanza, plugin):
	stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
	stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin


class basexmpp(object):
	def __init__(self):
		self.id = 0
		self.id_lock = threading.Lock()
		self.sentpresence = False
		self.fulljid = ''
		self.resource = ''
		self.jid = ''
		self.username = ''
		self.server = ''
		self.plugin = {}
		self.auto_authorize = True
		self.auto_subscribe = True
		self.event_handlers = {}
		self.roster = {}
		self.registerHandler(Callback('IM', MatchXMLMask("<message xmlns='%s'><body /></message>" % self.default_ns), self._handleMessage))
		self.registerHandler(Callback('Presence', MatchXMLMask("<presence xmlns='%s' />" % self.default_ns), self._handlePresence))
		self.add_event_handler('presence_subscribe', self._handlePresenceSubscribe)
		self.registerStanza(Message)
		self.registerStanza(Iq)
		self.registerStanza(Presence)
		self.stanzaPlugin(Iq, Roster)
		self.stanzaPlugin(Message, Nick)
		self.stanzaPlugin(Message, HTMLIM)

	def stanzaPlugin(self, stanza, plugin):
		stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
		stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin
	
	def Message(self, *args, **kwargs):
		return Message(self, *args, **kwargs)

	def Iq(self, *args, **kwargs):
		return Iq(self, *args, **kwargs)

	def Presence(self, *args, **kwargs):
		return Presence(self, *args, **kwargs)
	
	def set_jid(self, jid):
		"""Rip a JID apart and claim it as our own."""
		self.fulljid = jid
		self.resource = self.getjidresource(jid)
		self.jid = self.getjidbare(jid)
		self.username = jid.split('@', 1)[0]
		self.server = jid.split('@',1)[-1].split('/', 1)[0]
		
	def registerPlugin(self, plugin, pconfig = {}):
		"""Register a plugin not in plugins.__init__.__all__ but in the plugins
		directory."""
		# discover relative "path" to the plugins module from the main app, and import it.
		# TODO:
		# gross, this probably isn't necessary anymore, especially for an installed module
		__import__("%s.%s" % (globals()['plugins'].__name__, plugin))
		# init the plugin class
		self.plugin[plugin] = getattr(getattr(plugins, plugin), plugin)(self, pconfig) # eek
		# all of this for a nice debug? sure.
		xep = ''
		if hasattr(self.plugin[plugin], 'xep'):
			xep = "(XEP-%s) " % self.plugin[plugin].xep
		logging.debug("Loaded Plugin %s%s" % (xep, self.plugin[plugin].description))
	
	def register_plugins(self):
		"""Initiates all plugins in the plugins/__init__.__all__"""
		if self.plugin_whitelist:
			plugin_list = self.plugin_whitelist
		else:
			plugin_list = plugins.__all__
		for plugin in plugin_list:
			if plugin in plugins.__all__:
				self.registerPlugin(plugin, self.plugin_config.get(plugin, {}))
			else:
				raise NameError("No plugin by the name of %s listed in plugins.__all__." % plugin)
		# run post_init() for cross-plugin interaction
		for plugin in self.plugin:
			self.plugin[plugin].post_init()
	
	def getNewId(self):
		with self.id_lock:
			self.id += 1
			return self.getId()
	
	def add_handler(self, mask, pointer, disposable=False, threaded=False, filter=False, instream=False):
		#logging.warning("Deprecated add_handler used for %s: %s." % (mask, pointer))
		self.registerHandler(XMLCallback('add_handler_%s' % self.getNewId(), MatchXMLMask(mask), pointer, threaded, disposable, instream))
	
	def getId(self):
		return "%x".upper() % self.id

	def sendXML(self, data, mask=None, timeout=10):
		return self.send(self.tostring(data), mask, timeout)
	
	def send(self, data, mask=None, timeout=10):
		#logging.warning("Deprecated send used for \"%s\"" % (data,))
		#if not type(data) == type(''):
		#	data = self.tostring(data)<commit_msg>Added a flag to registerPlugin to control calling the plugin's post_init method.<commit_after>"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  <NAME>
    This file is part of SleekXMPP.

    See the file license.txt for copying permission.
"""
from __future__ import with_statement, unicode_literals


from xml.etree import cElementTree as ET
from . xmlstream.xmlstream import XMLStream
from . xmlstream.matcher.xmlmask import MatchXMLMask
from . xmlstream.matcher.many import MatchMany
from . xmlstream.handler.xmlcallback import XMLCallback
from . xmlstream.handler.xmlwaiter import XMLWaiter
from . xmlstream.handler.waiter import Waiter
from . xmlstream.handler.callback import Callback
from . import plugins
from . stanza.message import Message
from . stanza.iq import Iq
from . stanza.presence import Presence
from . stanza.roster import Roster
from . stanza.nick import Nick
from . stanza.htmlim import HTMLIM
from . stanza.error import Error

import logging
import threading

import sys

if sys.version_info < (3,0):
	reload(sys)
	sys.setdefaultencoding('utf8')


def stanzaPlugin(stanza, plugin):
	stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
	stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin


class basexmpp(object):
	def __init__(self):
		self.id = 0
		self.id_lock = threading.Lock()
		self.sentpresence = False
		self.fulljid = ''
		self.resource = ''
		self.jid = ''
		self.username = ''
		self.server = ''
		self.plugin = {}
		self.auto_authorize = True
		self.auto_subscribe = True
		self.event_handlers = {}
		self.roster = {}
		self.registerHandler(Callback('IM', MatchXMLMask("<message xmlns='%s'><body /></message>" % self.default_ns), self._handleMessage))
		self.registerHandler(Callback('Presence', MatchXMLMask("<presence xmlns='%s' />" % self.default_ns), self._handlePresence))
		self.add_event_handler('presence_subscribe', self._handlePresenceSubscribe)
		self.registerStanza(Message)
		self.registerStanza(Iq)
		self.registerStanza(Presence)
		self.stanzaPlugin(Iq, Roster)
		self.stanzaPlugin(Message, Nick)
		self.stanzaPlugin(Message, HTMLIM)

	def stanzaPlugin(self, stanza, plugin):
		stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
		stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin
	
	def Message(self, *args, **kwargs):
		return Message(self, *args, **kwargs)

	def Iq(self, *args, **kwargs):
		return Iq(self, *args, **kwargs)

	def Presence(self, *args, **kwargs):
		return Presence(self, *args, **kwargs)
	
	def set_jid(self, jid):
		"""Rip a JID apart and claim it as our own."""
		self.fulljid = jid
		self.resource = self.getjidresource(jid)
		self.jid = self.getjidbare(jid)
		self.username = jid.split('@', 1)[0]
		self.server = jid.split('@',1)[-1].split('/', 1)[0]
		
	def registerPlugin(self, plugin, pconfig = {}, run_post=True):
		"""Register a plugin not in plugins.__init__.__all__ but in the plugins
		directory."""
		# discover relative "path" to the plugins module from the main app, and import it.
		# TODO:
		# gross, this probably isn't necessary anymore, especially for an installed module
		__import__("%s.%s" % (globals()['plugins'].__name__, plugin))
		# init the plugin class
		self.plugin[plugin] = getattr(getattr(plugins, plugin), plugin)(self, pconfig) # eek
		# all of this for a nice debug? sure.
		xep = ''
		if hasattr(self.plugin[plugin], 'xep'):
			xep = "(XEP-%s) " % self.plugin[plugin].xep
		logging.debug("Loaded Plugin %s%s" % (xep, self.plugin[plugin].description))
		if run_post:
			self.plugin[plugin].post_init()
	
	def register_plugins(self):
		"""Initiates all plugins in the plugins/__init__.__all__"""
		if self.plugin_whitelist:
			plugin_list = self.plugin_whitelist
		else:
			plugin_list = plugins.__all__
		for plugin in plugin_list:
			if plugin in plugins.__all__:
				self.registerPlugin(plugin, self.plugin_config.get(plugin, {}), False)
			else:
				raise NameError("No plugin by the name of %s listed in plugins.__all__." % plugin)
		# run post_init() for cross-plugin interaction
		for plugin in self.plugin:
			self.plugin[plugin].post_init()
	
	def getNewId(self):
		with self.id_lock:
			self.id += 1
			return self.getId()
	
	def add_handler(self, mask, pointer, disposable=False, threaded=False, filter=False, instream=False):
		#logging.warning("Deprecated add_handler used for %s: %s." % (mask, pointer))
		self.registerHandler(XMLCallback('add_handler_%s' % self.getNewId(), MatchXMLMask(mask), pointer, threaded, disposable, instream))
	
	def getId(self):
		return "%x".upper() % self.id

	def sendXML(self, data, mask=None, timeout=10):
		return self.send(self.tostring(data), mask, timeout)
	
	def send(self, data, mask=None, timeout=10):
		#logging.warning("Deprecated send used for \"%s\"" % (data,))
		#if not type(data) == type(''):
		#	data = self.tostring(data)
{'fraction_non_alphanumeric': 0.1317902199979147, 'fraction_numerical': 0.003962047753101866, 'mean_word_length': 4.658997050147493, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '6679967', 'n_tokens_mistral': 3434, 'n_tokens_neox': 3158, 'n_words': 815}