PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/blogger/data.py

http://radioappz.googlecode.com/
Python | 146 lines | 97 code | 12 blank | 37 comment | 0 complexity | db03bc5c44ceee3cc8529f4021d9a9b4 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 Blogger API."""
 19
 20
 21__author__ = 'j.s@google.com (Jeff Scudder)'
 22
 23
 24import re
 25import atom.core
 26import gdata.data
 27
 28
 29LABEL_SCHEME = 'http://www.blogger.com/atom/ns#'
 30THR_TEMPLATE = '{http://purl.org/syndication/thread/1.0}%s'
 31
 32BLOG_NAME_PATTERN = re.compile('(http://)(\w*)')
 33BLOG_ID_PATTERN = re.compile('(tag:blogger.com,1999:blog-)(\w*)')
 34BLOG_ID2_PATTERN = re.compile('tag:blogger.com,1999:user-(\d+)\.blog-(\d+)')
 35POST_ID_PATTERN = re.compile(
 36    '(tag:blogger.com,1999:blog-)(\w*)(.post-)(\w*)')
 37COMMENT_ID_PATTERN = re.compile('.*-(\w*)$')
 38
 39
 40class BloggerEntry(gdata.data.GDEntry):
 41  """Adds convenience methods inherited by all Blogger entries."""
 42
 43  def get_blog_id(self):
 44    """Extracts the Blogger id of this blog.
 45
 46    This method is useful when contructing URLs by hand. The blog id is
 47    often used in blogger operation URLs. This should not be confused with
 48    the id member of a BloggerBlog. The id element is the Atom id XML element.
 49    The blog id which this method returns is a part of the Atom id.
 50
 51    Returns:
 52      The blog's unique id as a string.
 53    """
 54    if self.id.text:
 55      match = BLOG_ID_PATTERN.match(self.id.text)
 56      if match:
 57        return match.group(2)
 58      else:
 59        return BLOG_ID2_PATTERN.match(self.id.text).group(2)
 60    return None
 61
 62  GetBlogId = get_blog_id
 63
 64  def get_blog_name(self):
 65    """Finds the name of this blog as used in the 'alternate' URL.
 66
 67    An alternate URL is in the form 'http://blogName.blogspot.com/'. For an
 68    entry representing the above example, this method would return 'blogName'.
 69
 70    Returns:
 71      The blog's URL name component as a string.
 72    """
 73    for link in self.link:
 74      if link.rel == 'alternate':
 75        return BLOG_NAME_PATTERN.match(link.href).group(2)
 76    return None
 77
 78  GetBlogName = get_blog_name
 79
 80
 81class Blog(BloggerEntry):
 82  """Represents a blog which belongs to the user."""
 83
 84
 85class BlogFeed(gdata.data.GDFeed):
 86  entry = [Blog]
 87
 88
 89class BlogPost(BloggerEntry):
 90  """Represents a single post on a blog."""
 91
 92  def add_label(self, label):
 93    """Adds a label to the blog post.
 94
 95    The label is represented by an Atom category element, so this method
 96    is shorthand for appending a new atom.Category object.
 97
 98    Args:
 99      label: str
100    """
101    self.category.append(atom.data.Category(scheme=LABEL_SCHEME, term=label))
102
103  AddLabel = add_label
104
105  def get_post_id(self):
106    """Extracts the postID string from the entry's Atom id.
107
108    Returns: A string of digits which identify this post within the blog.
109    """
110    if self.id.text:
111      return POST_ID_PATTERN.match(self.id.text).group(4)
112    return None
113
114  GetPostId = get_post_id
115
116
117class BlogPostFeed(gdata.data.GDFeed):
118  entry = [BlogPost]
119
120
121class InReplyTo(atom.core.XmlElement):
122  _qname = THR_TEMPLATE % 'in-reply-to'
123  href = 'href'
124  ref = 'ref'
125  source = 'source'
126  type = 'type'
127
128
129class Comment(BloggerEntry):
130  """Blog post comment entry in a feed listing comments on a post or blog."""
131  in_reply_to = InReplyTo
132
133  def get_comment_id(self):
134    """Extracts the commentID string from the entry's Atom id.
135
136    Returns: A string of digits which identify this post within the blog.
137    """
138    if self.id.text:
139      return COMMENT_ID_PATTERN.match(self.id.text).group(1)
140    return None
141
142  GetCommentId = get_comment_id
143
144
145class CommentFeed(gdata.data.GDFeed):
146  entry = [Comment]