Factorize code and remove encoding bugs
git-svn-id: http://svn.32rwr.info/radicale/trunk@8 74e4794c-479d-4a33-9dda-c6c359d70f12
This commit is contained in:
parent
34d1d30998
commit
e7a5ef8c5d
@ -20,6 +20,8 @@
|
||||
# TODO: Manage depth and calendars/collections (see xmlutils)
|
||||
# TODO: Manage smart and configurable logs
|
||||
# TODO: Manage authentication
|
||||
# TODO: Magage command-line options
|
||||
# TODO: Forget twisted?
|
||||
|
||||
import sys
|
||||
from twisted.web import server
|
||||
|
@ -17,7 +17,6 @@
|
||||
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# TODO: Manage filters (see xmlutils)
|
||||
# TODO: Factorize code
|
||||
|
||||
import calendar
|
||||
|
||||
@ -28,37 +27,14 @@ def writeCalendar(headers=[calendar.Header("PRODID:-//The Radicale Team//NONSGML
|
||||
Create calendar from headers, timezones, todos, events
|
||||
"""
|
||||
# TODO: Manage encoding and EOL
|
||||
cal = "\n".join((
|
||||
"BEGIN:VCALENDAR",
|
||||
"\n".join([header.text for header in headers]),
|
||||
"\n".join([timezone.text for timezone in timezones]),
|
||||
"\n".join([todo.text for todo in todos]),
|
||||
"\n".join([event.text for event in events]),
|
||||
"END:VCALENDAR"))
|
||||
return "\n".join([line for line in cal.splitlines() if line])
|
||||
|
||||
def events(vcalendar):
|
||||
"""
|
||||
Find VEVENT Items in vcalendar
|
||||
"""
|
||||
events = []
|
||||
|
||||
lines = vcalendar.splitlines()
|
||||
inEvent = False
|
||||
eventLines = []
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("BEGIN:VEVENT"):
|
||||
inEvent = True
|
||||
eventLines = []
|
||||
|
||||
if inEvent:
|
||||
# TODO: Manage encoding
|
||||
eventLines.append(line)
|
||||
if line.startswith("END:VEVENT"):
|
||||
events.append(calendar.Event("\n".join(eventLines)))
|
||||
|
||||
return events
|
||||
cal = u"\n".join((
|
||||
u"BEGIN:VCALENDAR",
|
||||
u"\n".join([header.text for header in headers]),
|
||||
u"\n".join([timezone.text for timezone in timezones]),
|
||||
u"\n".join([todo.text for todo in todos]),
|
||||
u"\n".join([event.text for event in events]),
|
||||
u"END:VCALENDAR"))
|
||||
return u"\n".join([line for line in cal.splitlines() if line])
|
||||
|
||||
def headers(vcalendar):
|
||||
"""
|
||||
@ -76,47 +52,26 @@ def headers(vcalendar):
|
||||
|
||||
return headers
|
||||
|
||||
def timezones(vcalendar):
|
||||
"""
|
||||
Find VTIMEZONE Items in vcalendar
|
||||
"""
|
||||
timezones = []
|
||||
def _parse(vcalendar, tag, obj):
|
||||
items = []
|
||||
|
||||
lines = vcalendar.splitlines()
|
||||
inTz = False
|
||||
tzLines = []
|
||||
inItem = False
|
||||
itemLines = []
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("BEGIN:VTIMEZONE"):
|
||||
inTz = True
|
||||
tzLines = []
|
||||
if line.startswith("BEGIN:%s" % tag):
|
||||
inItem = True
|
||||
itemLines = []
|
||||
|
||||
if inTz:
|
||||
tzLines.append(line)
|
||||
if line.startswith("END:VTIMEZONE"):
|
||||
timezones.append(calendar.Timezone("\n".join(tzLines)))
|
||||
|
||||
return timezones
|
||||
|
||||
def todos(vcalendar):
|
||||
"""
|
||||
Find VTODO Items in vcalendar
|
||||
"""
|
||||
todos = []
|
||||
|
||||
lines = vcalendar.splitlines()
|
||||
inTodo = False
|
||||
todoLines = []
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("BEGIN:VTODO"):
|
||||
inTodo = True
|
||||
todoLines = []
|
||||
|
||||
if inTodo:
|
||||
if inItem:
|
||||
# TODO: Manage encoding
|
||||
todoLines.append(line)
|
||||
if line.startswith("END:VTODO"):
|
||||
todos.append(calendar.Todo("\n".join(todoLines)))
|
||||
itemLines.append(line)
|
||||
if line.startswith("END:%s" % tag):
|
||||
items.append(obj("\n".join(itemLines)))
|
||||
|
||||
return todos
|
||||
return items
|
||||
|
||||
events = lambda vcalendar: _parse(vcalendar, "VEVENT", calendar.Event)
|
||||
todos = lambda vcalendar: _parse(vcalendar, "VTODO", calendar.Todo)
|
||||
timezones = lambda vcalendar: _parse(vcalendar, "VTIMEZONE", calendar.Timezone)
|
||||
|
@ -44,7 +44,7 @@ def mkcalendar(name):
|
||||
if not os.path.exists(os.path.join(_folder, user)):
|
||||
os.makedirs(os.path.join(_folder, user))
|
||||
fd = open(os.path.join(_folder, user, cal), "w")
|
||||
fd.write(ical.writeCalendar())
|
||||
fd.write(ical.writeCalendar().encode(config.get("encoding", "stock")))
|
||||
|
||||
def read(cal):
|
||||
"""
|
||||
@ -77,7 +77,7 @@ def append(cal, vcalendar):
|
||||
fd.close()
|
||||
|
||||
for i,line in enumerate(tz.text.splitlines()):
|
||||
lines.insert(2+i, line.encode("utf-8")+"\n")
|
||||
lines.insert(2+i, line.encode(config.get("encoding", "stock"))+"\n")
|
||||
|
||||
fd = open(path, "w")
|
||||
fd.writelines(lines)
|
||||
@ -91,7 +91,7 @@ def append(cal, vcalendar):
|
||||
fd.close()
|
||||
|
||||
for line in obj.text.splitlines():
|
||||
lines.insert(-1, line.encode("utf-8")+"\n")
|
||||
lines.insert(-1, line.encode(config.get("encoding", "stock"))+"\n")
|
||||
|
||||
fd = open(path, "w")
|
||||
fd.writelines(lines)
|
||||
@ -111,7 +111,7 @@ def remove(cal, etag):
|
||||
events = [event for event in ical.events(cal) if event.etag() != etag]
|
||||
|
||||
fd = open(path, "w")
|
||||
fd.write(ical.writeCalendar(headers, timezones, todos, events))
|
||||
fd.write(ical.writeCalendar(headers, timezones, todos, events).encode(config.get("encoding", "stock")))
|
||||
fd.close()
|
||||
|
||||
if config.get("support", "defaultCalendar"):
|
||||
|
Loading…
Reference in New Issue
Block a user