/gdata/calendar_resource/data.py

http://radioappz.googlecode.com/ · Python · 202 lines · 67 code · 42 blank · 93 comment · 9 complexity · 7630148003132f2ffabc41557f74c8f6 MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright 2009 Google Inc. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Data model classes for parsing and generating XML for the Calendar
  17. Resource API."""
  18. __author__ = 'Vic Fryzel <vf@google.com>'
  19. import atom.core
  20. import atom.data
  21. import gdata.apps
  22. import gdata.data
  23. # This is required to work around a naming conflict between the Google
  24. # Spreadsheets API and Python's built-in property function
  25. pyproperty = property
  26. # The apps:property name of the resourceId property
  27. RESOURCE_ID_NAME = 'resourceId'
  28. # The apps:property name of the resourceCommonName property
  29. RESOURCE_COMMON_NAME_NAME = 'resourceCommonName'
  30. # The apps:property name of the resourceDescription property
  31. RESOURCE_DESCRIPTION_NAME = 'resourceDescription'
  32. # The apps:property name of the resourceType property
  33. RESOURCE_TYPE_NAME = 'resourceType'
  34. class AppsProperty(atom.core.XmlElement):
  35. """Represents an <apps:property> element in a Calendar Resource feed."""
  36. _qname = gdata.apps.APPS_TEMPLATE % 'property'
  37. name = 'name'
  38. value = 'value'
  39. class CalendarResourceEntry(gdata.data.GDEntry):
  40. """Represents a Calendar Resource entry in object form."""
  41. property = [AppsProperty]
  42. def _GetProperty(self, name):
  43. """Get the apps:property value with the given name.
  44. Args:
  45. name: string Name of the apps:property value to get.
  46. Returns:
  47. The apps:property value with the given name, or None if the name was
  48. invalid.
  49. """
  50. value = None
  51. for p in self.property:
  52. if p.name == name:
  53. value = p.value
  54. break
  55. return value
  56. def _SetProperty(self, name, value):
  57. """Set the apps:property value with the given name to the given value.
  58. Args:
  59. name: string Name of the apps:property value to set.
  60. value: string Value to give the apps:property value with the given name.
  61. """
  62. found = False
  63. for i in range(len(self.property)):
  64. if self.property[i].name == name:
  65. self.property[i].value = value
  66. found = True
  67. break
  68. if not found:
  69. self.property.append(AppsProperty(name=name, value=value))
  70. def GetResourceId(self):
  71. """Get the resource ID of this Calendar Resource object.
  72. Returns:
  73. The resource ID of this Calendar Resource object as a string or None.
  74. """
  75. return self._GetProperty(RESOURCE_ID_NAME)
  76. def SetResourceId(self, value):
  77. """Set the resource ID of this Calendar Resource object.
  78. Args:
  79. value: string The new resource ID value to give this object.
  80. """
  81. self._SetProperty(RESOURCE_ID_NAME, value)
  82. resource_id = pyproperty(GetResourceId, SetResourceId)
  83. def GetResourceCommonName(self):
  84. """Get the common name of this Calendar Resource object.
  85. Returns:
  86. The common name of this Calendar Resource object as a string or None.
  87. """
  88. return self._GetProperty(RESOURCE_COMMON_NAME_NAME)
  89. def SetResourceCommonName(self, value):
  90. """Set the common name of this Calendar Resource object.
  91. Args:
  92. value: string The new common name value to give this object.
  93. """
  94. self._SetProperty(RESOURCE_COMMON_NAME_NAME, value)
  95. resource_common_name = pyproperty(GetResourceCommonName,
  96. SetResourceCommonName)
  97. def GetResourceDescription(self):
  98. """Get the description of this Calendar Resource object.
  99. Returns:
  100. The description of this Calendar Resource object as a string or None.
  101. """
  102. return self._GetProperty(RESOURCE_DESCRIPTION_NAME)
  103. def SetResourceDescription(self, value):
  104. """Set the description of this Calendar Resource object.
  105. Args:
  106. value: string The new description value to give this object.
  107. """
  108. self._SetProperty(RESOURCE_DESCRIPTION_NAME, value)
  109. resource_description = pyproperty(GetResourceDescription,
  110. SetResourceDescription)
  111. def GetResourceType(self):
  112. """Get the type of this Calendar Resource object.
  113. Returns:
  114. The type of this Calendar Resource object as a string or None.
  115. """
  116. return self._GetProperty(RESOURCE_TYPE_NAME)
  117. def SetResourceType(self, value):
  118. """Set the type value of this Calendar Resource object.
  119. Args:
  120. value: string The new type value to give this object.
  121. """
  122. self._SetProperty(RESOURCE_TYPE_NAME, value)
  123. resource_type = pyproperty(GetResourceType, SetResourceType)
  124. def __init__(self, resource_id=None, resource_common_name=None,
  125. resource_description=None, resource_type=None, *args, **kwargs):
  126. """Constructs a new CalendarResourceEntry object with the given arguments.
  127. Args:
  128. resource_id: string (optional) The resource ID to give this new object.
  129. resource_common_name: string (optional) The common name to give this new
  130. object.
  131. resource_description: string (optional) The description to give this new
  132. object.
  133. resource_type: string (optional) The type to give this new object.
  134. args: The other parameters to pass to gdata.entry.GDEntry constructor.
  135. kwargs: The other parameters to pass to gdata.entry.GDEntry constructor.
  136. """
  137. super(CalendarResourceEntry, self).__init__(*args, **kwargs)
  138. if resource_id:
  139. self.resource_id = resource_id
  140. if resource_common_name:
  141. self.resource_common_name = resource_common_name
  142. if resource_description:
  143. self.resource_description = resource_description
  144. if resource_type:
  145. self.resource_type = resource_type
  146. class CalendarResourceFeed(gdata.data.GDFeed):
  147. """Represents a feed of CalendarResourceEntry objects."""
  148. entry = [CalendarResourceEntry]