o
    tBh                     @   sH   d dl mZ d dlmZmZ d dlZG dd deZG dd deZdS )    )
Collection)urlparseparse_qsNc                       sn   e Zd ZdZ fddZdd Zdd Zdd	 ZdddZdddZ	dddZ
 fddZ fddZ  ZS )PaginatedCollectiona  
    A subclass of Collection which allows cycling through pages of
    data through cursor-based pagination.

    :next_page_url contains a url for fetching the next page
    :previous_page_url contains a url for fetching the previous page

    You can use next_page_url and previous_page_url to fetch the next page
    of data by calling Resource.find(from_=page.next_page_url)
    c                    s   | dd}|d }t|tr&|r||j n|j}tt| j||d ntt| j|d|p0i i| d| jvr>td| 	 | jd< | jd 
dd| _| jd 
d	d| _d| _d| _d| _| d
d| _dS )zBIf given a Collection object as an argument, inherit its metadata.metadataNr   )r   resource_classzNCursor-based pagination requires a "resource_class" attribute in the metadata.
paginationnextpreviousno_iter_nextT)pop
isinstancer   updater   superr   __init__AttributeError&_PaginatedCollection__parse_paginationgetnext_page_urlprevious_page_url_next	_previous_current_iter_no_iter_next)selfargskwargsr   obj	__class__ i/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/shopify/collection.pyr      s"   

zPaginatedCollection.__init__c                 C   sz   d| j vri S | j d d| j d dd }|d u ri S i }|dD ]}|d\}}|dd ||dd < q$|S )	NheadersLinklinkz, z;    ")r   r   split)r   valuesresultvaluer$   relr    r    r!   __parse_pagination,   s   
 z&PaginatedCollection.__parse_paginationc                 C   
   t | jS )zBReturns true if the current page has any previous pages before it.)boolr   r   r    r    r!   has_previous_page:      
z%PaginatedCollection.has_previous_pagec                 C   r.   )zKReturns true if the current page has any pages beyond the current position.)r/   r   r0   r    r    r!   has_next_page>   r2   z!PaginatedCollection.has_next_pageFc                 C   *   | j r| j S |  std| | j|S )zReturns the previous page of items.

        Args:
            no_cache: If true the page will not be cached.
        Returns:
            A PaginatedCollection object with the new data set.
        zNo previous page)r   r1   
IndexError _PaginatedCollection__fetch_pager   r   no_cacher    r    r!   previous_pageB   
   z!PaginatedCollection.previous_pagec                 C   r4   )zReturns the next page of items.

        Args:
            no_cache: If true the page will not be cached.
        Returns:
            A PaginatedCollection object with the new data set.
        zNo next page)r   r3   r5   r6   r   r7   r    r    r!   	next_pageP   r:   zPaginatedCollection.next_pagec                 C   s0   | j d j|d}|s|| _| | j_| j|_|S )Nr   )from_)r   findr   r   r   )r   urlr8   r	   r    r    r!   __fetch_page^   s   z PaginatedCollection.__fetch_pagec                 #   sj    t t|  D ]}|V  q| jrdS z| js| | _|  | _| jD ]}|V  q"W dS  ty4   Y dS w )z6Iterates through all items, also fetching other pages.N)r   r   __iter__r   r   r;   r5   )r   itemr   r    r!   r@   f   s   

zPaginatedCollection.__iter__c                    s(   | j r	t| j }nd}|tt|   S )zIf fetched count all the pages.r   )r   lenr   r   __len__)r   countr   r    r!   rC   x   s   zPaginatedCollection.__len__)F)__name__
__module____qualname____doc__r   r   r1   r3   r9   r;   r6   r@   rC   __classcell__r    r    r   r!   r      s    


r   c                   @   s    e Zd ZdZdd Zdd ZdS )PaginatedIteratora  
    This class implements an iterator over paginated collections which aims to
    be more memory-efficient by not keeping more than one page in memory at a
    time.

    >>> from shopify import Product, PaginatedIterator
    >>> for page in PaginatedIterator(Product.find()):
    ...     for item in page:
    ...         do_something(item)
    ...
    # every page and the page items are iterated
    c                 C   s$   t |ts	td|| _d| j_d S )Nz8PaginatedIterator expects a PaginatedCollection instanceT)r   r   	TypeError
collectionr   )r   rL   r    r    r!   r      s   
zPaginatedIterator.__init__c                 c   s8    | j }	 |V  z|jdd}W n
 ty   Y dS w q)z1Iterate over pages, returning one page at a time.T)r8   N)rL   r;   r5   )r   current_pager    r    r!   r@      s   zPaginatedIterator.__iter__N)rE   rF   rG   rH   r   r@   r    r    r    r!   rJ      s    rJ   )	pyactiveresource.collectionr   six.moves.urllib.parser   r   cgir   objectrJ   r    r    r    r!   <module>   s
    |