Skip to content

Commit

Permalink
calendar module now working.
Browse files Browse the repository at this point in the history
TODO:
- output ?
- testing
- ...

Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
  • Loading branch information
awieser committed Dec 18, 2011
1 parent 7f1f413 commit b3c61fc
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 34 deletions.
4 changes: 4 additions & 0 deletions calendar/memacs-calendar.org
@@ -0,0 +1,4 @@
* Requirements
icalendar Python package
install it with
: $ easy install icalendar
75 changes: 50 additions & 25 deletions calendar/memacs-calendar.py
Expand Up @@ -12,14 +12,23 @@
from common.orgformat import OrgFormat
import urllib2
from urllib2 import HTTPError, URLError
from symbol import xor_expr
import sys
from common import orgwriter
import time
import calendar

try:
from icalendar import Calendar
except ImportError:
print "please install python package \"icalendar\""
sys.exit(3)


PROG_VERSION_NUMBER = u"0.1"
PROG_VERSION_DATE = u"2011-10-28"
# TODO set real description and so on
SHORT_DESCRIPTION = u"Memacs for file name time stamp"
TAG = u"filedatestamps"
TAG = u"calendar"
DESCRIPTION = u"""This script parses a text file containing absolute paths to files
with ISO datestamps and timestamps in their file names:
Expand Down Expand Up @@ -89,44 +98,60 @@ def main():
# do stuff
if args.calendar_file:
try:
file = codecs.open(args.calendar_file, 'rb', "utf_8")
file = codecs.open(args.calendar_file, 'rb')
data = file.read()
file.close()
except IOError:
logging.error("Error at opening file: %s" % args.calendar_file)
sys.exit(1)

elif args.calendar_url:
try:
req = urllib2.urlopen(args.calendar_url, None, 10)
data = req.read()
except HTTPError:
logging.error("Error at opening url: %s" % args.calendar_url)
sys.exit(1)
except URLError:
logging.error("Error at opening url: %s" % args.calendar_url)
sys.exit(1)
except ValueError:
logging.error("Error - no valid url: %s" % args.calendar_url)
sys.exit(1)

#print VEVENT_REGEX.findall(data)
for vevent in REGEX_VEVENT.findall(data):
print vevent
# dstart
dtstart_search = REGEX_VEVENT_DTSTART.search(vevent)
if dtstart_search:
dtstart = dtstart_search.group(1)
else:
dtstart = REGEX_VEVENT_DTSTARTALLDAY.search(vevent).group(1)
#dtend = REGEX_VEVENT_DTEND.search(vevent).group(1)
#description = REGEX_VEVENT_DESCRIPTION.search(vevent).group(1)
summary = REGEX_VEVENT_SUMMARY.search(vevent).group(1)
#location = REGEX_VEVENT_LOCATION.search(vevent).group(1)

print dtstart
#print dtend
#print description
#print summary

cal = Calendar.from_string(data);

#writer.write_org_subitem(orgdate + " " + OrgFormat.link(link=link, description=file))
# end do stuff
writer.close();
for component in cal.walk():
if component.name == "VCALENDAR":
# Set timezone
timezone = component.get('x-wr-timezone')
logging.debug("Setting timezone to: " + timezone)
os.environ['TZ'] = timezone;
time.tzset()
elif component.name == "VEVENT":
summary = unicode(component.get('summary'))
location = unicode(component.get('location'))
description = unicode(component.get('description'))
dtstart = unicode(component.get('dtstart')) # format: 20091207T180000Z or 20100122
dtend = unicode(component.get('dtend')) # format: 20091207T180000Z or 20100122
dtstamp = unicode(component.get('dtstamp')) # format: 20091207T180000Z
# logging.debug(summary)
# logging.debug(dtstart)
# logging.debug(dtend)
orgdatecreated = OrgFormat.date(OrgFormat.datetupelutctimestamp(dtstamp))
orgdate = OrgFormat.utcrange(dtstart, dtend)
logging.debug(orgdate + " " + summary)
writer.write_org_subitem(summary)
writer.writeln(" " +orgdate)
writer.writeln(" :PROPERTIES:")
if location:
writer.writeln(" :LOCATION:" +location)
if description:
writer.writeln(" :DESCRIPTION:" +description)
writer.writeln(" :END:")
else:
logging.info("Not handling component: "+component.name)


if __name__ == "__main__":
try:
Expand Down
86 changes: 78 additions & 8 deletions common/orgformat.py
Expand Up @@ -3,6 +3,7 @@
# Time-stamp: <2011-11-02 15:13:31 aw>

import time
import calendar

class OrgFormat(object):

Expand Down Expand Up @@ -51,14 +52,52 @@ def datetime(tuple_datetime):
"""
return OrgFormat.date(tuple_datetime, show_time=True)

@staticmethod
def daterange(begin, end):
"""
returns a date range string in org format
@param begin,end: has to be a time.struct_time
"""
assert type(begin) == time.struct_time and type(end) == time.struct_time
return "%s--%s" % (OrgFormat.date(begin, False), OrgFormat.date(end, False))

