o
    d`6                     @   s   d Z ddlZddlZddlmZ ddlZddlmZmZ ddl	m
Z
 ddl	mZ ddl	mZ ddl	mZ dd	lmZ ed
ZG dd deZG dd deZG dd deZG dd deZG dd deeeZdS )z;Connect to and interact with a REST server and its objects.    N)Template)urllibrange)
connection)element_containers)formats)util)
Collectionz	[a-z_]\w*c                   @   s   e Zd ZdZdS )Errorz'A general error derived from Exception.N)__name__
__module____qualname____doc__ r   r   v/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/pyactiveresource/activeresource.pyr
      s    r
   c                   @   st   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )Errorsz.Represents error lists returned by the server.c                 C   s   || _ i | _dS )zdConstructor for Errors object.

        Args:
            base: The parent resource object.
        N)baseerrors)selfr   r   r   r   __init__   s   
zErrors.__init__c                 C   
   t | jS Nlenr   r   r   r   r   size&      
zErrors.sizec                 C   r   r   r   r   r   r   r   __len__*      
zErrors.__len__c                 C   s   | j |g | dS )zAdd an error to a resource object's attribute.

        Args:
            attribute: The attribute to add the error to.
            error: The error string to add.
        Returns:
            None
        N)r   
setdefaultappend)r   	attributeerrorr   r   r   add-   s   	z
Errors.addc                 C   s   |  d| dS )zAdd an error to the base resource object rather than an attribute.

        Args:
            error: the error string to add.
        Returns:
            None
        r   N)r#   )r   r"   r   r   r   add_to_base8   s   zErrors.add_to_basec                 C   s
   i | _ dS )zoClear any errors that have been set.

        Args:
            None
        Returns:
            None
        N)r   r   r   r   r   clearB   s   
zErrors.clearc                 C   s`   | j j }|D ]%}| d }t|}||v r(| ||t|d d   q| | qd S )Nr      )	r   
attributeskeyssplitr   
underscorer#   r   r$   )r   messagesattribute_keysmessage	attr_namekeyr   r   r   
from_arrayL   s   
zErrors.from_arrayc                 C   sN   | j j }t|D ]\}}|D ]}||v r| || q| | qqd S r   )r   r'   r(   six	iteritemsr#   r$   )r   r+   r,   r/   r   r-   r   r   r   	from_hashV   s   zErrors.from_hashc                 C   sN   zt |d d }t|ts|g}W n t jy   g }Y nw | | dS )zGrab errors from an XML response.

        Args:
            xml_string: An xml errors object (e.g. '<errors></errors>')
        Returns:
            None
        r   r"   N)r   xml_to_dict
isinstancelistr
   r0   )r   
xml_stringr+   r   r   r   from_xml_   s   
zErrors.from_xmlc                 C   s   z
t |d}W n ty   i }Y nw |si }t|trBd|v s)t|dkrB|di }t|tr;| 	| dS | 
| dS | 
| dS )zGrab errors from a JSON response.

        Args:
            json_string: An json errors object (e.g. "{ 'errors': {} }")
        Returns:
            None
        utf-8r   r   N)r   json_to_dictdecode
ValueErrorr5   dictr   getr6   r0   r3   )r   json_stringdecodedr   r   r   r   	from_jsono   s   
zErrors.from_jsonc                 C   s&   | j |g }t|dkr|d S |S )a  Return the errors for the given attribute.

        Args:
            attribute: The attribute to retrieve errors for.
        Returns:
            An error string, or a list of error message strings or None
            if none exist for the given attribute.
        r&   r   )r   r>   r   )r   r!   r   r   r   r   on   s   	z	Errors.onc                 C   sP   g }t | jD ]\}}|D ]}|dkr|| q|d||f qq|S )zReturns all the full error messages in an array.

        Args:
            None
        Returns:
            An array of error strings.
        r    )r1   r2   r   r    join)r   r+   r/   r   r"   r   r   r   full_messages   s   zErrors.full_messagesN)r   r   r   r   r   propertyr   r   r#   r$   r%   r0   r3   r8   rA   rB   rE   r   r   r   r   r      s    	



	r   c                   @   s    e Zd ZdZdd Zdd ZdS )ClassAndInstanceMethodz@A descriptor to allow class/instance methods with the same name.c                 C   s   || _ || _d S r   )class_methodinstance_method)r   rH   rI   r   r   r   r         
