xmlutils.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2010 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. from radicale import client, config, ical
  30. # TODO: This is a well-known and accepted hack for ET to avoid ET from renaming
  31. # namespaces, which is accepted in XML norm but often not in XML
  32. # readers. Is there another clean solution to force namespaces?
  33. for key, value in config.items("namespace"):
  34. ET._namespace_map[value] = key
  35. def _tag(short_name, local):
  36. """Get XML Clark notation {uri(``short_name``)}``local``."""
  37. return "{%s}%s" % (config.get("namespace", short_name), local)
  38. def _response(code):
  39. """Return full W3C names from HTTP status codes."""
  40. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  41. def delete(obj, calendar, url):
  42. """Read and answer DELETE requests.
  43. Read rfc4918-9.6 for info.
  44. """
  45. # Reading request
  46. calendar.remove(obj)
  47. # Writing answer
  48. multistatus = ET.Element(_tag("D", "multistatus"))
  49. response = ET.Element(_tag("D", "response"))
  50. multistatus.append(response)
  51. href = ET.Element(_tag("D", "href"))
  52. href.text = url
  53. response.append(href)
  54. status = ET.Element(_tag("D", "status"))
  55. status.text = _response(200)
  56. response.append(status)
  57. return ET.tostring(multistatus, config.get("encoding", "request"))
  58. def propfind(xml_request, calendar, url):
  59. """Read and answer PROPFIND requests.
  60. Read rfc4918-9.1 for info.
  61. """
  62. # Reading request
  63. root = ET.fromstring(xml_request)
  64. propElement = root.find(_tag("D", "prop"))
  65. propList = propElement.getchildren()
  66. properties = [property.tag for property in propList]
  67. # Writing answer
  68. multistatus = ET.Element(_tag("D", "multistatus"))
  69. response = ET.Element(_tag("D", "response"))
  70. multistatus.append(response)
  71. href = ET.Element(_tag("D", "href"))
  72. href.text = url
  73. response.append(href)
  74. propstat = ET.Element(_tag("D", "propstat"))
  75. response.append(propstat)
  76. prop = ET.Element(_tag("D", "prop"))
  77. propstat.append(prop)
  78. if _tag("D", "resourcetype") in properties:
  79. resourcetype = ET.Element(_tag("D", "resourcetype"))
  80. resourcetype.append(ET.Element(_tag("C", "calendar")))
  81. prop.append(resourcetype)
  82. if _tag("D", "owner") in properties:
  83. owner = ET.Element(_tag("D", "owner"))
  84. owner.text = calendar.owner
  85. prop.append(owner)
  86. if _tag("D", "getcontenttype") in properties:
  87. getcontenttype = ET.Element(_tag("D", "getcontenttype"))
  88. getcontenttype.text = "text/calendar"
  89. prop.append(getcontenttype)
  90. if _tag("D", "getetag") in properties:
  91. getetag = ET.Element(_tag("D", "getetag"))
  92. getetag.text = calendar.etag()
  93. prop.append(getetag)
  94. if _tag("CS", "getctag") in properties:
  95. getctag = ET.Element(_tag("CS", "getctag"))
  96. getctag.text = calendar.ctag
  97. prop.append(getctag)
  98. status = ET.Element(_tag("D", "status"))
  99. status.text = _response(200)
  100. propstat.append(status)
  101. return ET.tostring(multistatus, config.get("encoding", "request"))
  102. def put(icalRequest, calendar, url, obj):
  103. """Read PUT requests."""
  104. if obj:
  105. # PUT is modifying obj
  106. calendar.replace(obj, icalRequest)
  107. else:
  108. # PUT is adding a new object
  109. calendar.append(icalRequest)
  110. def report(xml_request, calendar, url):
  111. """Read and answer REPORT requests.
  112. Read rfc3253-3.6 for info.
  113. """
  114. # Reading request
  115. root = ET.fromstring(xml_request)
  116. propElement = root.find(_tag("D", "prop"))
  117. propList = propElement.getchildren()
  118. properties = [property.tag for property in propList]
  119. filters = {}
  120. filterElement = root.find(_tag("C", "filter"))
  121. filterList = propElement.getchildren()
  122. # TODO: This should be recursive
  123. # TODO: Really manage filters (see ical)
  124. for filter in filterList:
  125. sub = filters[filter.get("name")] = {}
  126. for subfilter in filter.getchildren():
  127. sub[subfilter.get("name")] = {}
  128. if root.tag == _tag("C", "calendar-multiget"):
  129. # Read rfc4791-7.9 for info
  130. hreferences = set([hrefElement.text for hrefElement in root.findall(_tag("D", "href"))])
  131. else:
  132. hreferences = [url]
  133. # Writing answer
  134. multistatus = ET.Element(_tag("D", "multistatus"))
  135. # TODO: WTF, sunbird needs one response by object,
  136. # is that really what is needed?
  137. # Read rfc4791-9.[6|10] for info
  138. for hreference in hreferences:
  139. headers = ical.headers(calendar.vcalendar())
  140. # TODO: Define timezones by obj
  141. timezones = ical.timezones(calendar.vcalendar())
  142. objects = []
  143. objects.extend(ical.events(calendar.vcalendar()))
  144. objects.extend(ical.todos(calendar.vcalendar()))
  145. if not objects:
  146. # TODO: Read rfc4791-9.[6|10] to find a right answer
  147. response = ET.Element(_tag("D", "response"))
  148. multistatus.append(response)
  149. href = ET.Element(_tag("D", "href"))
  150. href.text = url
  151. response.append(href)
  152. status = ET.Element(_tag("D", "status"))
  153. status.text = _response(204)
  154. response.append(status)
  155. for obj in objects:
  156. # TODO: Use the hreference to read data and create href.text
  157. # We assume here that hreference is url
  158. response = ET.Element(_tag("D", "response"))
  159. multistatus.append(response)
  160. href = ET.Element(_tag("D", "href"))
  161. href.text = url
  162. response.append(href)
  163. propstat = ET.Element(_tag("D", "propstat"))
  164. response.append(propstat)
  165. prop = ET.Element(_tag("D", "prop"))
  166. propstat.append(prop)
  167. if _tag("D", "getetag") in properties:
  168. # TODO: Can UID and ETAG be the same?
  169. getetag = ET.Element(_tag("D", "getetag"))
  170. getetag.text = obj.etag()
  171. prop.append(getetag)
  172. if _tag("C", "calendar-data") in properties:
  173. cdata = ET.Element(_tag("C", "calendar-data"))
  174. # TODO: Maybe assume that events and todos are not the same
  175. cdata.text = ical.write_calendar(headers, timezones, [obj])
  176. prop.append(cdata)
  177. status = ET.Element(_tag("D", "status"))
  178. status.text = _response(200)
  179. propstat.append(status)
  180. return ET.tostring(multistatus, config.get("encoding", "request"))