m
xFc           @   s  d  Z  d Z d k Z y d k l Z WnC e j
 o7 y d k Z Wql e j
 o d k l Z ql Xn Xd k Z d k	 Z
 d k Z d k Z d k
 Z
 d Z d e f d     YZ d e f d	     YZ d
 e i i f d     YZ d e i i f d     YZ d S(   s  GBaseService extends the GDataService to streamline Google Base operations.

  GBaseService: Provides methods to query feeds and manipulate items. Extends 
                GDataService.

  DictionaryToParamList: Function which converts a dictionary into a list of 
                         URL arguments (represented as strings). This is a 
                         utility function used in CRUD operations.
s   api.jscudder (Jeffrey Scudder)N(   s   cElementTree(   s   ElementTrees,   http://www.google.com/base/feeds/items/batcht   Errorc           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t1   /mit/freeculture/lib/python/gdata/base/service.pyR    0   s   t   RequestErrorc           B   s   t  Z RS(   N(   R   R   (    (    (    R   R   4   s   t   GBaseServicec           B   s  t  Z d  Z e e e d e e d  Z d   Z d   Z d   Z d   Z e	 e e d d Z
 e d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e e e d  Z e e d  Z e e e i i d  Z e i i d  Z RS(   s#   Client for the Google Base service.s   base.google.comc         C   sD   t  i i i |  d | d | d d d | d | d | | |  _
 d  S(   Nt   emailt   passwordt   servicet   gbaset   sourcet   servert   additional_headers(   t   gdataR   t   GDataServicet   __init__t   selfR   R   R
   R   R   t   api_key(   R   R   R   R
   R   R   R   (    (    R   R   ;   s
    
c         C   s1   t  |  i t  p h  |  _ n | |  i d <d  S(   Ns   X-Google-Key(   t
   isinstanceR   R   t   dictR   (   R   R   (    (    R   t
   _SetAPIKeyD   s    c         C   s   |  i |  d  S(   N(   R   R   R   (   R   R   (    (    R   t   __SetAPIKeyI   s    c         C   s'   d |  i j o d  Sn |  i d Sd  S(   Ns   X-Google-Key(   R   R   t   None(   R   (    (    R   t
   _GetAPIKeyL   s    c         C   s
   |  i   S(   N(   R   R   (   R   (    (    R   t   __GetAPIKeyR   s    t   docs6   Get or set the API key to be included in all requests.c         C   sU   |  i | d | } | o | Sn. t | t i  o t i	 i
 | i    Sn | S(   s$  Performs a style query and returns a resulting feed or entry.

    Args:
      uri: string The full URI which be queried. Examples include
          '/base/feeds/snippets?bq=digital+camera', 
          'http://www.google.com/base/feeds/snippets?bq=digital+camera'
          '/base/feeds/items'
          I recommend creating a URI using a query class.
      converter: func (optional) A function which will be executed on the
          server's response. Examples include GBaseItemFromString, etc. 

    Returns:
      If converter was specified, returns the results of calling converter on
      the server's response. If converter was not specified, and the result
      was an Atom Entry, returns a GBaseItem, by default, the method returns
      the result of calling gdata.service's Get method.
    t	   converterN(   R   t   Gett   uriR   t   resultR   t   atomt   EntryR   t   baset   GBaseItemFromStringt   ToString(   R   R   R   R   (    (    R   t   QueryX   s     c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseSnippetFeedFromString(   R   R   (    (    R   t   QuerySnippetsFeedr   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseItemFeedFromString(   R   R   (    (    R   t   QueryItemsFeedu   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseAttributesFeedFromString(   R   R   (    (    R   t   QueryAttributesFeedx   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseItemTypesFeedFromString(   R   R   (    (    R   t   QueryItemTypesFeed{   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseLocalesFeedFromString(   R   R   (    (    R   t   QueryLocalesFeed~   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    R!   (   R   R   (    (    R   t   GetItem   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseSnippetFromString(   R   R   (    (    R   t
   GetSnippet   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseAttributeEntryFromString(   R   R   (    (    R   t   GetAttribute   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GBaseItemTypeEntryFromString(   R   R   (    (    R   t   GetItemType   s    c         C   s   |  i | d t i i S(   NR   (   R   R   R   R   R    t   GDataEntryFromString(   R   R   (    (    R   t	   GetLocale   s    c      	   C   s]   |  i | d d | d | d | } | o- t | t i	  o t
 i i | i    Sn | S(   s&  Adds an item to Google Base.

    Args: 
      new_item: ElementTree._Element A new item which is to be added to 
                Google Base.
      url_params: dict (optional) Additional URL parameters to be included
                  in the insertion request. 
      escape_params: boolean (optional) If true, the url_parameters will be
                     escaped before they are included in the request.
      converter: func (optional) Function which is executed on the server's
          response before it is returned. Usually this is a function like
          GBaseItemFromString which will parse the response and turn it into
          an object.

    Returns:
      If converter is defined, the results of running converter on the server's
      response. Otherwise, it will be a GBaseItem.
    s   /base/feeds/itemst
   url_paramst   escape_paramsR   N(   R   t   Postt   new_itemR7   R8   R   t   responseR   R   R   R   R    R!   R"   (   R   R:   R7   R8   R   R;   (    (    R   t
   InsertItem   s     c         C   s&   |  i d | i d  d | d | S(   s  Removes an item with the specified ID from Google Base.

    Args:
      item_id: string The ID of the item to be deleted. Example:
               'http://www.google.com/base/feeds/items/13185446517496042648'
      url_params: dict (optional) Additional URL parameters to be included
                  in the deletion request.
      escape_params: boolean (optional) If true, the url_parameters will be
                     escaped before they are included in the request.

    Returns:
      True if the delete succeeded.
    s   /%ss   http://www.google.com/R7   R8   N(   R   t   Deletet   item_idt   lstripR7   R8   (   R   R>   R7   R8   (    (    R   t
   DeleteItem   s     c      	   C   s]   |  i | | d | d | d | } | o- t | t	 i
  o t i i | i    Sn | S(   s  Updates an existing item.

    Args:
      item_id: string The ID of the item to be updated.  Example:
               'http://www.google.com/base/feeds/items/13185446517496042648'
      updated_item: string, ElementTree._Element, or ElementWrapper containing
                    the Atom Entry which will replace the base item which is 
                    stored at the item_id.
      url_params: dict (optional) Additional URL parameters to be included
                  in the update request.
      escape_params: boolean (optional) If true, the url_parameters will be
                     escaped before they are included in the request.
      converter: func (optional) Function which is executed on the server's
          response before it is returned. Usually this is a function like
          GBaseItemFromString which will parse the response and turn it into
          an object.

    Returns:
      If converter is defined, the results of running converter on the server's
      response. Otherwise, it will be a GBaseItem.
    R7   R8   R   N(   R   t   Putt   updated_itemR>   R7   R8   R   R;   R   R   R   R   R    R!   R"   (   R   R>   RB   R7   R8   R   R;   (    (    R   t
   UpdateItem   s     	c         C   s   |  i | t d | S(   s,  Sends a batch request feed to the server.
    
    Args: 
      batch_feed: gdata.BatchFeed A feed containing BatchEntry elements which
          contain the desired CRUD operation and any necessary entry data.
      converter: Function (optional) Function to be executed on the server's
          response. This function should take one string as a parameter. The
          default value is GBaseItemFeedFromString which will turn the result 
          into a gdata.base.GBaseItem object.

    Returns:
      A gdata.BatchFeed containing the results.
    R   N(   R   R9   t
   batch_feedt   BASE_BATCH_URLR   (   R   RD   R   (    (    R   t   ExecuteBatch   s     (   R   R   t   __doc__R   R   R   t   _GBaseService__SetAPIKeyR   t   _GBaseService__GetAPIKeyt   propertyR   R#   R%   R'   R)   R+   R-   R.   R0   R2   R4   R6   t   TrueR<   R@   R   R    R!   RC   R&   RF   (    (    (    R   R   8   s.    																 t	   BaseQueryc           B   s/   t  Z d   Z d   Z e e e d d Z RS(   Nc         C   s   |  d S(   Nt   bq(   R   (   R   (    (    R   t   _GetBaseQuery   s    c         C   s   | |  d <d  S(   NRM   (   t
   base_queryR   (   R   RO   (    (    R   t   _SetBaseQuery   s    R   s   The bq query parameter(   R   R   RN   RP   RJ   RM   (    (    (    R   RL      s   			(   RG   t
   __author__t   urllibt	   xml.etreet   cElementTreet   ElementTreet   ImportErrort   elementtreeR   t   atom.serviceR   t   gdata.servicet
   gdata.baseRE   t	   ExceptionR    R   R   R   R   R#   RL   (
   R   R   R    RR   RQ   RL   R   RE   R   RU   (    (    R   t   ?   s&   						