2005-01-27 07:59:08 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2008, James McCoy
|
2005-01-27 07:59:08 +01:00
|
|
|
# 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.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# 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 COPYRIGHT OWNER 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2012-08-04 17:24:08 +02:00
|
|
|
import ast
|
2005-01-27 07:59:08 +01:00
|
|
|
import time
|
|
|
|
import types
|
|
|
|
import textwrap
|
|
|
|
import traceback
|
2012-08-04 17:09:32 +02:00
|
|
|
import collections
|
2012-08-04 17:03:51 +02:00
|
|
|
from itertools import imap
|
2005-01-27 07:59:08 +01:00
|
|
|
|
2005-01-31 15:52:27 +01:00
|
|
|
from str import format
|
2005-01-27 07:59:08 +01:00
|
|
|
from file import mktemp
|
|
|
|
|
2008-12-08 21:38:38 +01:00
|
|
|
import crypt
|
|
|
|
|
2013-04-21 16:00:31 +02:00
|
|
|
from supybot.i18n import PluginInternationalization
|
|
|
|
_ = PluginInternationalization()
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
def abbrev(strings, d=None):
|
|
|
|
"""Returns a dictionary mapping unambiguous abbreviations to full forms."""
|
|
|
|
def eachSubstring(s):
|
|
|
|
for i in xrange(1, len(s)+1):
|
|
|
|
yield s[:i]
|
|
|
|
if len(strings) != len(set(strings)):
|
|
|
|
raise ValueError, \
|
2005-02-15 14:57:57 +01:00
|
|
|
'strings given to utils.abbrev have duplicates: %r' % strings
|
2005-01-27 07:59:08 +01:00
|
|
|
if d is None:
|
|
|
|
d = {}
|
|
|
|
for s in strings:
|
|
|
|
for abbreviation in eachSubstring(s):
|
|
|
|
if abbreviation not in d:
|
|
|
|
d[abbreviation] = s
|
|
|
|
else:
|
|
|
|
if abbreviation not in strings:
|
|
|
|
d[abbreviation] = None
|
|
|
|
removals = []
|
|
|
|
for key in d:
|
|
|
|
if d[key] is None:
|
|
|
|
removals.append(key)
|
|
|
|
for key in removals:
|
|
|
|
del d[key]
|
|
|
|
return d
|
|
|
|
|
|
|
|
def timeElapsed(elapsed, short=False, leadingZeroes=False, years=True,
|
|
|
|
weeks=True, days=True, hours=True, minutes=True, seconds=True):
|
|
|
|
"""Given <elapsed> seconds, returns a string with an English description of
|
2009-08-16 23:17:05 +02:00
|
|
|
the amount of time passed. leadingZeroes determines whether 0 days, 0
|
|
|
|
hours, etc. will be printed; the others determine what larger time periods
|
|
|
|
should be used.
|
2005-01-27 07:59:08 +01:00
|
|
|
"""
|
|
|
|
ret = []
|
2009-07-17 23:08:20 +02:00
|
|
|
before = False
|
2005-01-31 15:52:27 +01:00
|
|
|
def Format(s, i):
|
2005-01-27 07:59:08 +01:00
|
|
|
if i or leadingZeroes or ret:
|
|
|
|
if short:
|
|
|
|
ret.append('%s%s' % (i, s[0]))
|
|
|
|
else:
|
2005-01-31 15:52:27 +01:00
|
|
|
ret.append(format('%n', (i, s)))
|
2005-01-27 07:59:08 +01:00
|
|
|
elapsed = int(elapsed)
|
2009-07-17 23:08:20 +02:00
|
|
|
|
|
|
|
# Handle negative times
|
|
|
|
if elapsed < 0:
|
|
|
|
before = True
|
|
|
|
elapsed = -elapsed
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
assert years or weeks or days or \
|
|
|
|
hours or minutes or seconds, 'One flag must be True'
|
|
|
|
if years:
|
|
|
|
(yrs, elapsed) = (elapsed // 31536000, elapsed % 31536000)
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('year'), yrs)
|
2005-01-27 07:59:08 +01:00
|
|
|
if weeks:
|
|
|
|
(wks, elapsed) = (elapsed // 604800, elapsed % 604800)
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('week'), wks)
|
2005-01-27 07:59:08 +01:00
|
|
|
if days:
|
|
|
|
(ds, elapsed) = (elapsed // 86400, elapsed % 86400)
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('day'), ds)
|
2005-01-27 07:59:08 +01:00
|
|
|
if hours:
|
|
|
|
(hrs, elapsed) = (elapsed // 3600, elapsed % 3600)
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('hour'), hrs)
|
2005-01-27 07:59:08 +01:00
|
|
|
if minutes or seconds:
|
|
|
|
(mins, secs) = (elapsed // 60, elapsed % 60)
|
|
|
|
if leadingZeroes or mins:
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('minute'), mins)
|
2005-01-27 07:59:08 +01:00
|
|
|
if seconds:
|
|
|
|
leadingZeroes = True
|
2013-04-21 16:00:31 +02:00
|
|
|
Format(_('second'), secs)
|
2005-01-27 07:59:08 +01:00
|
|
|
if not ret:
|
|
|
|
raise ValueError, 'Time difference not great enough to be noted.'
|
2009-07-17 23:08:20 +02:00
|
|
|
result = ''
|
2005-01-27 07:59:08 +01:00
|
|
|
if short:
|
2009-07-17 23:08:20 +02:00
|
|
|
result = ' '.join(ret)
|
2005-01-27 07:59:08 +01:00
|
|
|
else:
|
2009-07-17 23:08:20 +02:00
|
|
|
result = format('%L', ret)
|
|
|
|
if before:
|
2013-04-21 16:00:31 +02:00
|
|
|
result = _('%s ago') % result
|
2009-07-17 23:08:20 +02:00
|
|
|
return result
|
2005-01-27 07:59:08 +01:00
|
|
|
|
|
|
|
def findBinaryInPath(s):
|
|
|
|
"""Return full path of a binary if it's in PATH, otherwise return None."""
|
|
|
|
cmdLine = None
|
|
|
|
for dir in os.getenv('PATH').split(':'):
|
|
|
|
filename = os.path.join(dir, s)
|
|
|
|
if os.path.exists(filename):
|
|
|
|
cmdLine = filename
|
|
|
|
break
|
|
|
|
return cmdLine
|
|
|
|
|
|
|
|
def sortBy(f, L):
|
|
|
|
"""Uses the decorate-sort-undecorate pattern to sort L by function f."""
|
|
|
|
for (i, elt) in enumerate(L):
|
|
|
|
L[i] = (f(elt), i, elt)
|
|
|
|
L.sort()
|
|
|
|
for (i, elt) in enumerate(L):
|
|
|
|
L[i] = L[i][2]
|
|
|
|
|
|
|
|
def saltHash(password, salt=None, hash='sha'):
|
|
|
|
if salt is None:
|
|
|
|
salt = mktemp()[:8]
|
|
|
|
if hash == 'sha':
|
2008-12-08 21:38:38 +01:00
|
|
|
hasher = crypt.sha
|
2005-01-27 07:59:08 +01:00
|
|
|
elif hash == 'md5':
|
2008-12-08 21:38:38 +01:00
|
|
|
hasher = crypt.md5
|
2012-08-04 17:12:23 +02:00
|
|
|
return '|'.join([salt, hasher((salt + password).encode('utf8')).hexdigest()])
|
2005-01-27 07:59:08 +01:00
|
|
|
|
2012-08-04 17:24:08 +02:00
|
|
|
_astStr2 = ast.Str if sys.version_info[0] < 3 else ast.Bytes
|
2005-01-27 07:59:08 +01:00
|
|
|
def safeEval(s, namespace={'True': True, 'False': False, 'None': None}):
|
|
|
|
"""Evaluates s, safely. Useful for turning strings into tuples/lists/etc.
|
|
|
|
without unsafely using eval()."""
|
|
|
|
try:
|
2012-08-04 17:24:08 +02:00
|
|
|
node = ast.parse(s)
|
2005-01-27 07:59:08 +01:00
|
|
|
except SyntaxError, e:
|
|
|
|
raise ValueError, 'Invalid string: %s.' % e
|
2012-08-04 17:24:08 +02:00
|
|
|
nodes = ast.parse(s).body
|
2005-01-27 07:59:08 +01:00
|
|
|
if not nodes:
|
2012-08-04 17:24:08 +02:00
|
|
|
if node.__class__ is ast.Module:
|
2005-01-27 07:59:08 +01:00
|
|
|
return node.doc
|
|
|
|
else:
|
2005-01-31 15:52:27 +01:00
|
|
|
raise ValueError, format('Unsafe string: %q', s)
|
2005-01-27 07:59:08 +01:00
|
|
|
node = nodes[0]
|
|
|
|
def checkNode(node):
|
2012-08-04 17:24:08 +02:00
|
|
|
if node.__class__ is ast.Expr:
|
|
|
|
node = node.value
|
|
|
|
if node.__class__ in (ast.Num,
|
|
|
|
ast.Str,
|
|
|
|
_astStr2):
|
2005-01-27 07:59:08 +01:00
|
|
|
return True
|
2012-08-04 17:24:08 +02:00
|
|
|
elif node.__class__ in (ast.List,
|
|
|
|
ast.Tuple):
|
|
|
|
return all([checkNode(x) for x in node.elts])
|
|
|
|
elif node.__class__ is ast.Dict:
|
|
|
|
return all([checkNode(x) for x in node.values]) and \
|
|
|
|
all([checkNode(x) for x in node.values])
|
|
|
|
elif node.__class__ is ast.Name:
|
|
|
|
if node.id in namespace:
|
2005-01-27 07:59:08 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
if checkNode(node):
|
|
|
|
return eval(s, namespace, namespace)
|
|
|
|
else:
|
2005-01-31 16:21:22 +01:00
|
|
|
raise ValueError, format('Unsafe string: %q', s)
|
2005-01-27 07:59:08 +01:00
|
|
|
|
|
|
|
def exnToString(e):
|
|
|
|
"""Turns a simple exception instance into a string (better than str(e))"""
|
|
|
|
strE = str(e)
|
|
|
|
if strE:
|
|
|
|
return '%s: %s' % (e.__class__.__name__, strE)
|
|
|
|
else:
|
|
|
|
return e.__class__.__name__
|
|
|
|
|
|
|
|
class IterableMap(object):
|
|
|
|
"""Define .iteritems() in a class and subclass this to get the other iters.
|
|
|
|
"""
|
|
|
|
def iteritems(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
for (key, _) in self.iteritems():
|
|
|
|
yield key
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
for (_, value) in self.iteritems():
|
|
|
|
yield value
|
|
|
|
|
2012-08-05 12:32:57 +02:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
# Our 2to3 fixers automatically rename iteritems/iterkeys/itervalues
|
|
|
|
# to items/keys/values
|
|
|
|
def items(self):
|
|
|
|
return list(self.iteritems())
|
2005-01-27 07:59:08 +01:00
|
|
|
|
2012-08-05 12:32:57 +02:00
|
|
|
def keys(self):
|
|
|
|
return list(self.iterkeys())
|
2005-01-27 07:59:08 +01:00
|
|
|
|
2012-08-05 12:32:57 +02:00
|
|
|
def values(self):
|
|
|
|
return list(self.itervalues())
|
|
|
|
__iter__ = iterkeys
|
|
|
|
else:
|
|
|
|
__iter__ = items
|
2005-01-27 07:59:08 +01:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
ret = 0
|
|
|
|
for _ in self.iteritems():
|
|
|
|
ret += 1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
for _ in self.iteritems():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-08-04 17:09:32 +02:00
|
|
|
class InsensitivePreservingDict(collections.MutableMapping):
|
2005-01-27 07:59:08 +01:00
|
|
|
def key(self, s):
|
|
|
|
"""Override this if you wish."""
|
|
|
|
if s is not None:
|
|
|
|
s = s.lower()
|
|
|
|
return s
|
|
|
|
|
|
|
|
def __init__(self, dict=None, key=None):
|
|
|
|
if key is not None:
|
|
|
|
self.key = key
|
|
|
|
self.data = {}
|
|
|
|
if dict is not None:
|
|
|
|
self.update(dict)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s(%s)' % (self.__class__.__name__,
|
|
|
|
super(InsensitivePreservingDict, self).__repr__())
|
|
|
|
|
|
|
|
def fromkeys(cls, keys, s=None, dict=None, key=None):
|
|
|
|
d = cls(dict=dict, key=key)
|
|
|
|
for key in keys:
|
|
|
|
d[key] = s
|
|
|
|
return d
|
|
|
|
fromkeys = classmethod(fromkeys)
|
|
|
|
|
|
|
|
def __getitem__(self, k):
|
|
|
|
return self.data[self.key(k)][1]
|
|
|
|
|
|
|
|
def __setitem__(self, k, v):
|
|
|
|
self.data[self.key(k)] = (k, v)
|
|
|
|
|
|
|
|
def __delitem__(self, k):
|
|
|
|
del self.data[self.key(k)]
|
|
|
|
|
2012-08-04 17:09:32 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.data)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
def iteritems(self):
|
|
|
|
return self.data.itervalues()
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
L = []
|
|
|
|
for (k, _) in self.iteritems():
|
|
|
|
L.append(k)
|
|
|
|
return L
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
return (self.__class__, (dict(self.data.values()),))
|
|
|
|
|
|
|
|
|
|
|
|
class NormalizingSet(set):
|
|
|
|
def __init__(self, iterable=()):
|
|
|
|
iterable = imap(self.normalize, iterable)
|
|
|
|
super(NormalizingSet, self).__init__(iterable)
|
|
|
|
|
|
|
|
def normalize(self, x):
|
|
|
|
return x
|
|
|
|
|
|
|
|
def add(self, x):
|
|
|
|
return super(NormalizingSet, self).add(self.normalize(x))
|
|
|
|
|
|
|
|
def remove(self, x):
|
|
|
|
return super(NormalizingSet, self).remove(self.normalize(x))
|
|
|
|
|
|
|
|
def discard(self, x):
|
|
|
|
return super(NormalizingSet, self).discard(self.normalize(x))
|
|
|
|
|
|
|
|
def __contains__(self, x):
|
|
|
|
return super(NormalizingSet, self).__contains__(self.normalize(x))
|
|
|
|
has_key = __contains__
|
|
|
|
|
|
|
|
def stackTrace(frame=None, compact=True):
|
|
|
|
if frame is None:
|
|
|
|
frame = sys._getframe()
|
|
|
|
if compact:
|
|
|
|
L = []
|
|
|
|
while frame:
|
|
|
|
lineno = frame.f_lineno
|
|
|
|
funcname = frame.f_code.co_name
|
|
|
|
filename = os.path.basename(frame.f_code.co_filename)
|
|
|
|
L.append('[%s|%s|%s]' % (filename, funcname, lineno))
|
|
|
|
frame = frame.f_back
|
|
|
|
return textwrap.fill(' '.join(L))
|
|
|
|
else:
|
|
|
|
return traceback.format_stack(frame)
|
|
|
|
|
|
|
|
def callTracer(fd=None, basename=True):
|
|
|
|
if fd is None:
|
|
|
|
fd = sys.stdout
|
|
|
|
def tracer(frame, event, _):
|
|
|
|
if event == 'call':
|
|
|
|
code = frame.f_code
|
|
|
|
lineno = frame.f_lineno
|
|
|
|
funcname = code.co_name
|
|
|
|
filename = code.co_filename
|
|
|
|
if basename:
|
|
|
|
filename = os.path.basename(filename)
|
|
|
|
print >>fd, '%s: %s(%s)' % (filename, funcname, lineno)
|
|
|
|
return tracer
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|