zClassAndInstanceMethod.__init__c                 C   s   |rt || jS t || jS r   )getattrrI   rH   )r   instanceownerr   r   r   __get__   s   zClassAndInstanceMethod.__get__N)r   r   r   r   r   rN   r   r   r   r   rG      s    rG   c                   @   sZ  e Zd ZdZdd Zedd Zdd Zdd	 Zeeed
dZ	dd Z
dd Zee
ed
dZdd Zdd Zeeed
dZdd Zdd Zeeed
dZdd Zdd Zeeed
dZd d! Zd"d# Zeeed
d$Zd%d& Zd'd( Zeeed
d)Zd*d+ Zd,d- Zeeed
d.Zd/d0 Zd1d2 Z eee d
d3Z!d;d4d5Z"d6d7 Z#d8d9 Z$ee#e$d
d:Z%d
S )<ResourceMetazA metaclass for ActiveResource objects.

    Provides a separate namespace for configuration objects (user,password,
    site, etc)c                 C   sf   d|vs|d st ||d< d|vs|d s t |d |d< t| |||}d|v r1|d |_|S )zCreate a new class.

        Args:
            mcs: The metaclass.
            name: The name of the class.
            bases: List of base classes from which mcs inherits.
            new_attrs: The class attribute dictionary.
        	_singular_plural_site)r   r*   	pluralizetype__new__site)mcsnamebases	new_attrsklassr   r   r   rU      s   	
zResourceMeta.__new__c                 C   sP   | j d }|tksd| jv r%| jdu r"t| j| j| j| j	| j
| _| jS |jS )z4A connection object which handles all HTTP requests.r&   _connectionN)__mro__object__dict__r\   r   
ConnectionrV   userpasswordtimeoutformat)clssuper_classr   r   r   r      s   

zResourceMeta.connectionc                 C      | j S r   )_userre   r   r   r   get_user      zResourceMeta.get_userc                 C      d | _ || _d S r   )r\   rh   re   valuer   r   r   set_user   rJ   zResourceMeta.set_userNzA username for HTTP Basic Auth.c                 C   rg   r   )	_passwordri   r   r   r   get_password   rk   zResourceMeta.get_passwordc                 C   rl   r   )r\   rp   rm   r   r   r   set_password   rJ   zResourceMeta.set_passwordzA password for HTTP Basic Auth.c                 C   rg   r   )rR   ri   r   r   r   get_site   rk   zResourceMeta.get_sitec                 C   sP   |d ur t j|}|jrt j|j| _|jr t j|j| _d | _|| _	d S r   )
r   parseurlparseusernameunquoterh   rb   rp   r\   rR   )re   rn   partsr   r   r   set_site   s   
zResourceMeta.set_sitez!The base REST site to connect to.c                 C   rg   r   _headersri   r   r   r   get_headers  rk   zResourceMeta.get_headersc                 C   
   || _ d S r   rz   rm   r   r   r   set_headers  r   zResourceMeta.set_headerszHTTP headers.c                 C   rg   r   )_timeoutri   r   r   r   get_timeout  rk   zResourceMeta.get_timeoutc                 C   rl   r   )r\   r   rm   r   r   r   set_timeout  rJ   zResourceMeta.set_timeoutz"Socket timeout for HTTP operationsc                 C   rg   r   )_formatri   r   r   r   
