/gdata/maps/data.py

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