
    5i-                         d Z ddlmZmZmZmZ ddlZddlmZm	Z	m
Z
 ddlmZmZ ddlmZmZ dd	lmZmZmZ ddlZd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$y)z
This submodule contains the callables (and their helpers) that are called by
the org-based commands of the dx command-line client.
    )print_functionunicode_literalsdivisionabsolute_importN   )try_callprompt_for_ynINTERACTIVE_CLI)process_find_by_property_argsprocess_phi_param   )
DXCLIErrorerr_exit)fill	DELIMITERformat_find_resultsc                 v    | j                         } | j                  d      sd| j                         z   }|S | }|S )zGets the user ID based on the value `user_id_or_username` specified on
    the command-line, being extra lenient and lowercasing the value in all
    cases.
    zuser-)lower
startswith)user_id_or_usernameuser_ids     n/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/cli/org.pyget_user_idr      sH    
 .335))'2/5577 N &N    c                     d| i}|j                   |d<   d|v r|j                  du rd|d<   n|j                  |d<   |j                  |d<   |j                  |d<   |j
                  |d<   |S )	a5  
    Used by:
        - `dx new user`
        - `dx add member`

    PRECONDITION:
        - If /org-x/invite is being called in conjunction with /user/new, then
          `_validate_new_user_input()` has been called on `args`; otherwise,
          the parser must perform all the basic input validation.
    inviteelevelset_bill_toTallowBillableActivities	appAccessprojectAccesssuppressEmailNotification)r   r   allow_billable_activities
app_accessproject_accessno_email)r   argsorg_invite_argss      r   get_org_invite_argsr)   ,   s     !'*O#zzOG!1!1T!95912595S5S12#'??OK '+':':OO$37==O/0r   c                    t        | j                        }	 t        j                  j	                  | j
                  d|gi      d   d    t        d      #  Y nxY wt        j                  j                  | j
                  t        ||              | j                  rt        d| j
                  z          y t        t        dj                  || j
                                     y )Nidresultsr   zxCannot add a user who is already a member of the org. To update an existing member's permissions, use 'dx update member'zorg-zInvited {u} to {o}uo)r   username_or_user_iddxpyapiorg_find_membersorg_idr   
org_inviter)   briefprintr   format)r'   r   s     r   add_membershipr9   D   s    $223GU!!$++wi/@A)LQO   T  U  	U 	HH%8$%GHzzft{{"#d'..DKK.HIJs   3A Ac                 `    t        | j                        | j                  | j                  d}|S )N)userrevokeProjectPermissionsrevokeAppPermissions)r   r0   revoke_project_permissionsrevoke_app_permissions)r'   remove_member_argss     r   _get_org_remove_member_argsrA   V   s2    D445$($C$C $ ; ;= r   c           
          t        | j                        }	 t        j                  j	                  | j
                  d|gi      d   d    | j                   }|sZt        t        dj                  || j
                  | j                  rdnd| j                  rdnd                   t        d	      rd
}|rt        j                  j                  | j
                  t!        |             }| j"                  rt        |d          y t        t        dj                  || j
                                     t        t        dj                  |                   t%        |d   j'                               dk7  r4|d   j'                         D ]  }t        dj                  |              nt        d       t        t        dj                  |                   t%        |d   j'                               dk7  r4|d   j'                         D ]  }t        dj                  |              y t        d       y t        t        dj                  || j
                                     y # t        $ r t        d      w xY w)Nr+   r,   r   z3Cannot remove a user who is not a member of the orgzxWARNING: About to remove {u} from {o}; project permissions will{rpp} be removed and app permissions will{rap} be removed z not)r.   r/   rpprapzPlease confirmTzRemoved {u} from {o}r-   z(Removed {u} from the following projects:)r.   projectsz	{p})pz	Nonez$Removed {u} from the following apps:appsz	{a})az Aborting removal of {u} from {o})r   r0   r1   r2   r3   r4   
IndexErrorr   confirmr7   r   r8   r>   r?   r	   org_remove_memberrA   r6   lenkeys)r'   r   	confirmedresult
project_idapp_ids         r   remove_membershiprS   ^   s.   $223GP!!$++wi/@A)LQO LL Id  N  U  U55611v U ? @ 	A
 )*I++DKK,G,MO::&,$-44w$++4NOP$AHH7HSTU6*%**,-2"("4"9"9"; 8J'..:.678 h$=DDwDOPQ6&>&&()Q.$Vn113 4F'..6.234 hd5<<w$++<VWXE  PNOOPs   3I( (I=c                    t        | j                        }|i i}| j                  | j                  ||   d<   n|||   d<   | j                  dk(  xr |dk(  }| j                  | j                  dk(  ||   d<   n
|rd||   d<   | j                  | j                  dk(  ||   d<   n
|rd||   d<   | j
                  | j
                  ||   d	<   |S |rd
||   d	<   |S )Nr   MEMBERADMINtruer   Fr    Tr!   
CONTRIBUTE)r   r0   r   r#   r$   r%   )r'   current_levelr   org_set_member_access_inputadmin_to_members        r   _get_org_set_member_access_argsr\      s   $223G#*B-zz8<

#G,W58E#G,W5jjH,I'1IO%%1JNJhJhlrJr#G,-FG	JO#G,-FG"<@OOv<U#G,[9	<@#G,[9&@D@S@S#G,_= '& 
@L#G,_=&&r   c                    t        | j                        }	 t        j                  j	                  | j
                  d|gi      d   d   }|d   }t        j                  j                  | j
                  t        | |            }| j                  rt        |d          y t        t        dj                  || j
                                     y # t        $ r t        d      w xY w)Nr+   r,   r   z3Cannot update a user who is not a member of the orgr   z Updated membership of {u} in {o}r-   )r   r0   r1   r2   r3   r4   rJ   r   org_set_member_accessr\   r6   r7   r   r8   )r'   r   member_accessrY   rP   s        r   update_membershipr`      s    $223GP11$++wi?PQR[\]^_ "'*MXX++DKK,KDLY-[\F zzfTld5<<w$++<VWX  PNOOPs   3C C#c                 ~    d| j                   i}| j                  | j                  |d<   | j                  sd|d<   d|iS )Nr   r   Tdescribequery)r   with_billable_activitiesr6   )r'   find_orgs_inputs     r   _get_find_orgs_argsrf      sI    

