
    bi                      ,    d Z ddlmZmZ ddlmZ d ZdS )z|
Primitive Router - Core browser operations

This router handles primitive browser operations like click, scroll, and type.
    )jsonifyrequest)require_api_keyc                    | dz   }|                     |dz   dg          t          fd                        }|                     |dz   dg          t          fd                        }|                     |dz   dg          t          fd	                        }|                     |d
z   dg          t          fd                        }|                     |dz   dg          t          fd                        }|                     |dz   dg          t          fd                        }	|                     |dz   dg          t          fd                        }
|                     |dz   dg          t          fd                        }|                     |dz   dg          t          fd                        }dS )z
    Register primitive routes with the app.

    Args:
        app: Quart app instance
        browser_navigator_getter: Function that returns the browser_navigator instance
    z
/primitivez/<page_id>/click-elementPOST)methodsc                   K   	              }|st          d| d          dfS t          j                     d{V }|                    d          }|st          d| d          dfS |                    ||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)z3Click an element on the page (primitive operation).!Browser navigator not initializederrorpage_id  NselectorzSelector required  )r   r   get_jsongetclick_element	Exceptionstr)r   browser_navigatordatar   resultebrowser_navigator_getters         6/home/byschii/byschiidev/penelope/routers/primitive.pyprimitive_click_elementz:register_primitive_routes.<locals>.primitive_click_element   s      	G 8 8 : :$ h)LY`aabbdggg )++++++++Dxx
++H X)<QQRRTWWW,::8WMMMMMMMMF6??" 	G 	G 	GSVV@@AA3FFFFFFF	G)   B AB )*B 
C	 C>C	C	z/<page_id>/click-positionc                   K   	              }|st          d| d          dfS t          j                     d{V }|                    d          }|                    d          }||t          d| d          dfS |                    |||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)	z@Click at specific coordinates on the page (primitive operation).r
   r   r   NxyX and Y coordinates requiredr   )r   r   r   r   click_positionr   r   )r   r   r   r   r    r   r   r   s          r   primitive_click_positionz;register_primitive_routes.<locals>.primitive_click_position+   s0     	G 8 8 : :$ h)LY`aabbdggg )++++++++DAAyAI)GT[\\]]_bbb,;;Aq'JJJJJJJJF6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gs)   B, AB,  +B, ,
C!6 CC!C!z/<page_id>/scroll-pagec                 r  K   	              }|st          d| d          dfS t          j                     d{V pi }|                    dd          }|                    ||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)z1Scroll the page vertically (primitive operation).r
   r   r   Ndistancei  )r   r   r   r   scroll_pager   r   )r   r   r   r%   r   r   r   s         r   primitive_scroll_pagez8register_primitive_routes.<locals>.primitive_scroll_page@   s     	G 8 8 : :$ h)LY`aabbdggg )++++++++1rDxx
D11H,887KKKKKKKKF6??" 	G 	G 	GSVV@@AA3FFFFFFF	G#   B AB 
B6 B1+B61B6z/<page_id>/type-textc                   K   	              }|st          d| d          dfS t          j                     d{V }|                    d          }|st          d| d          dfS |                    ||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)zType text at the currently focused element (primitive operation).

        IMPORTANT: An element should already be clicked/focused before calling this endpoint.
        r
   r   r   NtextText requiredr   )r   r   r   r   	type_textr   r   r   r   r   r*   r   r   r   s         r   primitive_type_textz6register_primitive_routes.<locals>.primitive_type_textQ   s     	G 8 8 : :$ h)LY`aabbdggg )++++++++D88F##D TWMMNNPSSS,66tWEEEEEEEEF6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gr   z/<page_id>/press-enterc                   K   	              }|st          d| d          dfS |                    |            d{V }t          |          S # t          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)zPress the Enter key at the currently focused element (primitive operation).

        IMPORTANT: An element should already be clicked/focused before calling this endpoint.
        r
   r   r   N)r   press_enterr   r   r   r   r   r   r   s       r   primitive_press_enterz8register_primitive_routes.<locals>.primitive_press_enterh   s      	G 8 8 : :$ h)LY`aabbdggg,88AAAAAAAAF6??" 	G 	G 	GSVV@@AA3FFFFFFF	G"   A )A 
B A?9B?Bz/<page_id>/press-tabc                 r  K   	              }|st          d| d          dfS t          j                     d{V pi }|                    dd          }|                    ||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)zPress the Tab key to move focus to the next element (primitive operation).

        IMPORTANT: The page should be in a state where Tab navigation is expected.
        r
   r   r   N
with_shiftF)r   r   r   r   	press_tabr   r   )r   r   r   r5   r   r   r   s         r   primitive_press_tabz6register_primitive_routes.<locals>.primitive_press_taby   s     	G 8 8 : :$ h)LY`aabbdggg )++++++++1rD,66J,66z7KKKKKKKKF6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gr(   z/<page_id>/paste-textc                   K   	              }|st          d| d          dfS t          j                     d{V }|                    d          }|st          d| d          dfS |                    ||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)a  Paste text at the currently focused element using clipboard (primitive operation).

        IMPORTANT: An element should already be clicked/focused before calling this endpoint.
        This is faster than type-text for long content and mimics human copy-paste behavior.
        r
   r   r   Nr*   r+   r   )r   r   r   r   
