From: James French Date: Mon, 4 Mar 2013 11:31:27 +0000 (+0800) Subject: Merge branch 'master' into cgi X-Git-Tag: cgi-1.0~4 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=257bf962578c430062608cb1e10248ec854423db;hp=2597e404590f2a839fcbbffb572542ab8a6b2310;p=frenchie%2Ficalparse.git Merge branch 'master' into cgi --- diff --git a/icalparse.py b/icalparse.py index f00b400..3907da7 100755 --- a/icalparse.py +++ b/icalparse.py @@ -116,13 +116,16 @@ def getHTTPContent(url='',cache='.httplib2-cache'): return (content, encoding) -def generateRules(): +def generateRules(ruleConfig): '''Attempts to load a series of rules into a list''' try: import parserrules except ImportError: return [] + for conf in ruleConfig: + parserrules.ruleConfig[conf] = ruleConfig[conf] + rules = [getattr(parserrules, rule) for rule in dir(parserrules) if callable(getattr(parserrules, rule))] return rules @@ -157,9 +160,9 @@ def writeOutput(cal, outfile=''): out.close() if __name__ == '__main__': + # Only load options parsing if this script was called directly, skip it + # if it's being called as a module. from optparse import OptionParser - # If the user passed us a 'stdin' argument, we'll go with that, - # otherwise we'll try for a url opener parser = OptionParser('usage: %prog [options] url') parser.add_option('-s', '--stdin', action='store_true', dest='stdin', @@ -171,9 +174,17 @@ if __name__ == '__main__': parser.add_option('-m','--encoding', dest='encoding', default='', help='Specify a different character encoding' '(ignored if the remote server also specifies one)') + parser.add_option('-t','--timezone', dest='timezone', default='Australia/Perth', + help='Specify a timezone to use if the remote calendar doesn\'t set it properly') (options, args) = parser.parse_args() + # Ensure the rules process using the desired timezone + ruleConfig = {} + ruleConfig["defaultTZ"] = options.timezone + + # If the user passed us a 'stdin' argument, we'll go with that, + # otherwise we'll try for a url opener if not args and not options.stdin: parser.print_usage() sys.exit(0) @@ -186,6 +197,6 @@ if __name__ == '__main__': encoding = encoding or options.encoding or 'utf-8' cal = vobject.readOne(unicode(content, encoding)) - cal = applyRules(cal, generateRules(), options.verbose) + cal = applyRules(cal, generateRules(ruleConfig), options.verbose) writeOutput(cal, options.outfile) diff --git a/parserrules.py b/parserrules.py index cf60f98..f1c55c1 100644 --- a/parserrules.py +++ b/parserrules.py @@ -28,6 +28,9 @@ import vobject +ruleConfig = {} +ruleConfig["defaultTZ"] = "UTC" + def facebookOrganiser(cal): '''Adds organiser details to the body of facebook calendars.''' @@ -104,8 +107,7 @@ def exDate(cal): from datetime import datetime from pytz import timezone - default = timezone('Australia/Perth') - + default = timezone(ruleConfig["defaultTZ"]) for event in cal.vevent_list: if not event.contents.has_key(u'exdate'): continue @@ -123,12 +125,13 @@ def exDate(cal): return cal def utcise(cal): - '''Removing local timezones in favour of UTC''' + '''Removing local timezones in favour of UTC. If the remote calendar specifies a timezone + then use it, otherwise assume it's in the user-specified or default values''' from datetime import datetime from pytz import timezone - default = timezone('Australia/Perth') + default = timezone(ruleConfig["defaultTZ"]) for event in cal.vevent_list: dtstart = getattr(event, 'dtstart', None) @@ -163,7 +166,8 @@ def unwantedParams(cal): return cal def exDate(cal): - '''Changes multi-EXDATE into singles (apple can't obey even simple specs)''' + '''Changes multi-EXDATE into singles (apple can't obey even simple specs). + If the remote calendar specifies a timezone then use it, otherwise use the user specified value''' for event in cal.vevent_list: if not event.contents.has_key(u'exdate'): continue