/gdata/codesearch/service.py

http://radioappz.googlecode.com/ · Python · 109 lines · 73 code · 10 blank · 26 comment · 0 complexity · e373a1ea4246bc527a71e8cad66efca3 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (c) 2007 Benoit Chesneau <benoitc@metavers.net>
  4. #
  5. # Permission to use, copy, modify, and distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """CodesearchService extends GDataService to streamline Google Codesearch
  17. operations"""
  18. __author__ = 'Benoit Chesneau'
  19. import atom
  20. import gdata.service
  21. import gdata.codesearch
  22. class CodesearchService(gdata.service.GDataService):
  23. """Client extension for Google codesearch service"""
  24. def __init__(self, email=None, password=None, source=None,
  25. server='www.google.com', additional_headers=None, **kwargs):
  26. """Creates a client for the Google codesearch service.
  27. Args:
  28. email: string (optional) The user's email address, used for
  29. authentication.
  30. password: string (optional) The user's password.
  31. source: string (optional) The name of the user's application.
  32. server: string (optional) The name of the server to which a connection
  33. will be opened. Default value: 'www.google.com'.
  34. **kwargs: The other parameters to pass to gdata.service.GDataService
  35. constructor.
  36. """
  37. gdata.service.GDataService.__init__(
  38. self, email=email, password=password, service='codesearch',
  39. source=source, server=server, additional_headers=additional_headers,
  40. **kwargs)
  41. def Query(self, uri, converter=gdata.codesearch.CodesearchFeedFromString):
  42. """Queries the Codesearch feed and returns the resulting feed of
  43. entries.
  44. Args:
  45. uri: string The full URI to be queried. This can contain query
  46. parameters, a hostname, or simply the relative path to a Document
  47. List feed. The DocumentQuery object is useful when constructing
  48. query parameters.
  49. converter: func (optional) A function which will be executed on the
  50. retrieved item, generally to render it into a Python object.
  51. By default the CodesearchFeedFromString function is used to
  52. return a CodesearchFeed object. This is because most feed
  53. queries will result in a feed and not a single entry.
  54. Returns :
  55. A CodesearchFeed objects representing the feed returned by the server
  56. """
  57. return self.Get(uri, converter=converter)
  58. def GetSnippetsFeed(self, text_query=None):
  59. """Retrieve Codesearch feed for a keyword
  60. Args:
  61. text_query : string (optional) The contents of the q query parameter. This
  62. string is URL escaped upon conversion to a URI.
  63. Returns:
  64. A CodesearchFeed objects representing the feed returned by the server
  65. """
  66. query=gdata.codesearch.service.CodesearchQuery(text_query=text_query)
  67. feed = self.Query(query.ToUri())
  68. return feed
  69. class CodesearchQuery(gdata.service.Query):
  70. """Object used to construct the query to the Google Codesearch feed. here only as a shorcut"""
  71. def __init__(self, feed='/codesearch/feeds/search', text_query=None,
  72. params=None, categories=None):
  73. """Constructor for Codesearch Query.
  74. Args:
  75. feed: string (optional) The path for the feed. (e.g. '/codesearch/feeds/search')
  76. text_query: string (optional) The contents of the q query parameter. This
  77. string is URL escaped upon conversion to a URI.
  78. params: dict (optional) Parameter value string pairs which become URL
  79. params when translated to a URI. These parameters are added to
  80. the query's items.
  81. categories: list (optional) List of category strings which should be
  82. included as query categories. See gdata.service.Query for
  83. additional documentation.
  84. Yelds:
  85. A CodesearchQuery object to construct a URI based on Codesearch feed
  86. """
  87. gdata.service.Query.__init__(self, feed, text_query, params, categories)