paste_textr   r   r-   s         r   primitive_paste_textz7register_primitive_routes.<locals>.primitive_paste_text   s     	G 8 8 : :$ h)LY`aabbdggg )++++++++D88F##D TWMMNNPSSS,77gFFFFFFFFF6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gr   z/<page_id>/zoom-pagec                   K   	              }|st          d| d          dfS t          j                     d{V }|                    d          }|                    d          }|                    dd          }||t          d	| d          d
fS |                    ||||            d{V }t          |          S # t
          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)z=Zoom the page at specified coordinates (primitive operation).r
   r   r   Nr   r    
zoom_level   r!   r   )r   r   r   r   	zoom_pager   r   )	r   r   r   r   r    r<   r   r   r   s	           r   primitive_zoom_pagez6register_primitive_routes.<locals>.primitive_zoom_page   sE     	G 8 8 : :$ h)LY`aabbdggg )++++++++DAA,22JyAI)GT[\\]]_bbb,66q!ZQQQQQQQQF6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gs)   C A0C ,C 
C8 C3-C83C8z/<page_id>/reset-zoomc                   K   	              }|st          d| d          dfS |                    |            d{V }t          |          S # t          $ r+}t          t          |          | d          dfcY d}~S d}~ww xY w)z7Reset page zoom to default level (primitive operation).r
   r   r   N)r   
reset_zoomr   r   r1   s       r   primitive_reset_zoomz7register_primitive_routes.<locals>.primitive_reset_zoom   s      	G 8 8 : :$ h)LY`aabbdggg,77@@@@@@@@F6??" 	G 	G 	GSVV@@AA3FFFFFFF	Gr3   N)router   )url_api_verisonappr   	primitiver   r#   r'   r.   r2   r7   r:   r?   rB   s     `          r   register_primitive_routesrG      s     ,.IYYy55xYHHG G G G _ IHG$ 	YYy66YIIG G G G _ JIG& 	YYy33fXYFFG G G G _ GFG 	YYy11F8YDDG G G G _ EDG* 	YYy33fXYFFG G G G _ GFG 	YYy11F8YDDG G G G _ EDG$ 	YYy22VHYEEG G G G _ FEG, 	YYy11F8YDDG G G G _ EDG( 	YYy22VHYEE
G 
G 
G 
G _ FE
G 
G 
G    N)__doc__quartr   r   lib.authr   rG    rH   r   <module>rM      s^     # " " " " " " " $ $ $ $ $ ${G {G {G {G {GrH   