PageRenderTime 72ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/src/googlecl/blogger/__init__.py

http://googlecl.googlecode.com/
Python | 112 lines | 94 code | 3 blank | 15 comment | 0 complexity | 617a127b9eeaf8a04dc0d1ec7fe7c99d MD5 | raw file
  1# Copyright (C) 2010 Google Inc.
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7#      http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14import googlecl
 15import googlecl.base
 16
 17service_name = __name__.split('.')[-1]
 18LOGGER_NAME = __name__
 19SECTION_HEADER = service_name.upper()
 20
 21
 22def _map_access_string(access_string):
 23  """Map an access string to a value Blogger will understand.
 24
 25  In this case, Blogger only cares about "is draft" so 'public' gets mapped to
 26  False, everything else to True.
 27
 28  Returns:
 29    Boolean indicating True (is a draft) or False (is not a draft).
 30  """
 31  if not access_string:
 32    return False
 33  if access_string == 'public':
 34    return False
 35  return True
 36
 37
 38class BloggerEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
 39  @property
 40  def access(self):
 41    """Access level (draft or public)."""
 42    if self.entry.control and self.entry.control.draft.text == 'yes':
 43      return 'draft'
 44    else:
 45      return 'public'
 46
 47  @property
 48  def author(self):
 49    """Author."""
 50    # Name of author 'x' name is in entry.author[x].name.text
 51    text_extractor = lambda entry: getattr(getattr(entry, 'name'), 'text')
 52    return self._join(self.entry.author, text_extractor=text_extractor)
 53
 54  @property
 55  def tags(self):
 56    return self.intra_property_delimiter.join(
 57                            [c.term for c in self.entry.category if c.term])
 58  labels = tags
 59#===============================================================================
 60# Each of the following _run_* functions execute a particular task.
 61#
 62# Keyword arguments:
 63#  client: Client to the service being used.
 64#  options: Contains all attributes required to perform the task
 65#  args: Additional arguments passed in on the command line, may or may not be
 66#        required
 67#===============================================================================
 68def _run_post(client, options, args):
 69  content_list = options.src + args
 70  entry_list = client.UploadPosts(content_list,
 71                                  blog_title=options.blog,
 72                                  post_title=options.title,
 73                                  is_draft=_map_access_string(options.access))
 74  if options.tags:
 75    client.LabelPosts(entry_list, options.tags)
 76
 77
 78def _run_delete(client, options, args):
 79  titles_list = googlecl.build_titles_list(options.title, args)
 80  post_entries = client.GetPosts(blog_title=options.blog,
 81                                 post_titles=titles_list)
 82  client.DeleteEntryList(post_entries, 'post', options.prompt)
 83
 84
 85def _run_list(client, options, args):
 86  titles_list = googlecl.build_titles_list(options.title, args)
 87  entries = client.GetPosts(options.blog, titles_list,
 88                            user_id=options.owner or 'default')
 89  for entry in entries:
 90    print googlecl.base.compile_entry_string(
 91                                             BloggerEntryToStringWrapper(entry),
 92                                             options.fields.split(','),
 93                                             delimiter=options.delimiter)
 94
 95
 96def _run_tag(client, options, args):
 97  titles_list = googlecl.build_titles_list(options.title, args)
 98  entries = client.GetPosts(options.blog, titles_list)
 99  client.LabelPosts(entries, options.tags)
100
101
102TASKS = {'delete': googlecl.base.Task('Delete a post.', callback=_run_delete,
103                                      required=['blog', 'title']),
104         'post': googlecl.base.Task('Post content.', callback=_run_post,
105                                    required=['src', 'blog'],
106                                    optional=['title', 'tags', 'access']),
107         'list': googlecl.base.Task('List posts in a blog',
108                                    callback=_run_list,
109                                    required=['fields', 'blog', 'delimiter'],
110                                    optional=['title', 'owner']),
111         'tag': googlecl.base.Task('Label posts', callback=_run_tag,
112                                   required=['blog', 'title', 'tags'])}