get_format  rk   zResourceMeta.get_formatc                 C   rl   r   )r\   r   rm   r   r   r   
set_format  rJ   zResourceMeta.set_formatz.A format object for encoding/decoding requestsc                 C   rg   r   rQ   ri   r   r   r   
get_plural!  rk   zResourceMeta.get_pluralc                 C   r}   r   r   rm   r   r   r   
set_plural$  r   zResourceMeta.set_pluralz$The plural name of this object type.c                 C   rg   r   rP   ri   r   r   r   get_singular*  rk   zResourceMeta.get_singularc                 C   r}   r   r   rm   r   r   r   set_singular-  r   zResourceMeta.set_singularz&The singular name of this object type.c                 C   s"   t | dr| jS tj| jd S )z7Return the prefix source, by default derived from site._prefix_source   )hasattrr   r   rt   urlsplitrV   ri   r   r   r   get_prefix_source3  s   
zResourceMeta.get_prefix_sourcec                 C   s
   || _ dS )z>Set the prefix source, which will be rendered into the prefix.N)r   rm   r   r   r   set_prefix_source:  r   zResourceMeta.set_prefix_sourcez+prefix for lookups for this type of object.c                 C   s
   |  |S )z+Return the rendered prefix for this object.)_prefix)re   optionsr   r   r   prefixA  r   zResourceMeta.prefixc                 C   rg   r   _primary_keyri   r   r   r   get_primary_keyE  rk   zResourceMeta.get_primary_keyc                 C   r}   r   r   rm   r   r   r   set_primary_keyH  r   zResourceMeta.set_primary_keyz5Name of attribute that uniquely identies the resourcer   )&r   r   r   r   rU   rF   r   rj   ro   ra   rq   rr   rb   rs   ry   rV   r|   r~   headersr   r   rc   r   r   rd   r   r   pluralr   r   singularr   r   prefix_sourcer   r   r   primary_keyr   r   r   r   rO      sp    


rO   c                   @   s  e Zd ZdZdZejZdZdZ	dZ
dZdZdZdvddZedvddZedwdd	Zed
d Zedd Zedd Zedd Zedd Zedd ZedwddZedwddZedi fddZedd ZedvddZedvd d!Zed"d# Zed$d% Zedxd'd(Z edxd)d*Z!ed+d, Z"ed-d. Z#ed/d0 Z$edwd1d2Z%d3d4 Z&d5d6 Z'dyd9d:Z(dzd;d<Z)d=d> Z*d?d@ Z+dAdB Z,dCdD Z-dEdF Z.dGdH Z/dIdJ Z0e1e/e0ddKZ2dLdM Z3dNdO Z4dPdQ Z5e6j7rdRdS Z8ndTdU Z9dVdW Z:dXdY Z;edZd[ Z<e		7d{d\d]Z=d^d_ Z>d`da Z?dbdc Z@dxdddeZAdxdfdgZBdhdi ZCdjdk ZDeEdldmZFeEdndoZGeEdpdqZHeEdrdsZIeEdtduZJdS )|ActiveResourcez$Represents an activeresource object.Nidc                 C   sJ   |du ri }| j | _i | _|r|| _ni | _| | t| | _d| _dS )a  Initialize a new ActiveResource object.

        Args:
            attributes: A dictionary of attributes which represent this object.
            prefix_options: A dict of prefixes to add to the request for
                            nested URLs.
        NT)	__class__r[   r'   _prefix_options_updater   r   _initialized)r   r'   prefix_optionsr   r   r   r   [  s   


zActiveResource.__init__c                 K   s*   |r| j |fi |S | jdd|i|S )a  Core method for finding resources.

        Args:
            id_: A specific resource to retrieve.
            from_: The path that resources will be fetched from.
            kwargs: any keyword arguments for query.

        Returns:
            An ActiveResource object.
        Raises:
            connection.Error: On any communications errors.
            Error: On any other errors.
        from_Nr   )_find_single_find_every)re   id_r   kwargsr   r   r   findp  s   zActiveResource.findc                 K   s$   | j dd|i|}|r|d S dS )a  Core method for finding resources.

        Args:
            from_: The path that resources will be fetched from.
            kwargs: any keyword arguments for query.

        Returns:
            The first resource from the list of returned resources or None if
            none are found.
        Raises:
            connection.Error: On any communications errors.
            Error: On any other errors.
        r   r   Nr   )r   )re   r   r   	resourcesr   r   r   
find_first  s   zActiveResource.find_firstc                 K   s   |  ||S )ac  Get a single resource from a specific URL.

        Args:
            from_: The path that resources will be fetched from.
            kwargs: Any keyword arguments for query.
        Returns:
            An ActiveResource object.
        Raises:
            connection.Error: On any communications errors.
            Error: On any other errors.
        )	_find_one)re   r   r   r   r   r   find_one  s   zActiveResource.find_onec                 K   sJ   |  |\}}| |||}z| j|| j}W dS  tjy$   Y dS w )a   Check whether a resource exists.

        Args:
            id_: The id or other key which specifies a unique object.
            kwargs: Any keyword arguments for query.
        Returns:
            True if the resource is found, False otherwise.
        TF)_split_options_element_pathr   headr   r
   )re   r   r   r   query_optionspath_r   r   r   exists  s   
