2003-03-27 21:10:10 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Simple utility functions.
|
|
|
|
"""
|
|
|
|
|
2003-03-31 08:55:34 +02:00
|
|
|
from __future__ import generators
|
|
|
|
|
2003-03-27 21:10:10 +01:00
|
|
|
from fix import *
|
|
|
|
|
|
|
|
import sgmllib
|
|
|
|
import htmlentitydefs
|
|
|
|
|
|
|
|
class HtmlToText(sgmllib.SGMLParser):
|
|
|
|
"""Taken from some eff-bot code on c.l.p."""
|
|
|
|
entitydefs = htmlentitydefs.entitydefs
|
2003-04-02 11:20:49 +02:00
|
|
|
def __init__(self, tagReplace=' '):
|
2003-03-27 21:10:10 +01:00
|
|
|
self.data = []
|
2003-04-02 11:20:49 +02:00
|
|
|
self.tagReplace = tagReplace
|
2003-03-27 21:10:10 +01:00
|
|
|
sgmllib.SGMLParser.__init__(self)
|
|
|
|
|
|
|
|
def unknown_starttag(self, tag, attrib):
|
2003-04-02 11:20:49 +02:00
|
|
|
self.data.append(self.tagReplace)
|
2003-03-27 21:10:10 +01:00
|
|
|
|
|
|
|
def unknown_endtag(self, tag):
|
2003-04-02 11:20:49 +02:00
|
|
|
self.data.append(self.tagReplace)
|
2003-03-27 21:10:10 +01:00
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
self.data.append(data)
|
|
|
|
|
|
|
|
def getText(self):
|
|
|
|
text = ''.join(self.data).strip()
|
2003-03-27 21:14:17 +01:00
|
|
|
return ' '.join(text.split()) # normalize whitespace
|
2003-03-27 21:10:10 +01:00
|
|
|
|
2003-04-02 11:20:49 +02:00
|
|
|
def htmlToText(s, tagReplace=' '):
|
|
|
|
x = HtmlToText(tagReplace)
|
2003-03-27 21:10:10 +01:00
|
|
|
x.feed(s)
|
|
|
|
return x.getText()
|
|
|
|
|
2003-03-31 07:14:21 +02:00
|
|
|
def eachSubstring(s):
|
|
|
|
for i in range(1, len(s)+1):
|
|
|
|
yield s[:i]
|
|
|
|
|
|
|
|
def abbrev(strings):
|
|
|
|
d = {}
|
|
|
|
for s in strings:
|
|
|
|
for abbreviation in eachSubstring(s):
|
|
|
|
if abbreviation not in d:
|
|
|
|
d[abbreviation] = s
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
2003-04-03 11:11:57 +02:00
|
|
|
def timeElapsed(now, then, leadingZeroes=False, years=True, weeks=True,
|
|
|
|
days=True, hours=True, minutes=True, seconds=True):
|
|
|
|
assert days or hours or minutes or seconds, 'One flag must be True'
|
|
|
|
elapsed = int(now - then)
|
|
|
|
ret = []
|
|
|
|
if years:
|
|
|
|
yrs, elapsed = elapsed // 31536000, elapsed % 31536000
|
|
|
|
if leadingZeroes or yrs:
|
|
|
|
if yrs:
|
|
|
|
leadingZeroes = True
|
|
|
|
if yrs != 1:
|
|
|
|
yrs = '%s years' % yrs
|
|
|
|
else:
|
|
|
|
yrs = '1 year'
|
|
|
|
ret.append(yrs)
|
|
|
|
if weeks:
|
|
|
|
wks, elapsed = elapsed // 604800, elapsed % 604800
|
|
|
|
if leadingZeroes or wks:
|
|
|
|
if wks:
|
|
|
|
leadingZeroes = True
|
|
|
|
if wks != 1:
|
|
|
|
wks = '%s weeks' % wks
|
|
|
|
else:
|
|
|
|
wks = '1 week'
|
|
|
|
ret.append(wks)
|
|
|
|
if days:
|
|
|
|
ds, elapsed = elapsed // 86400, elapsed % 86400
|
|
|
|
if leadingZeroes or ds:
|
|
|
|
if ds:
|
|
|
|
leadingZeroes = True
|
|
|
|
if ds != 1:
|
|
|
|
ds = '%s days' % ds
|
|
|
|
else:
|
|
|
|
ds = '1 day'
|
|
|
|
ret.append(ds)
|
|
|
|
if hours:
|
|
|
|
hrs, elapsed = elapsed // 3600, elapsed % 3600
|
|
|
|
if leadingZeroes or hrs:
|
|
|
|
if hrs:
|
|
|
|
leadingZeroes = True
|
|
|
|
if hrs != 1:
|
|
|
|
hrs = '%s hours' % hrs
|
|
|
|
else:
|
|
|
|
hrs = '1 hour'
|
|
|
|
ret.append(hrs)
|
|
|
|
if minutes or seconds:
|
|
|
|
mins, secs = elapsed // 60, elapsed % 60
|
|
|
|
if leadingZeroes or mins:
|
|
|
|
if mins != 1:
|
|
|
|
mins = '%s minutes' % mins
|
|
|
|
else:
|
|
|
|
mins = '1 minute'
|
|
|
|
ret.append(mins)
|
|
|
|
if seconds:
|
|
|
|
if secs != 1:
|
|
|
|
secs = '%s seconds' % secs
|
|
|
|
else:
|
|
|
|
secs = '1 second'
|
|
|
|
ret.append(secs)
|
|
|
|
if len(ret) == 0:
|
|
|
|
raise ValueError, 'Time difference not great enough to be noted.'
|
|
|
|
if len(ret) == 1:
|
|
|
|
return ret[0]
|
|
|
|
else:
|
|
|
|
return ' and '.join([', '.join(ret[:-1]), ret[-1]])
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-27 21:10:10 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|