/gdata/analytics/data.py

http://radioappz.googlecode.com/ · Python · 273 lines · 121 code · 70 blank · 82 comment · 7 complexity · 19ff534033efc2067a235ccd205da35f 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
  17. Google Analytics Data Export API."""
  18. __author__ = 'api.nickm@google.com (Nick Mihailovski)'
  19. import gdata.data
  20. import atom.core
  21. # XML Namespace used in Google Analytics API entities.
  22. DXP_NS = '{http://schemas.google.com/analytics/2009}%s'
  23. GA_NS = '{http://schemas.google.com/ga/2009}%s'
  24. class GetProperty(object):
  25. """Utility class to simplify retrieving Property objects."""
  26. def get_property(self, name):
  27. """Helper method to return a propery object by its name attribute.
  28. Args:
  29. name: string The name of the <dxp:property> element to retrieve.
  30. Returns:
  31. A property object corresponding to the matching <dxp:property> element.
  32. if no property is found, None is returned.
  33. """
  34. for prop in self.property:
  35. if prop.name == name:
  36. return prop
  37. return None
  38. GetProperty = get_property
  39. class GetMetric(object):
  40. """Utility class to simplify retrieving Metric objects."""
  41. def get_metric(self, name):
  42. """Helper method to return a propery value by its name attribute
  43. Args:
  44. name: string The name of the <dxp:metric> element to retrieve.
  45. Returns:
  46. A property object corresponding to the matching <dxp:metric> element.
  47. if no property is found, None is returned.
  48. """
  49. for met in self.metric:
  50. if met.name == name:
  51. return met
  52. return None
  53. GetMetric = get_metric
  54. class GetDimension(object):
  55. """Utility class to simplify retrieving Dimension objects."""
  56. def get_dimension(self, name):
  57. """Helper method to return a dimention object by its name attribute
  58. Args:
  59. name: string The name of the <dxp:dimension> element to retrieve.
  60. Returns:
  61. A dimension object corresponding to the matching <dxp:dimension> element.
  62. if no dimension is found, None is returned.
  63. """
  64. for dim in self.dimension:
  65. if dim.name == name:
  66. return dim
  67. return None
  68. GetDimension = get_dimension
  69. class StartDate(atom.core.XmlElement):
  70. """Analytics Feed <dxp:startDate>"""
  71. _qname = DXP_NS % 'startDate'
  72. class EndDate(atom.core.XmlElement):
  73. """Analytics Feed <dxp:endDate>"""
  74. _qname = DXP_NS % 'endDate'
  75. class Metric(atom.core.XmlElement):
  76. """Analytics Feed <dxp:metric>"""
  77. _qname = DXP_NS % 'metric'
  78. name = 'name'
  79. type = 'type'
  80. value = 'value'
  81. confidence_interval = 'confidenceInterval'
  82. class Aggregates(atom.core.XmlElement, GetMetric):
  83. """Analytics Data Feed <dxp:aggregates>"""
  84. _qname = DXP_NS % 'aggregates'
  85. metric = [Metric]
  86. class TableId(atom.core.XmlElement):
  87. """Analytics Feed <dxp:tableId>"""
  88. _qname = DXP_NS % 'tableId'
  89. class TableName(atom.core.XmlElement):
  90. """Analytics Feed <dxp:tableName>"""
  91. _qname = DXP_NS % 'tableName'
  92. class Property(atom.core.XmlElement):
  93. """Analytics Feed <dxp:property>"""
  94. _qname = DXP_NS % 'property'
  95. name = 'name'
  96. value = 'value'
  97. class Definition(atom.core.XmlElement):
  98. """Analytics Feed <dxp:definition>"""
  99. _qname = DXP_NS % 'definition'
  100. class Segment(atom.core.XmlElement):
  101. """Analytics Feed <dxp:segment>"""
  102. _qname = DXP_NS % 'segment'
  103. id = 'id'
  104. name = 'name'
  105. definition = Definition
  106. class Engagement(atom.core.XmlElement):
  107. """Analytics Feed <dxp:engagement>"""
  108. _qname = GA_NS % 'engagement'
  109. type = 'type'
  110. comparison = 'comparison'
  111. threshold_value = 'thresholdValue'
  112. class Step(atom.core.XmlElement):
  113. """Analytics Feed <dxp:step>"""
  114. _qname = GA_NS % 'step'
  115. number = 'number'
  116. name = 'name'
  117. path = 'path'
  118. class Destination(atom.core.XmlElement):
  119. """Analytics Feed <dxp:destination>"""
  120. _qname = GA_NS % 'destination'
  121. step = [Step]
  122. expression = 'expression'
  123. case_sensitive = 'caseSensitive'
  124. match_type = 'matchType'
  125. step1_required = 'step1Required'
  126. class Goal(atom.core.XmlElement):
  127. """Analytics Feed <dxp:goal>"""
  128. _qname = GA_NS % 'goal'
  129. destination = Destination
  130. engagement = Engagement
  131. number = 'number'
  132. name = 'name'
  133. value = 'value'
  134. active = 'active'
  135. class CustomVariable(atom.core.XmlElement):
  136. """Analytics Data Feed <dxp:customVariable>"""
  137. _qname = GA_NS % 'customVariable'
  138. index = 'index'
  139. name = 'name'
  140. scope = 'scope'
  141. class DataSource(atom.core.XmlElement, GetProperty):
  142. """Analytics Data Feed <dxp:dataSource>"""
  143. _qname = DXP_NS % 'dataSource'
  144. table_id = TableId
  145. table_name = TableName
  146. property = [Property]
  147. class Dimension(atom.core.XmlElement):
  148. """Analytics Feed <dxp:dimension>"""
  149. _qname = DXP_NS % 'dimension'
  150. name = 'name'
  151. value = 'value'
  152. # Account Feed.
  153. class AccountEntry(gdata.data.GDEntry, GetProperty):
  154. """Analytics Account Feed <entry>"""
  155. _qname = atom.data.ATOM_TEMPLATE % 'entry'
  156. table_id = TableId
  157. property = [Property]
  158. goal = [Goal]
  159. custom_variable = [CustomVariable]
  160. class AccountFeed(gdata.data.GDFeed):
  161. """Analytics Account Feed <feed>"""
  162. _qname = atom.data.ATOM_TEMPLATE % 'feed'
  163. segment = [Segment]
  164. entry = [AccountEntry]
  165. # Data Feed.
  166. class DataEntry(gdata.data.GDEntry, GetMetric, GetDimension):
  167. """Analytics Data Feed <entry>"""
  168. _qname = atom.data.ATOM_TEMPLATE % 'entry'
  169. dimension = [Dimension]
  170. metric = [Metric]
  171. def get_object(self, name):
  172. """Returns either a Dimension or Metric object with the same name as the
  173. name parameter.
  174. Args:
  175. name: string The name of the object to retrieve.
  176. Returns:
  177. Either a Dimension or Object that has the same as the name parameter.
  178. """
  179. output = self.GetDimension(name)
  180. if not output:
  181. output = self.GetMetric(name)
  182. return output
  183. GetObject = get_object
  184. class DataFeed(gdata.data.GDFeed):
  185. """Analytics Data Feed <feed>. Althrough there is only one datasource, it is
  186. stored in an array to replicate the design of the Java client library and
  187. ensure backwards compatibility if new data sources are added in the future.
  188. """
  189. _qname = atom.data.ATOM_TEMPLATE % 'feed'
  190. start_date = StartDate
  191. end_date = EndDate
  192. aggregates = Aggregates
  193. data_source = [DataSource]
  194. entry = [DataEntry]
  195. segment = Segment