PageRenderTime 84ms CodeModel.GetById 22ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 1ms

/gdata/webmastertools/__init__.py

http://radioappz.googlecode.com/
Python | 544 lines | 436 code | 91 blank | 17 comment | 10 complexity | ae11c3ae38d7f927ff0dca886b5ef190 MD5 | raw file
  1#!/usr/bin/python
  2#
  3# Copyright (C) 2008 Yu-Jie Lin
  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 Webmaster Tools."""
 18
 19
 20__author__ = 'livibetter (Yu-Jie Lin)'
 21
 22
 23try:
 24  from xml.etree import cElementTree as ElementTree
 25except ImportError:
 26  try:
 27    import cElementTree as ElementTree
 28  except ImportError:
 29    try:
 30      from xml.etree import ElementTree
 31    except ImportError:
 32      from elementtree import ElementTree
 33import atom
 34import gdata
 35
 36
 37# XML namespaces which are often used in Google Webmaster Tools entities.
 38GWEBMASTERTOOLS_NAMESPACE = 'http://schemas.google.com/webmasters/tools/2007'
 39GWEBMASTERTOOLS_TEMPLATE = '{http://schemas.google.com/webmasters/tools/2007}%s'
 40
 41
 42class Indexed(atom.AtomBase):
 43  _tag = 'indexed'
 44  _namespace = GWEBMASTERTOOLS_NAMESPACE
 45
 46
 47def IndexedFromString(xml_string):
 48  return atom.CreateClassFromXMLString(Indexed, xml_string)
 49
 50
 51class Crawled(atom.Date):
 52  _tag = 'crawled'
 53  _namespace = GWEBMASTERTOOLS_NAMESPACE
 54
 55
 56def CrawledFromString(xml_string):
 57  return atom.CreateClassFromXMLString(Crawled, xml_string)
 58
 59
 60class GeoLocation(atom.AtomBase):
 61  _tag = 'geolocation'
 62  _namespace = GWEBMASTERTOOLS_NAMESPACE
 63
 64
 65def GeoLocationFromString(xml_string):
 66  return atom.CreateClassFromXMLString(GeoLocation, xml_string)
 67
 68
 69class PreferredDomain(atom.AtomBase):
 70  _tag = 'preferred-domain'
 71  _namespace = GWEBMASTERTOOLS_NAMESPACE
 72
 73
 74def PreferredDomainFromString(xml_string):
 75  return atom.CreateClassFromXMLString(PreferredDomain, xml_string)
 76
 77
 78class CrawlRate(atom.AtomBase):
 79  _tag = 'crawl-rate'
 80  _namespace = GWEBMASTERTOOLS_NAMESPACE
 81
 82
 83def CrawlRateFromString(xml_string):
 84  return atom.CreateClassFromXMLString(CrawlRate, xml_string)
 85
 86
 87class EnhancedImageSearch(atom.AtomBase):
 88  _tag = 'enhanced-image-search'
 89  _namespace = GWEBMASTERTOOLS_NAMESPACE
 90
 91
 92def EnhancedImageSearchFromString(xml_string):
 93  return atom.CreateClassFromXMLString(EnhancedImageSearch, xml_string)
 94
 95
 96class Verified(atom.AtomBase):
 97  _tag = 'verified'
 98  _namespace = GWEBMASTERTOOLS_NAMESPACE
 99
100
101def VerifiedFromString(xml_string):
102  return atom.CreateClassFromXMLString(Verified, xml_string)
103
104
105class VerificationMethodMeta(atom.AtomBase):
106  _tag = 'meta'
107  _namespace = atom.ATOM_NAMESPACE
108  _children = atom.AtomBase._children.copy()
109  _attributes = atom.AtomBase._attributes.copy()
110  _attributes['name'] = 'name'
111  _attributes['content'] = 'content'
112
113  def __init__(self, text=None, name=None, content=None,
114      extension_elements=None, extension_attributes=None):
115    self.text = text
116    self.name = name
117    self.content = content
118    self.extension_elements = extension_elements or []
119    self.extension_attributes = extension_attributes or {}
120
121
122def VerificationMethodMetaFromString(xml_string):
123  return atom.CreateClassFromXMLString(VerificationMethodMeta, xml_string)
124
125
126class VerificationMethod(atom.AtomBase):
127  _tag = 'verification-method'
128  _namespace = GWEBMASTERTOOLS_NAMESPACE
129  _children = atom.Text._children.copy()
130  _attributes = atom.Text._attributes.copy()
131  _children['{%s}meta' % atom.ATOM_NAMESPACE] = (
132    'meta', VerificationMethodMeta)
133  _attributes['in-use'] = 'in_use'
134  _attributes['type'] = 'type'
135
136  def __init__(self, text=None, in_use=None, meta=None, type=None,
137      extension_elements=None, extension_attributes=None):
138    self.text = text
139    self.in_use = in_use
140    self.meta = meta
141    self.type = type
142    self.extension_elements = extension_elements or []
143    self.extension_attributes = extension_attributes or {}
144
145
146def VerificationMethodFromString(xml_string):
147  return atom.CreateClassFromXMLString(VerificationMethod, xml_string)
148
149
150class MarkupLanguage(atom.AtomBase):
151  _tag = 'markup-language'
152  _namespace = GWEBMASTERTOOLS_NAMESPACE
153
154
155def MarkupLanguageFromString(xml_string):
156  return atom.CreateClassFromXMLString(MarkupLanguage, xml_string)
157
158
159class SitemapMobile(atom.AtomBase):
160  _tag = 'sitemap-mobile'
161  _namespace = GWEBMASTERTOOLS_NAMESPACE
162  _children = atom.AtomBase._children.copy()
163  _attributes = atom.AtomBase._attributes.copy()
164  _children['{%s}markup-language' % GWEBMASTERTOOLS_NAMESPACE] = (
165      'markup_language', [MarkupLanguage])
166
167  def __init__(self, markup_language=None,
168      extension_elements=None, extension_attributes=None, text=None):
169
170    self.markup_language = markup_language or []
171    self.text = text
172    self.extension_elements = extension_elements or []
173    self.extension_attributes = extension_attributes or {}
174
175
176def SitemapMobileFromString(xml_string):
177  return atom.CreateClassFromXMLString(SitemapMobile, xml_string)
178
179
180class SitemapMobileMarkupLanguage(atom.AtomBase):
181  _tag = 'sitemap-mobile-markup-language'
182  _namespace = GWEBMASTERTOOLS_NAMESPACE
183
184
185def SitemapMobileMarkupLanguageFromString(xml_string):
186  return atom.CreateClassFromXMLString(SitemapMobileMarkupLanguage, xml_string)
187
188
189class PublicationLabel(atom.AtomBase):
190  _tag = 'publication-label'
191  _namespace = GWEBMASTERTOOLS_NAMESPACE
192
193
194def PublicationLabelFromString(xml_string):
195  return atom.CreateClassFromXMLString(PublicationLabel, xml_string)
196
197
198class SitemapNews(atom.AtomBase):
199  _tag = 'sitemap-news'
200  _namespace = GWEBMASTERTOOLS_NAMESPACE
201  _children = atom.AtomBase._children.copy()
202  _attributes = atom.AtomBase._attributes.copy()
203  _children['{%s}publication-label' % GWEBMASTERTOOLS_NAMESPACE] = (
204      'publication_label', [PublicationLabel])
205
206  def __init__(self, publication_label=None,
207    extension_elements=None, extension_attributes=None, text=None):
208
209    self.publication_label = publication_label or []
210    self.text = text
211    self.extension_elements = extension_elements or []
212    self.extension_attributes = extension_attributes or {}
213
214
215def SitemapNewsFromString(xml_string):
216  return atom.CreateClassFromXMLString(SitemapNews, xml_string)
217
218
219class SitemapNewsPublicationLabel(atom.AtomBase):
220  _tag = 'sitemap-news-publication-label'
221  _namespace = GWEBMASTERTOOLS_NAMESPACE
222
223
224def SitemapNewsPublicationLabelFromString(xml_string):
225  return atom.CreateClassFromXMLString(SitemapNewsPublicationLabel, xml_string)
226
227
228class SitemapLastDownloaded(atom.Date):
229  _tag = 'sitemap-last-downloaded'
230  _namespace = GWEBMASTERTOOLS_NAMESPACE
231
232
233def SitemapLastDownloadedFromString(xml_string):
234  return atom.CreateClassFromXMLString(SitemapLastDownloaded, xml_string)
235
236
237class SitemapType(atom.AtomBase):
238  _tag = 'sitemap-type'
239  _namespace = GWEBMASTERTOOLS_NAMESPACE
240
241
242def SitemapTypeFromString(xml_string):
243  return atom.CreateClassFromXMLString(SitemapType, xml_string)
244
245
246class SitemapStatus(atom.AtomBase):
247  _tag = 'sitemap-status'
248  _namespace = GWEBMASTERTOOLS_NAMESPACE
249
250
251def SitemapStatusFromString(xml_string):
252  return atom.CreateClassFromXMLString(SitemapStatus, xml_string)
253
254
255class SitemapUrlCount(atom.AtomBase):
256  _tag = 'sitemap-url-count'
257  _namespace = GWEBMASTERTOOLS_NAMESPACE
258
259
260def SitemapUrlCountFromString(xml_string):
261  return atom.CreateClassFromXMLString(SitemapUrlCount, xml_string)
262
263
264class LinkFinder(atom.LinkFinder):
265  """An "interface" providing methods to find link elements
266
267  SitesEntry elements often contain multiple links which differ in the rel 
268  attribute or content type. Often, developers are interested in a specific
269  type of link so this class provides methods to find specific classes of links.
270
271  This class is used as a mixin in SitesEntry.
272  """
273
274  def GetSelfLink(self):
275    """Find the first link with rel set to 'self'
276
277    Returns:
278      An atom.Link or none if none of the links had rel equal to 'self'
279    """
280
281    for a_link in self.link:
282      if a_link.rel == 'self':
283        return a_link
284    return None
285
286  def GetEditLink(self):
287    for a_link in self.link:
288      if a_link.rel == 'edit':
289        return a_link
290    return None
291
292  def GetPostLink(self):
293    """Get a link containing the POST target URL.
294    
295    The POST target URL is used to insert new entries.
296
297    Returns:
298      A link object with a rel matching the POST type.
299    """
300    for a_link in self.link:
301      if a_link.rel == 'http://schemas.google.com/g/2005#post':
302        return a_link
303    return None
304
305  def GetFeedLink(self):
306    for a_link in self.link:
307      if a_link.rel == 'http://schemas.google.com/g/2005#feed':
308        return a_link
309    return None
310
311
312class SitesEntry(atom.Entry, LinkFinder):
313  """A Google Webmaster Tools meta Entry flavor of an Atom Entry """
314
315  _tag = atom.Entry._tag
316  _namespace = atom.Entry._namespace
317  _children = atom.Entry._children.copy()
318  _attributes = atom.Entry._attributes.copy()
319  _children['{%s}entryLink' % gdata.GDATA_NAMESPACE] = (
320      'entry_link', [gdata.EntryLink])
321  _children['{%s}indexed' % GWEBMASTERTOOLS_NAMESPACE] = ('indexed', Indexed)
322  _children['{%s}crawled' % GWEBMASTERTOOLS_NAMESPACE] = (
323      'crawled', Crawled)
324  _children['{%s}geolocation' % GWEBMASTERTOOLS_NAMESPACE] = (
325      'geolocation', GeoLocation)
326  _children['{%s}preferred-domain' % GWEBMASTERTOOLS_NAMESPACE] = (
327      'preferred_domain', PreferredDomain)
328  _children['{%s}crawl-rate' % GWEBMASTERTOOLS_NAMESPACE] = (
329      'crawl_rate', CrawlRate)
330  _children['{%s}enhanced-image-search' % GWEBMASTERTOOLS_NAMESPACE] = (
331      'enhanced_image_search', EnhancedImageSearch)
332  _children['{%s}verified' % GWEBMASTERTOOLS_NAMESPACE] = (
333      'verified', Verified)
334  _children['{%s}verification-method' % GWEBMASTERTOOLS_NAMESPACE] = (
335      'verification_method', [VerificationMethod])
336  
337  def __GetId(self):
338    return self.__id
339
340  # This method was created to strip the unwanted whitespace from the id's 
341  # text node.
342  def __SetId(self, id):
343    self.__id = id
344    if id is not None and id.text is not None:
345      self.__id.text = id.text.strip()
346
347  id = property(__GetId, __SetId)
348
349  def __init__(self, category=None, content=None,
350      atom_id=None, link=None, title=None, updated=None,
351      entry_link=None, indexed=None, crawled=None,
352      geolocation=None, preferred_domain=None, crawl_rate=None,
353      enhanced_image_search=None,
354      verified=None, verification_method=None,
355      extension_elements=None, extension_attributes=None, text=None):
356    atom.Entry.__init__(self, category=category, 
357                        content=content, atom_id=atom_id, link=link, 
358                        title=title, updated=updated, text=text)
359
360    self.entry_link = entry_link or []
361    self.indexed = indexed
362    self.crawled = crawled
363    self.geolocation = geolocation
364    self.preferred_domain = preferred_domain
365    self.crawl_rate = crawl_rate
366    self.enhanced_image_search = enhanced_image_search
367    self.verified = verified
368    self.verification_method = verification_method or []
369
370
371def SitesEntryFromString(xml_string):
372  return atom.CreateClassFromXMLString(SitesEntry, xml_string)
373
374
375class SitesFeed(atom.Feed, LinkFinder):
376  """A Google Webmaster Tools meta Sites feed flavor of an Atom Feed"""
377
378  _tag = atom.Feed._tag
379  _namespace = atom.Feed._namespace
380  _children = atom.Feed._children.copy()
381  _attributes = atom.Feed._attributes.copy()
382  _children['{%s}startIndex' % gdata.OPENSEARCH_NAMESPACE] = (
383      'start_index', gdata.StartIndex)
384  _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [SitesEntry])
385  del _children['{%s}generator' % atom.ATOM_NAMESPACE]
386  del _children['{%s}author' % atom.ATOM_NAMESPACE]
387  del _children['{%s}contributor' % atom.ATOM_NAMESPACE]
388  del _children['{%s}logo' % atom.ATOM_NAMESPACE]
389  del _children['{%s}icon' % atom.ATOM_NAMESPACE]
390  del _children['{%s}rights' % atom.ATOM_NAMESPACE]
391  del _children['{%s}subtitle' % atom.ATOM_NAMESPACE]
392
393  def __GetId(self):
394    return self.__id
395
396  def __SetId(self, id):
397    self.__id = id
398    if id is not None and id.text is not None:
399      self.__id.text = id.text.strip()
400
401  id = property(__GetId, __SetId)
402
403  def __init__(self, start_index=None, atom_id=None, title=None, entry=None,
404      category=None, link=None, updated=None,
405      extension_elements=None, extension_attributes=None, text=None):
406    """Constructor for Source
407    
408    Args:
409      category: list (optional) A list of Category instances
410      id: Id (optional) The entry's Id element
411      link: list (optional) A list of Link instances
412      title: Title (optional) the entry's title element
413      updated: Updated (optional) the entry's updated element
414      entry: list (optional) A list of the Entry instances contained in the 
415          feed.
416      text: String (optional) The text contents of the element. This is the 
417          contents of the Entry's XML text node. 
418          (Example: <foo>This is the text</foo>)
419      extension_elements: list (optional) A list of ExtensionElement instances
420          which are children of this element.
421      extension_attributes: dict (optional) A dictionary of strings which are 
422          the values for additional XML attributes of this element.
423    """
424
425    self.start_index = start_index
426    self.category = category or []
427    self.id = atom_id
428    self.link = link or []
429    self.title = title
430    self.updated = updated
431    self.entry = entry or []
432    self.text = text
433    self.extension_elements = extension_elements or []
434    self.extension_attributes = extension_attributes or {}
435
436
437def SitesFeedFromString(xml_string):
438  return atom.CreateClassFromXMLString(SitesFeed, xml_string)
439
440
441class SitemapsEntry(atom.Entry, LinkFinder):
442  """A Google Webmaster Tools meta Sitemaps Entry flavor of an Atom Entry """
443
444  _tag = atom.Entry._tag
445  _namespace = atom.Entry._namespace
446  _children = atom.Entry._children.copy()
447  _attributes = atom.Entry._attributes.copy()
448  _children['{%s}sitemap-type' % GWEBMASTERTOOLS_NAMESPACE] = (
449      'sitemap_type', SitemapType)
450  _children['{%s}sitemap-status' % GWEBMASTERTOOLS_NAMESPACE] = (
451      'sitemap_status', SitemapStatus)
452  _children['{%s}sitemap-last-downloaded' % GWEBMASTERTOOLS_NAMESPACE] = (
453      'sitemap_last_downloaded', SitemapLastDownloaded)
454  _children['{%s}sitemap-url-count' % GWEBMASTERTOOLS_NAMESPACE] = (
455      'sitemap_url_count', SitemapUrlCount)
456  _children['{%s}sitemap-mobile-markup-language' % GWEBMASTERTOOLS_NAMESPACE] \
457      = ('sitemap_mobile_markup_language', SitemapMobileMarkupLanguage)
458  _children['{%s}sitemap-news-publication-label' % GWEBMASTERTOOLS_NAMESPACE] \
459      = ('sitemap_news_publication_label', SitemapNewsPublicationLabel)
460  
461  def __GetId(self):
462    return self.__id
463
464  # This method was created to strip the unwanted whitespace from the id's 
465  # text node.
466  def __SetId(self, id):
467    self.__id = id
468    if id is not None and id.text is not None:
469      self.__id.text = id.text.strip()
470
471  id = property(__GetId, __SetId)
472
473  def __init__(self, category=None, content=None,
474      atom_id=None, link=None, title=None, updated=None,
475      sitemap_type=None, sitemap_status=None, sitemap_last_downloaded=None,
476      sitemap_url_count=None, sitemap_mobile_markup_language=None,
477      sitemap_news_publication_label=None,
478      extension_elements=None, extension_attributes=None, text=None):
479    atom.Entry.__init__(self, category=category, 
480                        content=content, atom_id=atom_id, link=link, 
481                        title=title, updated=updated, text=text)
482
483    self.sitemap_type = sitemap_type
484    self.sitemap_status = sitemap_status
485    self.sitemap_last_downloaded = sitemap_last_downloaded
486    self.sitemap_url_count = sitemap_url_count
487    self.sitemap_mobile_markup_language = sitemap_mobile_markup_language
488    self.sitemap_news_publication_label = sitemap_news_publication_label
489
490
491def SitemapsEntryFromString(xml_string):
492  return atom.CreateClassFromXMLString(SitemapsEntry, xml_string)
493
494
495class SitemapsFeed(atom.Feed, LinkFinder):
496  """A Google Webmaster Tools meta Sitemaps feed flavor of an Atom Feed"""
497
498  _tag = atom.Feed._tag
499  _namespace = atom.Feed._namespace
500  _children = atom.Feed._children.copy()
501  _attributes = atom.Feed._attributes.copy()
502  _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [SitemapsEntry])
503  _children['{%s}sitemap-mobile' % GWEBMASTERTOOLS_NAMESPACE] = (
504      'sitemap_mobile', SitemapMobile)
505  _children['{%s}sitemap-news' % GWEBMASTERTOOLS_NAMESPACE] = (
506      'sitemap_news', SitemapNews)
507  del _children['{%s}generator' % atom.ATOM_NAMESPACE]
508  del _children['{%s}author' % atom.ATOM_NAMESPACE]
509  del _children['{%s}contributor' % atom.ATOM_NAMESPACE]
510  del _children['{%s}logo' % atom.ATOM_NAMESPACE]
511  del _children['{%s}icon' % atom.ATOM_NAMESPACE]
512  del _children['{%s}rights' % atom.ATOM_NAMESPACE]
513  del _children['{%s}subtitle' % atom.ATOM_NAMESPACE]
514
515  def __GetId(self):
516    return self.__id
517
518  def __SetId(self, id):
519    self.__id = id
520    if id is not None and id.text is not None:
521      self.__id.text = id.text.strip()
522
523  id = property(__GetId, __SetId)
524
525  def __init__(self, category=None, content=None,
526      atom_id=None, link=None, title=None, updated=None,
527      entry=None, sitemap_mobile=None, sitemap_news=None,
528      extension_elements=None, extension_attributes=None, text=None):
529
530    self.category = category or []
531    self.id = atom_id
532    self.link = link or []
533    self.title = title
534    self.updated = updated
535    self.entry = entry or []
536    self.text = text
537    self.sitemap_mobile = sitemap_mobile
538    self.sitemap_news = sitemap_news
539    self.extension_elements = extension_elements or []
540    self.extension_attributes = extension_attributes or {}
541
542
543def SitemapsFeedFromString(xml_string):
544  return atom.CreateClassFromXMLString(SitemapsFeed, xml_string)