@staticmethod
def datetimerange(begin, end):
"""
returns a date range string in org format
@param begin,end: has to be a time.struct_time
"""
assert type(begin) == time.struct_time and type(end) == time.struct_time
return "%s--%s" % (OrgFormat.date(begin, True), OrgFormat.date(end, True))

@staticmethod
def utcrange(begin, end):
"""
returns a date(time) range string in org format
@param begin,end: has to be a String: YYYYMMDDTHHMMSSZ or
YYYYMMDDTHHMMSST or
YYYYMMDD
"""
begin_tupel = OrgFormat.datetupelutctimestamp(begin)
end_tupel = OrgFormat.datetupelutctimestamp(end)

if begin_tupel.tm_sec == 0 and begin_tupel.tm_min == 0 and begin_tupel.tm_hour == 0 \
and end_tupel.tm_sec == 0 and end_tupel.tm_min == 0 and end_tupel.tm_hour == 0:
return OrgFormat.daterange(begin_tupel, end_tupel)
else:
return OrgFormat.datetimerange(begin_tupel, end_tupel)

@staticmethod
def strdate(date_string):
"""
returns a date string in org format
i.e.: * <YYYY-MM-DD Sun>
@param date-string: has to be a str in following format: YYYY-MM-DD
"""
assert date_string.__class__ == str
assert date_string.__class__ == str or date_string.__class__ == unicode
tuple_date = OrgFormat.datetupeliso8601(date_string)
return OrgFormat.date(tuple_date, show_time=False)

Expand All @@ -69,9 +108,9 @@ def strdatetime(datetime_string):
i.e.: * <YYYY-MM-DD Sun HH:MM>
@param date-string: has to be a str in following format: YYYY-MM-DD HH:MM
"""
assert datetime_string.__class__ == str
assert datetime_string.__class__ == str or datetime_string.__class__ == unicode
tuple_date = time.strptime(datetime_string, "%Y-%m-%d %H:%M")
return OrgFormat.date(tuple_date,show_time=True)
return OrgFormat.date(tuple_date, show_time=True)

@staticmethod
def strdatetimeiso8601(datetime_string):
Expand All @@ -81,19 +120,50 @@ def strdatetimeiso8601(datetime_string):
@param date-string: has to be a str in following format: YYYY-MM-DDTHH.MM.SS or
YYYY-MM-DDTHH.MM
"""
assert datetime_string.__class__ == str
assert datetime_string.__class__ == str or datetime_string.__class__ == unicode
tuple_date = OrgFormat.datetimetupeliso8601(datetime_string)
return OrgFormat.date(tuple_date,show_time=True)
return OrgFormat.date(tuple_date, show_time=True)

@staticmethod
def datetimetupeliso8601(datetime_string):
assert datetime_string.__class__ == str
"""
returns a time_tupel
@param datetime_string: YYYY-MM-DDTHH.MM.SS or
YYYY-MM-DDTHH.MM
"""
assert datetime_string.__class__ == str or datetime_string.__class__ == unicode
try:
return time.strptime(datetime_string, "%Y-%m-%dT%H.%M.%S")
except ValueError:
return time.strptime(datetime_string, "%Y-%m-%dT%H.%M")

@staticmethod
def datetupeliso8601(datetime_string):
assert datetime_string.__class__ == str
return time.strptime(datetime_string, "%Y-%m-%d")
"""
returns a time_tupel
@param datetime_string: YYYY-MM-DD
"""
assert datetime_string.__class__ == str or datetime_string.__class__ == unicode
return time.strptime(datetime_string, "%Y-%m-%d")

@staticmethod
def datetupelutctimestamp(datetime_string):
"""
returns a time_tupel
@param datetime_string: YYYYMMDDTHHMMSSZ or
YYYYMMDDTHHMMSST or
YYYYMMDD
"""
assert datetime_string.__class__ == str or datetime_string.__class__ == unicode
string_length = len(datetime_string)
if string_length == 16:
#YYYYMMDDTHHMMSSZ
return time.localtime(calendar.timegm(time.strptime(datetime_string, "%Y%m%dT%H%M%SZ")))
elif string_length == 15:
#YYYYMMDDTHHMMSST
return time.strptime(datetime_string, "%Y%m%dT%H%M%S")
elif string_length ==8:
#YYYYMMDD
return time.strptime(datetime_string, "%Y%m%d")
else:
raise ValueError, "string has no correct format"
7 changes: 6 additions & 1 deletion common/tests/orgformat_test.py
Expand Up @@ -51,5 +51,10 @@ def test_iso8601_datetupel(self):
self.assertEqual(2011, OrgFormat.datetupeliso8601("2011-11-30").tm_year, "datetimeiso8601 error")
self.assertEqual(11 , OrgFormat.datetupeliso8601("2011-11-30").tm_mon, "datetimeiso8601 error")
self.assertEqual(30 , OrgFormat.datetupeliso8601("2011-11-30").tm_mday, "datetimeiso8601 error")


def test_date_ranges(self):
self.assertEqual(True,False)

def test_utc_time(self):
self.assertEqual(True,False)

0 comments on commit b3c61fc

Please sign in to comment.