4 from xml.dom import minidom
8 def open_anything(source, alternative=None):
9 """URI, filename, or string --> stream
11 This function lets you define parsers that take any input source
12 (URL, pathname to local or network file, or actual data as a string)
13 and deal with it in a uniform manner. Returned object is guaranteed
14 to have all the basic stdio read methods (read, readline, readlines).
15 Just .close() the object when you're done with it.
17 if hasattr(source, "read"):
23 # try to open with urllib (if source is http, ftp, or file URL)
25 return urllib.urlopen(source)
26 except (IOError, OSError):
28 print "ERROR with URL ("+source+")!\n"
30 # try to open with native open function (if source is pathname)
33 except (IOError, OSError):
35 print "ERROR with file!\n"
37 # treat source as string
38 if alternative == None:
39 print 'LAST RESORT. String is "'+source+'"\n'
40 return StringIO.StringIO(str(source))
42 print 'LAST RESORT. String is "'+alternative+'"\n'
43 return StringIO.StringIO(str(alternative))
46 def load_xml(source, alternative=None):
47 """load XML input source, return parsed XML document
49 - a URL of a remote XML file ("http://diveintopython.org/kant.xml")
50 - a filename of a local XML file ("~/diveintopython/common/py/kant.xml")
51 - standard input ("-")
52 - the actual XML document, as a string
54 sock = open_anything(source, alternative)
56 xmldoc = minidom.parse(sock).documentElement
57 except (IOError, OSError):
58 print "ERROR with data"
60 sock = open_anything('<response method="getProjects"><project projName="ERROR!"/></response>')
61 xmldoc = minidom.parse(sock).documentElement
66 def to_fuzzy_date(targetDate, todaysDate = datetime.datetime.today()):
68 Conert a date/time/datetime object to a fuzzy date
70 @bug Not perfect, but good enough for now
72 delta = targetDate - todaysDate
73 days = abs(delta.days)
74 directionBy1 = "Next" if 0 < delta.days else "Last"
75 directionByN = "Later" if 0 < delta.days else "Earlier"
76 directionByInf = "from now" if 0 < delta.days else "ago"
78 return "Forever %s" % directionByInf
80 return "%s year" % directionBy1
82 return "%s this year" % directionByN
84 return "%s month" % directionBy1
86 return "%s this month" % directionByN
88 return "%s week" % directionBy1
90 return "%s this week" % directionByN
92 return "%s day" % directionByN