zActiveResource.existsc                 C   s   | |}|   |S )a   Create and save a resource with the given attributes.

        Args:
            attributes: A dictionary of attributes which represent this object.
        Returns:
            The new resource (which may or may not have been saved successfully).
        )save)re   r'   resourcer   r   r   create  s   	zActiveResource.createc                 C   sB   i }i }t |D ]\}}||  v r|||< q	|||< q	||gS )zSplit prefix options and query options.

        Args:
            options: A dictionary of prefix and/or query options.
        Returns:
            A tuple containing (prefix_options, query_options)
        )r1   r2   _prefix_parameters)re   r   r   r   r/   rn   r   r   r   r     s   


zActiveResource._split_optionsc                 K   s4   |  |\}}| |||}| | j|| j|S )a<  Get a single object from the default URL.

        Args:
            id_: The id or other key which specifies a unique object.
            kwargs: Any keyword arguments for the query.
        Returns:
            An ActiveResource object.
        Raises:
            ConnectionError: On any error condition.
        )r   r   _build_objectr   get_formattedr   )re   r   r   r   r   r   r   r   r   r     s
   zActiveResource._find_singlec                 C   s$   ||  | }| | j|| jS )aJ  Find a single resource from a one-off URL.

        Args:
            from_: The path from which to retrieve the resource.
            query_options: Any keyword arguments for the query.
        Returns:
            An ActiveResource object.
        Raises:
            connection.ConnectionError: On any error condition.
        )_query_stringr   r   r   r   )re   r   r   r   r   r   r   r     s   zActiveResource._find_onec                 K   sj   |  |\}}|r|| || | }d}n| ||}| j|| j}| j|j	}| 
|||jS )zGet all resources.

        Args:
            from_: (optional) The path from which to retrieve the resource.
            kwargs: Any keyword arguments for the query.
        Returns:
            A list of resources.
        N)r   updater   _collection_pathr   r>   r   rd   r;   body_build_collection)re   r   r   r   r   r   responseobjsr   r   r   r     s   

zActiveResource._find_everyc                 C   s
   | ||S )a3  Create an object or objects from the given resource.

        Args:
            attributes: A dictionary representing a resource.
            prefix_options: A dict of prefixes to add to the request for
                            nested URLs.
        Returns:
            An ActiveResource object.
        r   )re   r'   r   r   r   r   r     s   
zActiveResource._build_objectc                    s6   t |tr	|g}n
 fdd|D }t|d|idS )a  Create a Collection of objects from the given resources.

        Args:
            elements: A list of dictionaries representing resources.
            prefix_options: A dict of prefixes to add to the request for
                            nested URLs.
            headers: The response headers that came with the resources.
        Returns:
            A Collection of ActiveResource objects.
        c                 3   s    | ]	}  |V  qd S r   )r   ).0elre   r   r   r   	<genexpr>1  s    
z3ActiveResource._build_collection.<locals>.<genexpr>r   )metadata)r5   r=   r	   )re   elementsr   r   r   r   r   r     s   
z ActiveResource._build_collectionc                 C   s   |r	dt | S dS )zReturn a query string for the given options.

        Args:
            query_options: A dictionary of query keys/values.
        Returns:
            A string containing the encoded query.
        ? )r   to_query)re   r   r   r   r   r   =  s   	zActiveResource._query_stringc                 C   s&   d|  || j|| jj| |d S )a  Get the element path for the given id.

        Examples:
            Comment.element_path(1, {'post_id': 5}) -> /posts/5/act
        Args:
            id_: The id of the object to retrieve.
            prefix_options: A dict of prefixes to add to the request for
                            nested URLs.
            query_options: A dict of items to add to the query string for
                           the request.
        Returns:
            The path (relative to site) to the element formatted with the query.
        z0%(prefix)s/%(plural)s/%(id)s.%(format)s%(query)s)r   r   r   rd   queryr   rQ   rd   	extensionr   )re   r   r   r   r   r   r   r   K  s   zActiveResource._element_pathc                 C   s$   d|  || j| jj| |d S )a  Get the collection path for this object type.

        Examples:
            Comment.collection_path() -> /comments.xml
            Comment.collection_path(query_options={'active': 1})
                -> /comments.xml?active=1
            Comment.collection_path({'posts': 5})
                -> /posts/5/comments.xml
        Args:
            prefix_options: A dict of prefixes to add to the request for
                            nested URLs
            query_options: A dict of items to add to the query string for
                           the request.
        Returns:
            The path (relative to site) to this type of collection.
        z)%(prefix)s/%(plural)s.%(format)s%(query)s)r   r   rd   r   r   )re   r   r   r   r   r   r   a  s   zActiveResource._collection_pathc                 C   s8   |  |\}}d| || j|| jj| |d }|S )a  Get the collection path for this resource type.

        Args:
            method_name: The HTTP method being used.
            options: A dictionary of query/prefix options.
        Returns:
            The path (relative to site) to this type of collection.
        z9%(prefix)s/%(plural)s/%(method_name)s.%(format)s%(query)sr   r   method_namerd   r   )r   r   rQ   rd   r   r   )re   r   r   r   r   r   r   r   r   _custom_method_collection_urly  s   
