xmlutils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # -*- coding: utf-8; indent-tabs-mode: nil; -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2009 Guillaume Ayoub
  5. # Copyright © 2008 Nicolas Kandel
  6. # Copyright © 2008 Pascal Halter
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. XML and iCal requests manager.
  22. Note that all these functions need to receive unicode objects for full
  23. iCal requests (PUT) and string objects with charset correctly defined
  24. in them for XML requests (all but PUT).
  25. """
  26. # TODO: Manage errors (see __init__)
  27. # TODO: Manage depth and calendars/collections (see main)
  28. import xml.etree.ElementTree as ET
  29. import config
  30. import ical
  31. # TODO: This is a well-known and accepted hack for ET to avoid ET from renaming
  32. # namespaces, which is accepted in XML norm but often not in XML
  33. # readers. Is there another clean solution to force namespaces?
  34. for key,value in config.items("namespace"):
  35. ET._namespace_map[value] = key
  36. def _tag(short_name, local):
  37. """Get XML Clark notation {uri(``short_name``)}``local``."""
  38. return "{%s}%s"%(config.get("namespace", short_name), local)
  39. def delete(obj, calendar, url):
  40. """Read and answer DELETE requests.
  41. Read rfc4918-9.6 for info.
  42. """
  43. # Reading request
  44. calendar.remove(obj)
  45. # Writing answer
  46. multistatus = ET.Element(_tag("D", "multistatus"))
  47. response = ET.Element(_tag("D", "response"))
  48. multistatus.append(response)
  49. href = ET.Element(_tag("D", "href"))
  50. href.text = url
  51. response.append(href)
  52. status = ET.Element(_tag("D", "status"))
  53. status.text = config.get("status", "200")
  54. response.append(status)
  55. return ET.tostring(multistatus, config.get("encoding", "request"))
  56. def propfind(xml_request, calendar, url):
  57. """Read and answer PROPFIND requests.
  58. Read rfc4918-9.1 for info.
  59. """
  60. # Reading request
  61. root = ET.fromstring(xml_request)
  62. propElement = root.find(_tag("D", "prop"))
  63. propList = propElement.getchildren()
  64. properties = [property.tag for property in propList]
  65. # Writing answer
  66. multistatus = ET.Element(_tag("D", "multistatus"))
  67. response = ET.Element(_tag("D", "response"))
  68. multistatus.append(response)
  69. href = ET.Element(_tag("D", "href"))
  70. href.text = url
  71. response.append(href)
  72. propstat = ET.Element(_tag("D", "propstat"))
  73. response.append(propstat)
  74. prop = ET.Element(_tag("D", "prop"))
  75. propstat.append(prop)
  76. if _tag("D", "resourcetype") in properties:
  77. resourcetype = ET.Element(_tag("D", "resourcetype"))
  78. resourcetype.append(ET.Element(_tag("D", "collection")))
  79. resourcetype.append(ET.Element(_tag("C", "calendar")))
  80. prop.append(resourcetype)
  81. if _tag("D", "owner") in properties:
  82. owner = ET.Element(_tag("D", "owner"))
  83. owner.text = calendar.owner
  84. prop.append(owner)
  85. if _tag("D", "getcontenttype") in properties:
  86. getcontenttype = ET.Element(_tag("D", "getcontenttype"))
  87. getcontenttype.text = "text/calendar"
  88. prop.append(getcontenttype)
  89. if _tag("D", "getetag") in properties:
  90. getetag = ET.Element(_tag("D", "getetag"))
  91. getetag.text = calendar.etag()
  92. prop.append(getetag)
  93. if _tag("CS", "getctag") in properties:
  94. getctag = ET.Element(_tag("CS", "getctag"))
  95. getctag.text = calendar.ctag
  96. prop.append(getctag)
  97. status = ET.Element(_tag("D", "status"))
  98. status.text = config.get("status", "200")
  99. propstat.append(status)
  100. return ET.tostring(multistatus, config.get("encoding", "request"))
  101. def put(icalRequest, calendar, url, obj):
  102. """Read PUT requests."""
  103. if obj:
  104. # PUT is modifying obj
  105. calendar.replace(obj, icalRequest)
  106. else:
  107. # PUT is adding a new object
  108. calendar.append(icalRequest)
  109. def report(xml_request, calendar, url):
  110. """Read and answer REPORT requests.
  111. Read rfc3253-3.6 for info.
  112. """
  113. # Reading request
  114. root = ET.fromstring(xml_request)
  115. propElement = root.find(_tag("D", "prop"))
  116. propList = propElement.getchildren()
  117. properties = [property.tag for property in propList]
  118. filters = {}
  119. filterElement = root.find(_tag("C", "filter"))
  120. filterList = propElement.getchildren()
  121. # TODO: This should be recursive
  122. # TODO: Really manage filters (see ical)
  123. for filter in filterList:
  124. sub = filters[filter.get("name")] = {}
  125. for subfilter in filter.getchildren():
  126. sub[subfilter.get("name")] = {}
  127. if root.tag == _tag("C", "calendar-multiget"):
  128. # Read rfc4791-7.9 for info
  129. hreferences = set([hrefElement.text for hrefElement in root.findall(_tag("D", "href"))])
  130. else:
  131. hreferences = [url]
  132. # Writing answer
  133. multistatus = ET.Element(_tag("D", "multistatus"))
  134. # TODO: WTF, sunbird needs one response by object,
  135. # is that really what is needed?
  136. # Read rfc4791-9.[6|10] for info
  137. for hreference in hreferences:
  138. headers = ical.headers(calendar.vcalendar())
  139. # TODO: Define timezones by obj
  140. timezones = ical.timezones(calendar.vcalendar())
  141. objects = []
  142. objects.extend(ical.events(calendar.vcalendar()))
  143. objects.extend(ical.todos(calendar.vcalendar()))
  144. if not objects:
  145. # TODO: Read rfc4791-9.[6|10] to find a right answer
  146. response = ET.Element(_tag("D", "response"))
  147. multistatus.append(response)
  148. href = ET.Element(_tag("D", "href"))
  149. href.text = url
  150. response.append(href)
  151. status = ET.Element(_tag("D", "status"))
  152. status.text = config.get("status", "204")
  153. response.append(status)
  154. for obj in objects:
  155. # TODO: Use the hreference to read data and create href.text
  156. # We assume here that hreference is url
  157. response = ET.Element(_tag("D", "response"))
  158. multistatus.append(response)
  159. href = ET.Element(_tag("D", "href"))
  160. href.text = url
  161. response.append(href)
  162. propstat = ET.Element(_tag("D", "propstat"))
  163. response.append(propstat)
  164. prop = ET.Element(_tag("D", "prop"))
  165. propstat.append(prop)
  166. if _tag("D", "getetag") in properties:
  167. # TODO: Can UID and ETAG be the same?
  168. getetag = ET.Element(_tag("D", "getetag"))
  169. getetag.text = obj.etag()
  170. prop.append(getetag)
  171. if _tag("C", "calendar-data") in properties:
  172. cdata = ET.Element(_tag("C", "calendar-data"))
  173. # TODO: Maybe assume that events and todos are not the same
  174. cdata.text = ical.write_calendar(headers, timezones, [obj])
  175. prop.append(cdata)
  176. status = ET.Element(_tag("D", "status"))
  177. status.text = config.get("status", "200")
  178. propstat.append(status)
  179. return ET.tostring(multistatus, config.get("encoding", "request"))