PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/health/__init__.py

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