z,ActiveResource._custom_method_collection_urlc                 K      |  ||}| j|| jS zGet a nested resource or resources.

        Args:
            method_name: the nested resource to retrieve.
            kwargs: Any keyword arguments for the query.
        Returns:
            A dictionary representing the returned data.
        )r   r   r   r   re   r   r   urlr   r   r   
_class_get     
zActiveResource._class_get    c                 K      |  ||}| j|| j|S )a(  Get a nested resource or resources.

        Args:
            method_name: the nested resource to retrieve.
            body: The data to send as the body of the request.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r   r   postr   re   r   r   r   r   r   r   r   _class_post     zActiveResource._class_postc                 K   r   )a)  Update a nested resource or resources.

        Args:
            method_name: the nested resource to update.
            body: The data to send as the body of the request.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r   r   putr   r   r   r   r   
_class_put  r   zActiveResource._class_putc                 K   r   zDelete a nested resource or resources.

        Args:
            method_name: the nested resource to delete.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r   r   deleter   r   r   r   r   _class_delete  r   zActiveResource._class_deletec                 K   r   zPredicate a nested resource or resources exists.

        Args:
            method_name: the nested resource to predicate exists.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r   r   r   r   r   r   r   r   _class_head  r   zActiveResource._class_headc                 C   sR   | j }t|}t }|j|D ]}dD ]}| | r%|| |  qq|S )a>  Return a list of the parameters used in the site prefix.

        e.g. /objects/$object_id would yield ['object_id']
             /objects/${object_id}/people/$person_id/ would yield
             ['object_id', 'person_id']
        Args:
            None
        Returns:
            A set of named parameters.
        )bracednamed)r   r   setpatternfinditer	groupdictr#   )re   r   templater(   match
match_typer   r   r   r     s   z!ActiveResource._prefix_parametersc                    sZ    du ri  t dd| j}t|}|  }t fdd|D  | }t dd|S )zReturn the prefix for this object type.

        Args:
            options: A dictionary containing additional prefixes to prepend.
        Returns:
            A string containing the path to this element.
        Nz/$r   c                    s   g | ]
}|  |d fqS )r   )r>   )r   kr   r   r   
<listcomp>  s    z*ActiveResource._prefix.<locals>.<listcomp>z^/+)resubr   r   r   r=   safe_substitute)re   r   r   r   r(   r   r   r   r   r     s   	
zActiveResource._prefixc                 C   s   i }t | jD ]7\}}t|tr/g }|D ]}t|tr$||  q|| q|||< qt|tr;| ||< q|||< q|S )z#Convert the object to a dictionary.)r1   r2   r'   r5   r6   r   r    to_dict)r   valuesr/   rn   	new_valueitemr   r   r   r     s   




zActiveResource.to_dictc                 K   s   t | d| jjj di |S )Nto_r   )rK   r[   rd   r   )r   r   r   r   r   encode
  s   zActiveResource.encodeTFc                 C   s"   |s| j }tj|  ||||dS )ad  Convert the object to an xml string.

        Args:
            root: The name of the root element for xml output.
            header: Whether to include the xml header.
            pretty: Whether to "pretty-print" format the output.
            dasherize: Whether to dasherize the xml attribute names.
        Returns:
            An xml string.
        )rootheaderpretty	dasherize)rP   r   to_xmlr   )r   r   r   r   r   r   r   r   r     s   zActiveResource.to_xmlc                 C   s&   |dkr| j }tj|  |ddS )z$Convert the object to a json string.T)r   r9   )rP   r   to_jsonr   r   )r   r   r   r   r   r     s   zActiveResource.to_jsonc                 C   s.   | j j| | j| j| j j}| | dS )zConnect to the server and update this resource's attributes.

        Args:
            None
        Returns:
            None
        N)r[   r   r   r   r   r   r   r   )r   r'   r   r   r   reload$  s
   zActiveResource.reloadc              
   C   s  z;| j   | jr| jjj| | j| j| jj| 	 d}n| jjj
