PageRenderTime 109ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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.
  14. import googlecl
  15. import googlecl.base
  16. service_name = __name__.split('.')[-1]
  17. LOGGER_NAME = __name__
  18. SECTION_HEADER = service_name.upper()
  19. def _map_access_string(access_string):
  20. """Map an access string to a value Blogger will understand.
  21. In this case, Blogger only cares about "is draft" so 'public' gets mapped to
  22. False, everything else to True.
  23. Returns:
  24. Boolean indicating True (is a draft) or False (is not a draft).
  25. """
  26. if not access_string:
  27. return False
  28. if access_string == 'public':
  29. return False
  30. return True
  31. class BloggerEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
  32. @property
  33. def access(self):
  34. """Access level (draft or public)."""
  35. if self.entry.control and self.entry.control.draft.text == 'yes':
  36. return 'draft'
  37. else:
  38. return 'public'
  39. @property
  40. def author(self):
  41. """Author."""
  42. # Name of author 'x' name is in entry.author[x].name.text
  43. text_extractor = lambda entry: getattr(getattr(entry, 'name'), 'text')
  44. return self._join(self.entry.author, text_extractor=text_extractor)
  45. @property
  46. def tags(self):
  47. return self.intra_property_delimiter.join(
  48. [c.term for c in self.entry.category if c.term])
  49. labels = tags
  50. #===============================================================================
  51. # Each of the following _run_* functions execute a particular task.
  52. #
  53. # Keyword arguments:
  54. # client: Client to the service being used.
  55. # options: Contains all attributes required to perform the task
  56. # args: Additional arguments passed in on the command line, may or may not be
  57. # required
  58. #===============================================================================
  59. def _run_post(client, options, args):
  60. content_list = options.src + args
  61. entry_list = client.UploadPosts(content_list,
  62. blog_title=options.blog,
  63. post_title=options.title,
  64. is_draft=_map_access_string(options.access))
  65. if options.tags:
  66. client.LabelPosts(entry_list, options.tags)
  67. def _run_delete(client, options, args):
  68. titles_list = googlecl.build_titles_list(options.title, args)
  69. post_entries = client.GetPosts(blog_title=options.blog,
  70. post_titles=titles_list)
  71. client.DeleteEntryList(post_entries, 'post', options.prompt)
  72. def _run_list(client, options, args):
  73. titles_list = googlecl.build_titles_list(options.title, args)
  74. entries = client.GetPosts(options.blog, titles_list,
  75. user_id=options.owner or 'default')
  76. for entry in entries:
  77. print googlecl.base.compile_entry_string(
  78. BloggerEntryToStringWrapper(entry),
  79. options.fields.split(','),
  80. delimiter=options.delimiter)
  81. def _run_tag(client, options, args):
  82. titles_list = googlecl.build_titles_list(options.title, args)
  83. entries = client.GetPosts(options.blog, titles_list)
  84. client.LabelPosts(entries, options.tags)
  85. TASKS = {'delete': googlecl.base.Task('Delete a post.', callback=_run_delete,
  86. required=['blog', 'title']),
  87. 'post': googlecl.base.Task('Post content.', callback=_run_post,
  88. required=['src', 'blog'],
  89. optional=['title', 'tags', 'access']),
  90. 'list': googlecl.base.Task('List posts in a blog',
  91. callback=_run_list,
  92. required=['fields', 'blog', 'delimiter'],
  93. optional=['title', 'owner']),
  94. 'tag': googlecl.base.Task('Label posts', callback=_run_tag,
  95. required=['blog', 'title', 'tags'])}