<commit_before>#!/usr/bin/env python
#
#  PyGab - Python Jabber Framework
#  Copyright (c) 2008, <NAME>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  - Redistributions of source code must retain the above copyright
#  notice, this list of conditions and the following disclaimer.
#
#  - Redistributions in binary form must reproduce the above copyright
#  notice, this list of conditions and the following disclaimer in the
#  documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## PLEASE NOTE
## utils.py performs a * import on the current module's custom utils.py file
## This allows plugins to function seemlessly across modules.

from __future__ import with_statement

import	datetime
import	dict4ini
import	errno
import	os
import	re
import	sys
import	time
import	traceback

from 	common.ini		import	iMan
from	xmpp.protocol	import	JID

#Command exception classes
class CommandHelp(Exception):pass
class CommandError(Exception):pass

def get_module():
	'''get_module() -> str

	Return the name of the module by importing __main__
	Warning: If this is called on import in a module that is imported
				by __main__ it will cause an infinate import loop.

	'''
	# These two lines are depricated lines.
	# It turns out __main__.__file__ == sys.argv[0] so using sys.argv[0]
	# removes the risk of infinate import loops! =]
	#import __main__
	#selffile = os.path.split(__main__.__file__)[1]
	selffile = os.path.split(sys.argv[0])[1]
	return os.path.splitext(selffile)[0]

def get_import(mod=get_module(), from_=[], import_=[]):
	'''get_import(mod=get_module(), from_=[], import_=[]) -> str

	Return a string that can be used to import from the running module.
	mod		- The name of the module or base python module.
	from_   - A list of sub-modules. Joined by '.'s
	import_ - A list of objects to import from a module. Join by ','s

	Warning: If this is called on import in a module that is imported
				by __main__ it will cause an infinate import loop.

	'''
	f = ''
	i = '*'

	if from_:
		assert isinstance(from_, list)
		f = '.%s' % '.'.join(from_)

	if import_:
		assert isinstance(import_, list)
		i = ', '.join(import_)

	if from_:
		return 'from %s%s import %s' % (mod, f, i)

#=============================
#=         User Tools        =
#=============================
#======
#= User Names
def getname(jid):
	"""getname(xmpp.protocol.JID jid) -> unicode

	Converts a user@domain/resource to simply a user name.

	"""
	# Make sure all JIDs are stardized to be JID objects.
	# They're much easier to manipulate.
	assert isinstance(jid, JID)
	domain = iMan.config.server.domain
	if jid.domain == domain:
		return unicode(jid.node)

	if '%' in jid.node and jid.domain != domain:
		return unicode(jid.node[:jid.node.find('%')])

	return unicode(jid)

def getnickname(jid):
	"""getnickname(xmpp.protocol.JID jid) -> unicode

	Converts a user@domain/resource to a displayable nickname.

	"""
	# Make sure all JIDs are stardized to be JID objects.
	# They're much easier to manipulate.
	assert isinstance(jid, JID)
	domain = iMan.config.server.domain

	if has_nick(jid):
		return get_nick(jid)

	# If there is no nickname, return the name of the user.
	return getname(jid)

def getjid(user, domain='', resource=''):
	"""getjid(str user, str domain=iMan.config.server.domain
		str resource=iMan.config.server.resource) -> xmpp.protocol.JID

	Returns a JID for 'user'.

	"""
	if isinstance(user, JID):
			return user

	if not domain:
		domain = iMan.config.server.domain

	# We can't do anything with non-strings.
	assert isinstance(user, basestring), 'getjid got passed a %s' % type(user)

	if '@' in user:
		user = user.split('@', 1)[0]
	return JID(
		node=user,
		domain=domain,
		resource=resource
	)

def has_nick(jid):
	"""Returns True if jid has a nickname"""
	pass

def get_nick(jid):
	"""Returns the nickname of jid"""
	pass

def has_attr(jid, attr, value):
	"""Return True if 'jid' has value."""
	return iMan.has_entry('roster', jid, attr, value)
def del_attr(jid, attr, rank):
	"""Delete value from 'jid'."""
	return iMan.del_entry('roster', jid, attr, value)
def add_attr(jid, attr, rank):
	"""Add value to 'jid'."""
	return iMan.add_entry('roster', jid, attr, value)
def set_attr(jid, attr, rank):
	"""Set 'jid's attr to value."""
	return iMan.set_entry('roster', jid, attr, value)
