/gdata/codesearch/__init__.py

http://radioappz.googlecode.com/ · Python · 136 lines · 73 code · 28 blank · 35 comment · 8 complexity · 87055f8a58d4dbfe5c8c71a034505dc4 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. """Contains extensions to Atom objects used by Google Codesearch"""
  17. __author__ = 'Benoit Chesneau'
  18. import atom
  19. import gdata
  20. CODESEARCH_NAMESPACE='http://schemas.google.com/codesearch/2006'
  21. CODESEARCH_TEMPLATE='{http://shema.google.com/codesearch/2006}%s'
  22. class Match(atom.AtomBase):
  23. """ The Google Codesearch match element """
  24. _tag = 'match'
  25. _namespace = CODESEARCH_NAMESPACE
  26. _children = atom.AtomBase._children.copy()
  27. _attributes = atom.AtomBase._attributes.copy()
  28. _attributes['lineNumber'] = 'line_number'
  29. _attributes['type'] = 'type'
  30. def __init__(self, line_number=None, type=None, extension_elements=None,
  31. extension_attributes=None, text=None):
  32. self.text = text
  33. self.type = type
  34. self.line_number = line_number
  35. self.extension_elements = extension_elements or []
  36. self.extension_attributes = extension_attributes or {}
  37. class File(atom.AtomBase):
  38. """ The Google Codesearch file element"""
  39. _tag = 'file'
  40. _namespace = CODESEARCH_NAMESPACE
  41. _children = atom.AtomBase._children.copy()
  42. _attributes = atom.AtomBase._attributes.copy()
  43. _attributes['name'] = 'name'
  44. def __init__(self, name=None, extension_elements=None,
  45. extension_attributes=None, text=None):
  46. self.text = text
  47. self.name = name
  48. self.extension_elements = extension_elements or []
  49. self.extension_attributes = extension_attributes or {}
  50. class Package(atom.AtomBase):
  51. """ The Google Codesearch package element"""
  52. _tag = 'package'
  53. _namespace = CODESEARCH_NAMESPACE
  54. _children = atom.AtomBase._children.copy()
  55. _attributes = atom.AtomBase._attributes.copy()
  56. _attributes['name'] = 'name'
  57. _attributes['uri'] = 'uri'
  58. def __init__(self, name=None, uri=None, extension_elements=None,
  59. extension_attributes=None, text=None):
  60. self.text = text
  61. self.name = name
  62. self.uri = uri
  63. self.extension_elements = extension_elements or []
  64. self.extension_attributes = extension_attributes or {}
  65. class CodesearchEntry(gdata.GDataEntry):
  66. """ Google codesearch atom entry"""
  67. _tag = gdata.GDataEntry._tag
  68. _namespace = gdata.GDataEntry._namespace
  69. _children = gdata.GDataEntry._children.copy()
  70. _attributes = gdata.GDataEntry._attributes.copy()
  71. _children['{%s}file' % CODESEARCH_NAMESPACE] = ('file', File)
  72. _children['{%s}package' % CODESEARCH_NAMESPACE] = ('package', Package)
  73. _children['{%s}match' % CODESEARCH_NAMESPACE] = ('match', [Match])
  74. def __init__(self, author=None, category=None, content=None,
  75. atom_id=None, link=None, published=None,
  76. title=None, updated=None,
  77. match=None,
  78. extension_elements=None, extension_attributes=None, text=None):
  79. gdata.GDataEntry.__init__(self, author=author, category=category,
  80. content=content, atom_id=atom_id, link=link,
  81. published=published, title=title,
  82. updated=updated, text=None)
  83. self.match = match or []
  84. def CodesearchEntryFromString(xml_string):
  85. """Converts an XML string into a CodesearchEntry object.
  86. Args:
  87. xml_string: string The XML describing a Codesearch feed entry.
  88. Returns:
  89. A CodesearchEntry object corresponding to the given XML.
  90. """
  91. return atom.CreateClassFromXMLString(CodesearchEntry, xml_string)
  92. class CodesearchFeed(gdata.GDataFeed):
  93. """feed containing list of Google codesearch Items"""
  94. _tag = gdata.GDataFeed._tag
  95. _namespace = gdata.GDataFeed._namespace
  96. _children = gdata.GDataFeed._children.copy()
  97. _attributes = gdata.GDataFeed._attributes.copy()
  98. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [CodesearchEntry])
  99. def CodesearchFeedFromString(xml_string):
  100. """Converts an XML string into a CodesearchFeed object.
  101. Args:
  102. xml_string: string The XML describing a Codesearch feed.
  103. Returns:
  104. A CodeseartchFeed object corresponding to the given XML.
  105. """
  106. return atom.CreateClassFromXMLString(CodesearchFeed, xml_string)