| | j| jj| 	 d}| |}|r:|| _W n3 tjyn } z&| jjtjkrT| j |jj n| jjtjkrc| j |jj W Y d}~dS d}~ww z
| jj|j}W n tjy   Y dS w |r| | dS )a<  Save the object to the server.

        Args:
            None
        Returns:
            True on success, False on ResourceInvalid errors (sets the errors
            attribute if an <errors> object is returned by the server).
        Raises:
            connection.Error: On any communications problems.
        )dataNFT)r   r%   r   r[   r   r   r   r   r   r   r   r   _id_from_responseResourceInvalidrd   r   	XMLFormatr8   r   r   
JSONFormatrA   r;   r
   r   )r   r   new_iderrr'   r   r   r   r   1  sB   



zActiveResource.savec                 C   s   t | j S )zReturns True if no errors have been set.

        Args:
            None
        Returns:
            True if no errors have been set, False otherwise.
        r   r   r   r   r   is_validY  s   zActiveResource.is_validc              
   C   sR   t d|d|dd}|r'zt|dW S  ty&   |d Y S w dS )zPull the ID out of a response from a create POST.

        Args:
            response: A Response object.
        Returns:
           An id string.
        z\/([^\/]*?)(\.\w+)?$Locationlocationr   r&   N)r   searchr>   intgroupr<   )r   r   r   r   r   r   r  c  s   
z ActiveResource._id_from_responsec                 C   s$   | j j| | j| j| j j dS )zxDeletes the resource from the remote service.

        Args:
            None
        Returns:
            None
        N)r[   r   r   r   r   r   r   r   r   r   r   destroyt  s   zActiveResource.destroyc                 C   s   | j | jjS r   )r'   r>   r[   r   r   r   r   r   get_id     zActiveResource.get_idc                 C   s   || j | jj< d S r   )r'   r[   r   )r   rn   r   r   r   set_id  s   zActiveResource.set_idzValue stored in the primary keyc                 C   s&   d| j v r|| jv r| j| S t|)zRetrieve the requested attribute if it exists.

        Args:
            name: The attribute name.
        Returns:
            The attribute's value.
        Raises:
            AttributeError: if no such attribute exists.
        r'   )r_   r'   AttributeError)r   rX   r   r   r   __getattr__  s   



zActiveResource.__getattr__c                 C   sT   d| j v r!|| j v st| j|drt| || dS || j|< dS t| || dS )zSet the named attributes.

        Args:
            name: The attribute name.
            value: The attribute's value.
        Returns:
            None
        r   N)r_   rK   r   r^   __setattr__r'   )r   rX   rn   r   r   r   r    s
   
	zActiveResource.__setattr__c                 C   s   d| j | jf S )Nz%s(%s))rP   r   r   r   r   r   __repr__  r  zActiveResource.__repr__c                 C   s&   t || jrt| j|jS t| j|S r   )r5   r   cmpr   r   otherr   r   r   __cmp__  s   zActiveResource.__cmp__c                 C   s$   |j | j ko| j|jko| j|jkS r   )r   r   r   r  r   r   r   __eq__  s
   

zActiveResource.__eq__c                 C   s   t ttt| jS r   )hashtuplesortedr1   r2   r'   r   r   r   r   __hash__  s   zActiveResource.__hash__c                 C   s   t |tsdS t|D ]D\}}t |tr| |}||}n,t |trId}g }|D ]}t |trB|du r:| |}||| q*|| q*n|}|| j|< qdS )zUpdate the object with the given attributes.

        Args:
            attributes: A dictionary of attributes.
        Returns:
            None
        N)	r5   r=   r1   r2   _find_class_forr6   _find_class_for_collectionr    r'   )r   r'   r/   rn   r[   attrchildr   r   r   r     s&   






zActiveResource._updatec                 C   s   |  t|S )a  Look in the parent modules for classes matching the element name.

        One or both of element/class name must be specified.

        Args:
            collection_name: The name of the collection type.
        Returns:
            A Resource class.
        )r  r   singularize)re   collection_namer   r   r   r    s   z)ActiveResource._find_class_for_collectionc           	      C   sJ  |s|st d|st|}n|st|}| jd}tt|ddD ]o}ztd	|d|  t