+O$$0595R5R12::&*
#_%%r   c           	         t        j                  t        |       d         }| j                  r(t	        t        j
                  t        |                   y | j                  r|D ]  }t	        |d           y |D ]I  }t	        dj                  |d   | j                  rt        | j                        nd|d   d                K y )Nrc   r+   z
{o}{d1}{n}z : rb   name)r/   d1n)r1   	find_orgsrf   jsonr7   dumpslistr6   r8   	delimiterr   )r'   res_iterress      r   rk   rk      s    ~~1$7@AHyydjjh()	 	C#d)	  	C,%%d)15Idnn-Uj/&) &  	r   c                     t        t        j                  | j                  | j                  | j
                         }t        | |       y )N)r4   r   rb   )r   r1   r3   r4   r   r6   r   r'   r,   s     r   r3   r3      s4    t,,T[[

bfblbl^lnGg&r   c                    | j                   t        rt        d      | _         | j                   t        d       | j                  | j                   | j
                  | j                  dd}t        t        j                  j                  |      }| j                  rt        |d          y t        d| j                   z   dz   |d   z   dz          y )	Nz$Enter descriptive name for new org: z2No org name supplied and input is not interactive.)memberListVisibilityrestrictProjectTransfer)handlerh   policiesr+   zCreated new org called "z" ())rh   r
   inputr   rw   member_list_visibilityproject_transfer_abilityr   r1   r2   org_newr6   r7   )r'   org_new_inputresps      r   new_orgr      s    yy_@A	yyEF#{{DII:>:U:U=A=Z=Z"\]M DHH$$m4Dzzd4j(4994u<tDzICOPr   c                    i }| j                   | j                   |d<   | j                  | j                  |d<   t        d | j                  | j                  | j
                  | j                  fD              s| j                  i |d<   | j                  | j                  |d   d<   | j                  | j                  |d   d<   | j
                  dk(  r	d|d   d<   n| j                  dk(  rd	|d   d<   | j                  | j                  d
k(  |d   d<   | j                  %	 t        j                  | j                        |d<   |S |S #  t        d       Y |S xY w)Nrh   samlIdPc              3   $   K   | ]  }|d v 
 yw))NFN ).0policys     r   	<genexpr>z'_get_org_update_args.<locals>.<genexpr>   s       b66&  bs   rx   ru   rv   TjobReuseFrW    detailedJobMetricsCollectDefaultjobLogsForwardingz1Invalid JSON input for --job-logs-forwarding-json)rh   saml_idpanyr{   r|   enable_job_reusedisable_job_reuse$detailed_job_metrics_collect_defaultjob_logs_forwarding_jsonrl   loadsr   )r'   org_update_inputss     r   _get_org_update_argsr      s   yy$(II&!}} '+}})$
  bd6Q6QSWSpSprv  sH  sH  JN  J`  J`  6a  b  b  fj  fO  fO  f[(**%"".@D@[@[*%&<=$$0CGC`C`*%&?@$48*%j1			4	'49*%j100<LPLuLuyL*%&HI$$0	J59ZZ@]@]5^12 	JHIs   !"E Ec                     t        |       }t        t        j                  j                  | j
                  |      }| j                  rt        |d          y t        t        dj                  |d                      y )Nr+   zUpdated {o})r/   )
r   r   r1   r2   
org_updater4   r6   r7   r   r8   )r'   r   rq   s      r   
update_orgr   	  s\    ,T2
488&&5F
GCzzc$id=''#d)'456r   c                    t        t        |        t        t        |        	 t        j                  | j
                  | j                  d| j                  | j                  | j                  | j                   | j                  | j                  | j                  | j                  | j                        }t#        |        y #  t!                Y xY w)Nglob)r4   rh   	name_modeids
propertiestagsrb   publiccreated_aftercreated_beforeregioncontainsPHI)r   r   r   r1   org_find_projectsr4   rh   r   r   tagr6   r   r   r   r   r   r   r   rs   s     r   r   r     s    *D1%
(($))W]-1XX$//X\X`X`7;zz>047;7I7I8<8K8K04595E5EG g&
s   BB? ?Cc                     	 t        j                  | j                  | j                  d| j                   | j
                  | j                        }t        |        y #  t                Y xY w)Nr   )r4   rh   r   rb   r   r   )	r1   org_find_appsr4   rh   r6   r   r   r   r   rs   s     r   r   r   #  s[    $$DKK*.))/537::~373E3E484G4GI g&
s   AA A+)%__doc__
__future__r   r   r   r   r1   rC   r   r	   r
   parsersr   r   
exceptionsr   r   dxpy.utils.printingr   r   r   rl   r   r)   r9   rA   rS   r\   r`   rf   rk   r3   r   r   r   r   r   r   r   r   <module>r      s{   " S R  6 6 E / F F 
0K$'YT':Y&	&"'
Q$:7'"
'r   