/gdata/health/__init__.py

http://radioappz.googlecode.com/ · Python · 229 lines · 98 code · 40 blank · 91 comment · 6 complexity · e8ae222abe00f5f2498bfeefa1005af3 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. """Contains extensions to Atom objects used with Google Health."""
  17. __author__ = 'api.eric@google.com (Eric Bidelman)'
  18. import atom
  19. import gdata
  20. CCR_NAMESPACE = 'urn:astm-org:CCR'
  21. METADATA_NAMESPACE = 'http://schemas.google.com/health/metadata'
  22. class Ccr(atom.AtomBase):
  23. """Represents a Google Health <ContinuityOfCareRecord>."""
  24. _tag = 'ContinuityOfCareRecord'
  25. _namespace = CCR_NAMESPACE
  26. _children = atom.AtomBase._children.copy()
  27. def __init__(self, extension_elements=None,
  28. extension_attributes=None, text=None):
  29. atom.AtomBase.__init__(self, extension_elements=extension_elements,
  30. extension_attributes=extension_attributes, text=text)
  31. def GetAlerts(self):
  32. """Helper for extracting Alert/Allergy data from the CCR.
  33. Returns:
  34. A list of ExtensionElements (one for each allergy found) or None if
  35. no allergies where found in this CCR.
  36. """
  37. try:
  38. body = self.FindExtensions('Body')[0]
  39. return body.FindChildren('Alerts')[0].FindChildren('Alert')
  40. except:
  41. return None
  42. def GetAllergies(self):
  43. """Alias for GetAlerts()."""
  44. return self.GetAlerts()
  45. def GetProblems(self):
  46. """Helper for extracting Problem/Condition data from the CCR.
  47. Returns:
  48. A list of ExtensionElements (one for each problem found) or None if
  49. no problems where found in this CCR.
  50. """
  51. try:
  52. body = self.FindExtensions('Body')[0]
  53. return body.FindChildren('Problems')[0].FindChildren('Problem')
  54. except:
  55. return None
  56. def GetConditions(self):
  57. """Alias for GetProblems()."""
  58. return self.GetProblems()
  59. def GetProcedures(self):
  60. """Helper for extracting Procedure data from the CCR.
  61. Returns:
  62. A list of ExtensionElements (one for each procedure found) or None if
  63. no procedures where found in this CCR.
  64. """
  65. try:
  66. body = self.FindExtensions('Body')[0]
  67. return body.FindChildren('Procedures')[0].FindChildren('Procedure')
  68. except:
  69. return None
  70. def GetImmunizations(self):
  71. """Helper for extracting Immunization data from the CCR.
  72. Returns:
  73. A list of ExtensionElements (one for each immunization found) or None if
  74. no immunizations where found in this CCR.
  75. """
  76. try:
  77. body = self.FindExtensions('Body')[0]
  78. return body.FindChildren('Immunizations')[0].FindChildren('Immunization')
  79. except:
  80. return None
  81. def GetMedications(self):
  82. """Helper for extracting Medication data from the CCR.
  83. Returns:
  84. A list of ExtensionElements (one for each medication found) or None if
  85. no medications where found in this CCR.
  86. """
  87. try:
  88. body = self.FindExtensions('Body')[0]
  89. return body.FindChildren('Medications')[0].FindChildren('Medication')
  90. except:
  91. return None
  92. def GetResults(self):
  93. """Helper for extracting Results/Labresults data from the CCR.
  94. Returns:
  95. A list of ExtensionElements (one for each result found) or None if
  96. no results where found in this CCR.
  97. """
  98. try:
  99. body = self.FindExtensions('Body')[0]
  100. return body.FindChildren('Results')[0].FindChildren('Result')
  101. except:
  102. return None
  103. class ProfileEntry(gdata.GDataEntry):
  104. """The Google Health version of an Atom Entry."""
  105. _tag = gdata.GDataEntry._tag
  106. _namespace = atom.ATOM_NAMESPACE
  107. _children = gdata.GDataEntry._children.copy()
  108. _attributes = gdata.GDataEntry._attributes.copy()
  109. _children['{%s}ContinuityOfCareRecord' % CCR_NAMESPACE] = ('ccr', Ccr)
  110. def __init__(self, ccr=None, author=None, category=None, content=None,
  111. atom_id=None, link=None, published=None, title=None,
  112. updated=None, text=None, extension_elements=None,
  113. extension_attributes=None):
  114. self.ccr = ccr
  115. gdata.GDataEntry.__init__(
  116. self, author=author, category=category, content=content,
  117. atom_id=atom_id, link=link, published=published, title=title,
  118. updated=updated, extension_elements=extension_elements,
  119. extension_attributes=extension_attributes, text=text)
  120. class ProfileFeed(gdata.GDataFeed):
  121. """A feed containing a list of Google Health profile entries."""
  122. _tag = gdata.GDataFeed._tag
  123. _namespace = atom.ATOM_NAMESPACE
  124. _children = gdata.GDataFeed._children.copy()
  125. _attributes = gdata.GDataFeed._attributes.copy()
  126. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [ProfileEntry])
  127. class ProfileListEntry(gdata.GDataEntry):
  128. """The Atom Entry in the Google Health profile list feed."""
  129. _tag = gdata.GDataEntry._tag
  130. _namespace = atom.ATOM_NAMESPACE
  131. _children = gdata.GDataEntry._children.copy()
  132. _attributes = gdata.GDataEntry._attributes.copy()
  133. def GetProfileId(self):
  134. return self.content.text
  135. def GetProfileName(self):
  136. return self.title.text
  137. class ProfileListFeed(gdata.GDataFeed):
  138. """A feed containing a list of Google Health profile list entries."""
  139. _tag = gdata.GDataFeed._tag
  140. _namespace = atom.ATOM_NAMESPACE
  141. _children = gdata.GDataFeed._children.copy()
  142. _attributes = gdata.GDataFeed._attributes.copy()
  143. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [ProfileListEntry])
  144. def ProfileEntryFromString(xml_string):
  145. """Converts an XML string into a ProfileEntry object.
  146. Args:
  147. xml_string: string The XML describing a Health profile feed entry.
  148. Returns:
  149. A ProfileEntry object corresponding to the given XML.
  150. """
  151. return atom.CreateClassFromXMLString(ProfileEntry, xml_string)
  152. def ProfileListEntryFromString(xml_string):
  153. """Converts an XML string into a ProfileListEntry object.
  154. Args:
  155. xml_string: string The XML describing a Health profile list feed entry.
  156. Returns:
  157. A ProfileListEntry object corresponding to the given XML.
  158. """
  159. return atom.CreateClassFromXMLString(ProfileListEntry, xml_string)
  160. def ProfileFeedFromString(xml_string):
  161. """Converts an XML string into a ProfileFeed object.
  162. Args:
  163. xml_string: string The XML describing a ProfileFeed feed.
  164. Returns:
  165. A ProfileFeed object corresponding to the given XML.
  166. """
  167. return atom.CreateClassFromXMLString(ProfileFeed, xml_string)
  168. def ProfileListFeedFromString(xml_string):
  169. """Converts an XML string into a ProfileListFeed object.
  170. Args:
  171. xml_string: string The XML describing a ProfileListFeed feed.
  172. Returns:
  173. A ProfileListFeed object corresponding to the given XML.
  174. """
  175. return atom.CreateClassFromXMLString(ProfileListFeed, xml_string)