jd	|d|  }W n	 tyI   Y q%w z
t||}|W   S  ty   ztd	|j|g t
jd	|j|g }W n
 ty|   Y Y q%w zt||}|W  Y   S  ty   Y Y q%w w |rtt|| fd| jiS dS )a  Look in the parent modules for classes matching the element name.

        One or both of element/class name must be specified.

        Args:
            element_name: The name of the element type.
            class_name: The class name of the element type.
            create_missing: Whether classes should be auto-created if no
                existing match is found.
        Returns:
            A Resource class.
        z1One of element_name,class_name must be specified..r   Nr   )r
   r   r*   camelizer   r)   r   r   
__import__rD   sysmodulesImportErrorrK   r  r   rT   str)	re   element_name
class_namecreate_missingmodule_pathdepthmoduler[   	submoduler   r   r   r    sH   



zActiveResource._find_class_forc              	   C   sL   |  |\}}|| j d| j|| j| j|| jjj| 	|d }|S )a  Get the element path for this type of object.

        Args:
            method_name: The HTTP method being used.
            options: A dictionary of query/prefix options.
        Returns:
            The path (relative to site) to the element formatted with the query.
        z@%(prefix)s/%(plural)s/%(id)s/%(method_name)s.%(format)s%(query)s)r   r   r   r   rd   r   )
r   r   r   r[   r   rQ   r   rd   r   r   r   r   r   r   r   r   r   r   r   _custom_method_element_url  s   	
z)ActiveResource._custom_method_element_urlc                 C   sH   |  |\}}|| j d| j|| j|| jjj| |d }|S )a%  Get the element path for creating new objects of this type.

        Args:
            method_name: The HTTP method being used.
            options: A dictionary of query/prefix options.
        Returns:
            The path (relative to site) to the element formatted with the query.
        z=%(prefix)s/%(plural)s/new/%(method_name)s.%(format)s%(query)sr   )	r   r   r   r[   r   rQ   rd   r   r   r3  r   r   r   _custom_method_new_element_url-  s   	
z-ActiveResource._custom_method_new_element_urlc                 K       |  ||}| jj|| jjS r   )r4  r[   r   r   r   r   r   r   r   r   r   r   _instance_getA     	zActiveResource._instance_getc                 K   sB   | j r
| ||}n|s|  }| ||}| jj|| jj|S )a*  Create a new resource/nested resource.

        Args:
            method_name: the nested resource to post to.
            body: The data to send as the body of the request.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r   r4  r   r5  r[   r   r   r   r   r   r   r   r   r   r   r   _instance_postM  s   
zActiveResource._instance_postc                 K   s"   |  ||}| jj|| jj|S )a  Update a nested resource.

        Args:
            method_name: the nested resource to update.
            body: The data to send as the body of the request.
            kwargs: Any keyword arguments for the query.
        Returns:
            A connection.Response object.
        )r4  r[   r   r   r   r:  r   r   r   _instance_put_  s   
zActiveResource._instance_putc                 K   r6  r   )r4  r[   r   r   r   r7  r   r   r   _instance_deletel  r9  zActiveResource._instance_deletec                 K   r6  r   )r4  r[   r   r   r   r7  r   r   r   _instance_headx  r9  zActiveResource._instance_headr   r8  r   r;  r   r<  r   r=  r   r>  )NNr   )r   )NTFT)T)NNT)Kr   r   r   r   r\   r   r  r   r{   rp   rR   r   rh   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  rF   r   r  r  r  r1   PY2r  r  r  r   r  r  r4  r5  r8  r;  r<  r=  r>  rG   r>   r   r   r   r   r   r   r   r   r   O  s    














(


1





r   )r   r   r(  stringr   r1   	six.movesr   r   pyactiveresourcer   r   r   r   pyactiveresource.collectionr	   compile
VALID_NAME	Exceptionr
   r^   r   rG   rT   rO   with_metaclassr   r   r   r   r   <module>   s&   
  