faset over fra Z3950 til google books
This commit is contained in:
136
python/gdata/codesearch/__init__.py
Normal file
136
python/gdata/codesearch/__init__.py
Normal file
@@ -0,0 +1,136 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (c) 2007 Benoit Chesneau <benoitc@metavers.net>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
|
||||
"""Contains extensions to Atom objects used by Google Codesearch"""
|
||||
|
||||
__author__ = 'Benoit Chesneau'
|
||||
|
||||
|
||||
import atom
|
||||
import gdata
|
||||
|
||||
|
||||
CODESEARCH_NAMESPACE='http://schemas.google.com/codesearch/2006'
|
||||
CODESEARCH_TEMPLATE='{http://shema.google.com/codesearch/2006}%s'
|
||||
|
||||
|
||||
class Match(atom.AtomBase):
|
||||
""" The Google Codesearch match element """
|
||||
_tag = 'match'
|
||||
_namespace = CODESEARCH_NAMESPACE
|
||||
_children = atom.AtomBase._children.copy()
|
||||
_attributes = atom.AtomBase._attributes.copy()
|
||||
_attributes['lineNumber'] = 'line_number'
|
||||
_attributes['type'] = 'type'
|
||||
|
||||
def __init__(self, line_number=None, type=None, extension_elements=None,
|
||||
extension_attributes=None, text=None):
|
||||
self.text = text
|
||||
self.type = type
|
||||
self.line_number = line_number
|
||||
self.extension_elements = extension_elements or []
|
||||
self.extension_attributes = extension_attributes or {}
|
||||
|
||||
|
||||
class File(atom.AtomBase):
|
||||
""" The Google Codesearch file element"""
|
||||
_tag = 'file'
|
||||
_namespace = CODESEARCH_NAMESPACE
|
||||
_children = atom.AtomBase._children.copy()
|
||||
_attributes = atom.AtomBase._attributes.copy()
|
||||
_attributes['name'] = 'name'
|
||||
|
||||
def __init__(self, name=None, extension_elements=None,
|
||||
extension_attributes=None, text=None):
|
||||
self.text = text
|
||||
self.name = name
|
||||
self.extension_elements = extension_elements or []
|
||||
self.extension_attributes = extension_attributes or {}
|
||||
|
||||
|
||||
class Package(atom.AtomBase):
|
||||
""" The Google Codesearch package element"""
|
||||
_tag = 'package'
|
||||
_namespace = CODESEARCH_NAMESPACE
|
||||
_children = atom.AtomBase._children.copy()
|
||||
_attributes = atom.AtomBase._attributes.copy()
|
||||
_attributes['name'] = 'name'
|
||||
_attributes['uri'] = 'uri'
|
||||
|
||||
def __init__(self, name=None, uri=None, extension_elements=None,
|
||||
extension_attributes=None, text=None):
|
||||
self.text = text
|
||||
self.name = name
|
||||
self.uri = uri
|
||||
self.extension_elements = extension_elements or []
|
||||
self.extension_attributes = extension_attributes or {}
|
||||
|
||||
|
||||
class CodesearchEntry(gdata.GDataEntry):
|
||||
""" Google codesearch atom entry"""
|
||||
_tag = gdata.GDataEntry._tag
|
||||
_namespace = gdata.GDataEntry._namespace
|
||||
_children = gdata.GDataEntry._children.copy()
|
||||
_attributes = gdata.GDataEntry._attributes.copy()
|
||||
|
||||
_children['{%s}file' % CODESEARCH_NAMESPACE] = ('file', File)
|
||||
_children['{%s}package' % CODESEARCH_NAMESPACE] = ('package', Package)
|
||||
_children['{%s}match' % CODESEARCH_NAMESPACE] = ('match', [Match])
|
||||
|
||||
def __init__(self, author=None, category=None, content=None,
|
||||
atom_id=None, link=None, published=None,
|
||||
title=None, updated=None,
|
||||
match=None,
|
||||
extension_elements=None, extension_attributes=None, text=None):
|
||||
|
||||
gdata.GDataEntry.__init__(self, author=author, category=category,
|
||||
content=content, atom_id=atom_id, link=link,
|
||||
published=published, title=title,
|
||||
updated=updated, text=None)
|
||||
|
||||
self.match = match or []
|
||||
|
||||
|
||||
def CodesearchEntryFromString(xml_string):
|
||||
"""Converts an XML string into a CodesearchEntry object.
|
||||
|
||||
Args:
|
||||
xml_string: string The XML describing a Codesearch feed entry.
|
||||
|
||||
Returns:
|
||||
A CodesearchEntry object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(CodesearchEntry, xml_string)
|
||||
|
||||
|
||||
class CodesearchFeed(gdata.GDataFeed):
|
||||
"""feed containing list of Google codesearch Items"""
|
||||
_tag = gdata.GDataFeed._tag
|
||||
_namespace = gdata.GDataFeed._namespace
|
||||
_children = gdata.GDataFeed._children.copy()
|
||||
_attributes = gdata.GDataFeed._attributes.copy()
|
||||
_children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [CodesearchEntry])
|
||||
|
||||
|
||||
def CodesearchFeedFromString(xml_string):
|
||||
"""Converts an XML string into a CodesearchFeed object.
|
||||
Args:
|
||||
xml_string: string The XML describing a Codesearch feed.
|
||||
Returns:
|
||||
A CodeseartchFeed object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(CodesearchFeed, xml_string)
|
109
python/gdata/codesearch/service.py
Normal file
109
python/gdata/codesearch/service.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (c) 2007 Benoit Chesneau <benoitc@metavers.net>
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software for any
|
||||
# purpose with or without fee is hereby granted, provided that the above
|
||||
# copyright notice and this permission notice appear in all copies.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
|
||||
"""CodesearchService extends GDataService to streamline Google Codesearch
|
||||
operations"""
|
||||
|
||||
|
||||
__author__ = 'Benoit Chesneau'
|
||||
|
||||
|
||||
import atom
|
||||
import gdata.service
|
||||
import gdata.codesearch
|
||||
|
||||
|
||||
class CodesearchService(gdata.service.GDataService):
|
||||
"""Client extension for Google codesearch service"""
|
||||
|
||||
def __init__(self, email=None, password=None, source=None,
|
||||
server='www.google.com', additional_headers=None, **kwargs):
|
||||
"""Creates a client for the Google codesearch service.
|
||||
|
||||
Args:
|
||||
email: string (optional) The user's email address, used for
|
||||
authentication.
|
||||
password: string (optional) The user's password.
|
||||
source: string (optional) The name of the user's application.
|
||||
server: string (optional) The name of the server to which a connection
|
||||
will be opened. Default value: 'www.google.com'.
|
||||
**kwargs: The other parameters to pass to gdata.service.GDataService
|
||||
constructor.
|
||||
"""
|
||||
gdata.service.GDataService.__init__(
|
||||
self, email=email, password=password, service='codesearch',
|
||||
source=source, server=server, additional_headers=additional_headers,
|
||||
**kwargs)
|
||||
|
||||
def Query(self, uri, converter=gdata.codesearch.CodesearchFeedFromString):
|
||||
"""Queries the Codesearch feed and returns the resulting feed of
|
||||
entries.
|
||||
|
||||
Args:
|
||||
uri: string The full URI to be queried. This can contain query
|
||||
parameters, a hostname, or simply the relative path to a Document
|
||||
List feed. The DocumentQuery object is useful when constructing
|
||||
query parameters.
|
||||
converter: func (optional) A function which will be executed on the
|
||||
retrieved item, generally to render it into a Python object.
|
||||
By default the CodesearchFeedFromString function is used to
|
||||
return a CodesearchFeed object. This is because most feed
|
||||
queries will result in a feed and not a single entry.
|
||||
|
||||
Returns :
|
||||
A CodesearchFeed objects representing the feed returned by the server
|
||||
"""
|
||||
return self.Get(uri, converter=converter)
|
||||
|
||||
def GetSnippetsFeed(self, text_query=None):
|
||||
"""Retrieve Codesearch feed for a keyword
|
||||
|
||||
Args:
|
||||
text_query : string (optional) The contents of the q query parameter. This
|
||||
string is URL escaped upon conversion to a URI.
|
||||
Returns:
|
||||
A CodesearchFeed objects representing the feed returned by the server
|
||||
"""
|
||||
|
||||
query=gdata.codesearch.service.CodesearchQuery(text_query=text_query)
|
||||
feed = self.Query(query.ToUri())
|
||||
return feed
|
||||
|
||||
|
||||
class CodesearchQuery(gdata.service.Query):
|
||||
"""Object used to construct the query to the Google Codesearch feed. here only as a shorcut"""
|
||||
|
||||
def __init__(self, feed='/codesearch/feeds/search', text_query=None,
|
||||
params=None, categories=None):
|
||||
"""Constructor for Codesearch Query.
|
||||
|
||||
Args:
|
||||
feed: string (optional) The path for the feed. (e.g. '/codesearch/feeds/search')
|
||||
text_query: string (optional) The contents of the q query parameter. This
|
||||
string is URL escaped upon conversion to a URI.
|
||||
params: dict (optional) Parameter value string pairs which become URL
|
||||
params when translated to a URI. These parameters are added to
|
||||
the query's items.
|
||||
categories: list (optional) List of category strings which should be
|
||||
included as query categories. See gdata.service.Query for
|
||||
additional documentation.
|
||||
|
||||
Yelds:
|
||||
A CodesearchQuery object to construct a URI based on Codesearch feed
|
||||
"""
|
||||
|
||||
gdata.service.Query.__init__(self, feed, text_query, params, categories)
|
Reference in New Issue
Block a user