<commit_msg>Purged a dict4ini import.<commit_after>#!/usr/bin/env python
#
#  PyGab - Python Jabber Framework
#  Copyright (c) 2008, <NAME>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  - Redistributions of source code must retain the above copyright
#  notice, this list of conditions and the following disclaimer.
#
#  - Redistributions in binary form must reproduce the above copyright
#  notice, this list of conditions and the following disclaimer in the
#  documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## PLEASE NOTE
## utils.py performs a * import on the current module's custom utils.py file
## This allows plugins to function seemlessly across modules.

from __future__ import with_statement

import	datetime
import	errno
import	os
import	re
import	sys
import	time
import	traceback

from 	common.ini		import iMan
from	xmpp.protocol	import JID

#Command exception classes
class CommandHelp(Exception):pass
class CommandError(Exception):pass

def get_module():
	'''get_module() -> str

	Return the name of the module by importing __main__
	Warning: If this is called on import in a module that is imported
				by __main__ it will cause an infinate import loop.

	'''
	# These two lines are depricated lines.
	# It turns out __main__.__file__ == sys.argv[0] so using sys.argv[0]
	# removes the risk of infinate import loops! =]
	#import __main__
	#selffile = os.path.split(__main__.__file__)[1]
	selffile = os.path.split(sys.argv[0])[1]
	return os.path.splitext(selffile)[0]

def get_import(mod=get_module(), from_=[], import_=[]):
	'''get_import(mod=get_module(), from_=[], import_=[]) -> str

	Return a string that can be used to import from the running module.
	mod		- The name of the module or base python module.
	from_   - A list of sub-modules. Joined by '.'s
	import_ - A list of objects to import from a module. Join by ','s

	Warning: If this is called on import in a module that is imported
				by __main__ it will cause an infinate import loop.

	'''
	f = ''
	i = '*'

	if from_:
		assert isinstance(from_, list)
		f = '.%s' % '.'.join(from_)

	if import_:
		assert isinstance(import_, list)
		i = ', '.join(import_)

	if from_:
		return 'from %s%s import %s' % (mod, f, i)

#=============================
#=         User Tools        =
#=============================
#======
#= User Names
def getname(jid):
	"""getname(xmpp.protocol.JID jid) -> unicode

	Converts a user@domain/resource to simply a user name.

	"""
	# Make sure all JIDs are stardized to be JID objects.
	# They're much easier to manipulate.
	assert isinstance(jid, JID)
	domain = iMan.config.server.domain
	if jid.domain == domain:
		return unicode(jid.node)

	if '%' in jid.node and jid.domain != domain:
		return unicode(jid.node[:jid.node.find('%')])

	return unicode(jid)

def getnickname(jid):
	"""getnickname(xmpp.protocol.JID jid) -> unicode

	Converts a user@domain/resource to a displayable nickname.

	"""
	# Make sure all JIDs are stardized to be JID objects.
	# They're much easier to manipulate.
	assert isinstance(jid, JID)
	domain = iMan.config.server.domain

	if has_nick(jid):
		return get_nick(jid)

	# If there is no nickname, return the name of the user.
	return getname(jid)

def getjid(user, domain='', resource=''):
	"""getjid(str user, str domain=iMan.config.server.domain
		str resource=iMan.config.server.resource) -> xmpp.protocol.JID

	Returns a JID for 'user'.

	"""
	if isinstance(user, JID):
			return user

	if not domain:
		domain = iMan.config.server.domain

	# We can't do anything with non-strings.
	assert isinstance(user, basestring), 'getjid got passed a %s' % type(user)

	if '@' in user:
		user = user.split('@', 1)[0]
	return JID(
		node=user,
		domain=domain,
		resource=resource
	)

def has_nick(jid):
	"""Returns True if jid has a nickname"""
	pass

def get_nick(jid):
	"""Returns the nickname of jid"""
	pass

def has_attr(jid, attr, value):
	"""Return True if 'jid' has value."""
	return iMan.has_entry('roster', jid, attr, value)
def del_attr(jid, attr, rank):
	"""Delete value from 'jid'."""
	return iMan.del_entry('roster', jid, attr, value)
def add_attr(jid, attr, rank):
	"""Add value to 'jid'."""
	return iMan.add_entry('roster', jid, attr, value)
def set_attr(jid, attr, rank):
	"""Set 'jid's attr to value."""
	return iMan.set_entry('roster', jid, attr, value)

def isbanned(user): return has_attr(getname(user).lower(), 'rank', 'banned')
def ismod(user): return has_attr(getname(user).lower(), 'rank', 'mod')
def isadmin(user): return has_attr(getname(user).lower(), 'rank', 'admin')

{'fraction_non_alphanumeric': 0.11848739495798319, 'fraction_numerical': 0.002427637721755369, 'mean_word_length': 4.258222876779578, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '6045504', 'n_tokens_mistral': 3644, 'n_tokens_neox': 3412, 'n_words': 1303}