PageRenderTime 18ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/maps/data.py

http://radioappz.googlecode.com/
Python | 125 lines | 77 code | 24 blank | 24 comment | 7 complexity | bb90f3794ab326566b3c99e9c0f58a10 MD5 | raw file
  1#!/usr/bin/env python
  2#
  3# Copyright (C) 2009 Google Inc.
  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
 18"""Data model classes for parsing and generating XML for the Maps Data API."""
 19
 20
 21__author__ = 'api.roman.public@google.com (Roman Nurik)'
 22
 23
 24import re
 25import atom.core
 26import gdata.data
 27
 28
 29MAP_ATOM_ID_PATTERN = re.compile('/maps/feeds/maps/'
 30                                 '(?P<user_id>\w+)/'
 31                                 '(?P<map_id>\w+)$')
 32
 33FEATURE_ATOM_ID_PATTERN = re.compile('/maps/feeds/features/'
 34                                     '(?P<user_id>\w+)/'
 35                                     '(?P<map_id>\w+)/'
 36                                     '(?P<feature_id>\w+)$')
 37
 38# The KML mime type
 39KML_CONTENT_TYPE = 'application/vnd.google-earth.kml+xml'
 40
 41# The OGC KML 2.2 namespace
 42KML_NAMESPACE = 'http://www.opengis.net/kml/2.2'
 43
 44class MapsDataEntry(gdata.data.GDEntry):
 45  """Adds convenience methods inherited by all Maps Data entries."""
 46
 47  def get_user_id(self):
 48    """Extracts the user ID of this entry."""
 49    if self.id.text:
 50      match = self.__class__.atom_id_pattern.search(self.id.text)
 51      if match:
 52        return match.group('user_id')
 53    return None
 54
 55  GetUserId = get_user_id
 56
 57  def get_map_id(self):
 58    """Extracts the map ID of this entry."""
 59    if self.id.text:
 60      match = self.__class__.atom_id_pattern.search(self.id.text)
 61      if match:
 62        return match.group('map_id')
 63    return None
 64
 65  GetMapId = get_map_id
 66
 67
 68class Map(MapsDataEntry):
 69  """Represents a map which belongs to the user."""
 70  atom_id_pattern = MAP_ATOM_ID_PATTERN
 71
 72
 73class MapFeed(gdata.data.GDFeed):
 74  """Represents an atom feed of maps."""
 75  entry = [Map]
 76
 77
 78class KmlContent(atom.data.Content):
 79  """Represents an atom content element that encapsulates KML content."""
 80
 81  def __init__(self, **kwargs):
 82    super(KmlContent, self).__init__(type=KML_CONTENT_TYPE, **kwargs)
 83    if 'kml' in kwargs:
 84      self.kml = kwargs['kml']
 85
 86  def _get_kml(self):
 87    if self.children:
 88      return self.children[0]
 89    else:
 90      return ''
 91
 92  def _set_kml(self, kml):
 93    if not kml:
 94      self.children = []
 95      return
 96
 97    if type(kml) == str:
 98      kml = atom.core.parse(kml)
 99      if not kml.namespace:
100        kml.namespace = KML_NAMESPACE
101
102    self.children = [kml]
103
104  kml = property(_get_kml, _set_kml)
105
106
107class Feature(MapsDataEntry):
108  """Represents a single feature in a map."""
109  atom_id_pattern = FEATURE_ATOM_ID_PATTERN
110  content = KmlContent
111
112  def get_feature_id(self):
113    """Extracts the feature ID of this feature."""
114    if self.id.text:
115      match = self.__class__.atom_id_pattern.search(self.id.text)
116      if match:
117        return match.group('feature_id')
118    return None
119
120  GetFeatureId = get_feature_id
121
122
123class FeatureFeed(gdata.data.GDFeed):
124  """Represents an atom feed of features."""
125  entry = [Feature]