3
NZa              1   @   sR  d Z dZdZdZddlZddlZddlZddlT ddlm	Z	m
Z
mZ ddlmZ ydd	lmZ W n8 ek
r Z zeje dd
lmZ W Y ddZ[X nX G dd dejZejeee edeefeeefZejej  ej!dd\Z"Z#Z$Z%Z&Z'e&dkre	j( s4ej)de  eZ%ne	j* s4ej)de  eZ%e%ekre&dkrPej+d yddl,T ddl-m.Z. W n* ek
r   ej)d e/j0d Y nX dZ1dZ2e3e/j4Z1e'dkre5j6dddZ'e'j7 dkre8e9j: Z'ejde'  e'j7 dkrdZy e'j;d\Z<Z=dj>e<egZ'W n( e?k
rB   e'Z<dj>e'egZ'Y nX ejde'  e@dZAde< ZBejdeB  eAjCeBeDjEZFeFre1jGeA ndZ'e'dkrejd  nTejd!e'  eHjIeHe' e'eD_:ye:jJe:jKe:jLe' W n e:jMk
r   Y nX e. Z2e2jN  e1jOe2 yejd" e1jP  W n eQk
rB   Y nX e/j0d nxe	jRe&\Z3ZSeeSd# ZTej+d$ e3e/j4Z1eTjUdZVeVjN  yejd" e1jW  W n  eQk
r   e/j0d Y nX nydd%lXmXZX W n* ek
r   ej)d& e/j0d Y nX e ZYyeYjZ  W n  eQk
r8   ej)d' Y nX ej[d( ej[d) dS )*z6.0zFax Address Bookzhp-fabz$A simple fax address book for HPLIP.    N)*)utilstuimodule)input)import_module)dyn_import_modc               @   s  e Z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d Zdd Zd:ddZd;ddZdd  ZeZd!d" Zd#d$ Zd%d& ZeZd'd( ZeZd)d* ZeZd+d, ZeZd-d. Zd/d0 ZeZd1d2 Z e Z!d3d4 Z"e"Z#d5d6 Z$d7d8 Z%d9S )<Consolec             C   s,   t jj|  d| _tj | _tjd| _	d S )NzBType 'help' for a list of commands. Type 'exit' or 'quit' to quit.z	hp-fab > )
cmdCmd__init__ZintrofaxZFaxAddressBookdblogboldprompt)self r   /usr/share/hplip/fab.pyr   6   s    
zConsole.__init__c             C   s   t | j dS )z/Print a list of commands that have been enteredN)print_hist)r   argsr   r   r   do_hist=   s    zConsole.do_histc             C   s   dS )zExits from the console   r   )r   r   r   r   r   do_exitA   s    zConsole.do_exitc             C   s   dS )zExits from the consoler   r   r   )r   r   r   r   r   do_quitE   s    zConsole.do_quitc             C   s
   | j |S )z$Exit on system end of file character)r   )r   r   r   r   r   do_EOFJ   s    zConsole.do_EOFc             C   s   t jj| | dS )zGet help on commands
           'help' or '?' with no arguments prints a list of commands for which help is available
           'help <command>' or '? <command>' gives help on <command>
        N)r
   r   do_help)r   r   r   r   r   r   N   s    zConsole.do_helpc             C   s,   t jj|  g | _i | _i | _| jd dS )zInitialization before prompting user for commands.
           Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
         N)r
   r   preloopr   Z_localsZ_globalsdo_list)r   r   r   r   r    W   s
    zConsole.preloopc             C   s   t jj|  td dS )zTake care of any unfinished business.
           Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
        z
Exiting...N)r
   r   postloopr   )r   r   r   r   r"   b   s    zConsole.postloopc             C   s   |  j |j g7  _ |S )z This method is called after the line has been input but before
            it has been interpreted. If you want to modifdy the input line
            before execution (for example, variable substitution) do it here.
        )r   strip)r   liner   r   r   precmdi   s    zConsole.precmdc             C   s   |S )zIf you want to stop the console, return something that evaluates to true.
           If you want to do some post command processing, do it here.
        r   )r   stopr$   r   r   r   postcmdq   s    zConsole.postcmdc             C   s   dS )zDo nothing on empty input lineNr   )r   r   r   r   	emptylinew   s    zConsole.emptylinec             C   s   t jd d S )Nz2Unrecognized command. Use 'help' to list commands.)r   error)r   r$   r   r   r   default{   s    zConsole.defaultTFc             C   s   |sx|rt tjdj }nt tjdj }|j dkrNttjd dS |sf|rZdS tjd q|r| jj	|d k	rtjd qn| jj	|d krtjd qP qW nJ|j }|r| jj	|d k	rtjd dS n| jj	|d krtjd dS |S )	Nz?Enter the name to add to the group (<enter>=done*, c=cancel) ? zEnter name (c=cancel) ? cCanceledr   zName must not be blank.z4Name already exists. Please choose a different name.z.Name not found. Please enter a different name.)
r   r   r   r#   lowerr   redr)   r   get)r   r   fail_if_matchalt_textnicknamer   r   r   get_nickname~   s<    




zConsole.get_nicknamec             C   s   | j j }|sx|r(ttjdj }nttjdj }|j dkrXttjd dS |sp|rddS tj	d q|dkrtd dS |r||krtj	d	 qn||krtj	d
 qP qW n:|j }|r||krtj	d	 dS n||krtj	d
 dS |S )Nz4Enter the group to join (<enter>=done*, c=cancel) ? zEnter the group (c=cancel) ? r+   r,   r   z!The group name must not be blank.Allz<Cannot specify group 'All'. Please choose a different group.z6Group already exists. Please choose a different group.z0Group not found. Please enter a different group.)
r   get_all_groupsr   r   r   r#   r-   r   r.   r)   )r   r   r0   r1   
all_groupsZ	groupnamer   r   r   get_groupname   sD    





zConsole.get_groupnamec             C   s\   |rD|j  j d }|jdr,| jd dS |jdrD| jd dS | jd | jd dS )zl
        List names and/or groups.
        list [names|groups|all|]
        dir [names|groups|all|]
        r   Znamr   NZgro)r#   split
startswithdo_names	do_groups)r   r   Zscoper   r   r   r!      s    




zConsole.do_listc             C   s   | j j }tj| ttjd t|dkrtj }d|_	xFt
|j D ]6\}}|jdsJ|j||d |d	 d
j|d f qJW |j  ntd t  dS )z+
        List names.
        names
        z
Names:
r   Name
Fax NumberNotesMember of Group(s)__r   notesz, groupsz(None)N)r<   r=   r>   r?   )r   get_all_recordsr   debugr   r   lenr   	Formatterheaderlistitemsr9   addjoinoutput)r   r   all_entriesfnameer   r   r   r:      s    


(
zConsole.do_namesc             C   s   | j j }tj| ttjd t|rvtj }d	|_	x2|D ]*}|j
|djdd | j j|D f q>W |j  ntd t  dS )
z-
        List groups.
        groups
        z	
Groups:
GroupMembersz, c             S   s   g | ]}|j d s|qS )r@   )r9   ).0xr   r   r   
<listcomp>   s    z%Console.do_groups.<locals>.<listcomp>z(None)N)rQ   rR   )r   r5   r   rD   r   r   rE   r   rF   rG   rJ   rK   group_membersrL   )r   r   r6   rN   groupr   r   r   r;     s    


*
zConsole.do_groupsc             C   s  | j |dd}|sdS | jj|}tj| ttjd|  d}d}d}|d }xttjd| j }|j	 dkrttj
d	 dS | r| rtjd
 qT|s|}d}	x$|D ]}
|
dkrtjd d}	P qW |	rTP qTW |d }ttjd| j }|j	 dkrttj
d	 dS |s$|}|d r6td g }x^|d D ]R}|dkrVqDtjd| dd\}	}|	sttj
d	 dS |rD|j| qDW td x| jdddd}|j	 dkrttj
d	 dS |sP | jj }||kr.tjd tjddd\}	}|	s$ttj
d	 dS |s.q||d krJtjd q|j| qW | jj||||||| | j| t  dS )zI
        Edit an name.
        edit [name]
        modify [name]
        F)r0   Nz!
Edit/modify information for %s:
r   r   z&Fax Number (<enter>='%s', c=cancel) ? r+   r,   zFax number must not be empty.Tz0123456789-(+) *#zOInvalid characters in fax number. Fax number may only contain '0123456789-(+) 'rA   z!Notes (<enter>='%s', c=cancel) ? rB   z
Leave or Stay in a Group:
r4   zStay in group %s z*(y=yes* (stay), n=no (leave), c=cancel) ? )choice_promptz
Join New Group(s):
)r0   r1   zGroup not found.zIs this a new groupz!(y=yes* (new), n=no, c=cancel) ? zTGroup already specified. Choose a different group name or press <enter> to continue.)r3   r   r/   r   rD   r   r   r   r#   r-   r.   r)   r   enter_yes_noappendr7   r5   warnsetdo_show)r   r   r2   rP   lastname	firstnametitleZsave_faxnumfaxnumokr+   Z
save_notesrA   Z
new_groupsgans	add_groupr6   r   r   r   do_edit)  s     
 


 







zConsole.do_editc       	      C   s   | j |dd}|sdS | jj|}g }td xT|D ]L}|jds4tjd| dd\}}nq4|srttjd	 dS |r4|j	| q4W td
 xF| j
dddd}|j dkrttjd	 dS |j sP |j	| qW | jj|| t  dS )zQ
        Edit a group.
        editgrp [group]
        modifygrp [group]
        F)r0   Nz
Existing Names in Group:
r@   zShould '%s' stay in this group z*(y=yes* (stay), n=no (leave), c=cancel) ? )rX   r,   z
Add New Names to Group:
r   T)r0   r1   r+   )r7   r   rV   r   r9   r   rY   r   r.   rZ   r3   r-   update_groups)	r   r   rW   Zold_entriesZnew_entriesrP   rb   rd   r2   r   r   r   
do_editgrp  s6     


zConsole.do_editgrpc             C   s  | j |dd}|sdS ttjd|  d}d}d}xtttjdj }|j dkrfttjd dS |svtjd	 q6d}x$|D ]}|d
krtjd d}P qW |r6P q6W ttjdj }	|	j j dkrttjd dS g }
| j	j
 }xttjdj }|j dkr ttjd dS |s(P |dkrBttjd q||krtjd x6ttjdj j }|dkrtjd qXP qXW |dkrq||
krtjd q|
j| qW |
jd | j	j||||||
|	 | j| dS )zD
        Add an name.
        add [name]
        new [name]
        T)r0   Nz
Enter information for %s:
r   zFax Number (c=cancel) ? r+   r,   zFax number must not be empty.z0123456789-(+) *#zQInvalid characters in fax number. Fax number may only contain '0123456789-(+) *#'FzNotes (c=cancel) ? z,Member of group (<enter>=done*, c=cancel) ? r4   zCannot specify 'All'.zGroup not found.z%Is this a new group (y=yes*, n=no) ? nyz1Please enter 'y', 'n' or press <enter> for 'yes'.zTGroup already specified. Choose a different group name or press <enter> to continue.)r   ri   rj   )r3   r   r   r   r   r#   r-   r.   r)   r   r5   r[   rZ   r\   r]   )r   r   r2   r`   r_   r^   ra   rb   r+   rA   rB   r6   re   Z
user_inputr   r   r   do_add  sp     


 









zConsole.do_addc             C   sz   | j |dd}|sdS g }xF| jdddd}|j dkrJttjd dS |j sTP |j| qW | jj|| t  dS )	zL
        Add a group.
        addgrp [group]
        newgrp [group]
        T)r0   Nr   F)r0   r1   r+   r,   )	r7   r3   r-   r   r   r.   rZ   r   rg   )r   r   rW   Zentriesr2   r   r   r   	do_addgrpR  s     zConsole.do_addgrpc             C   s   | j j }tj| ttjd t|dkrtj }d|_	xFt
|j D ]6\}}|jdsJ|j||d |d	 d
j|d f qJW |j  t  dS )z2
        View all name data.
        view
        z
View all Data:
r   r<   Faxr>   Member of Group(s)r@   r   rA   z, rB   N)r<   rm   r>   rn   )r   rC   r   rD   r   r   rE   r   rF   rG   rH   rI   r9   rJ   rK   rL   )r   r   rM   rN   rO   rP   r   r   r   do_viewo  s    


(zConsole.do_viewc             C   s   | j |dd}|sdS | jj|}|rtj }d|_|jd|f |jd|d f |jd	|d
 f |jddj|d f |j  n
t	j
d t  dS )zW
        Show a name (all details).
        show [name]
        details [name]
        F)r0   NKeyValuezName:zFax Number:r   zNotes:rA   zMember of Group(s):z, rB   z:Name not found. Use the 'names' command to view all names.)rp   rq   )r3   r   r/   r   rF   rG   rJ   rK   rL   r   r)   r   )r   r   rO   rP   rN   r   r   r   r]     s     

zConsole.do_showc             C   s,   | j |dd}|sdS | jj| t  dS )zE
        Remove a name.
        rm [name]
        del [name]
        F)r0   N)r3   r   deleter   )r   r   r2   r   r   r   do_rm  s
     zConsole.do_rmc             C   s,   | j |dd}|sdS | jj| t  dS )zN
        Remove a group.
        rmgrp [group]
        delgrp [group]
        F)r0   N)r7   r   Zdelete_groupr   )r   r   rW   r   r   r   do_rmgrp  s
     zConsole.do_rmgrpc             C   s   t jtt dS )z
About fab.N)r   Z	log_title	__title____version__)r   r   r   r   r   do_about  s    zConsole.do_aboutc             C   sN  |j  j }|stjd dS |d }t|dkr@|d j }nd}|dkr^tjd	|  dS tjj|s|tjd
|  dS |dkrtjj	|d j }|dkrd}n4|dkrd}n&t
|djdj }d|krd}nd}|dkrtd|  | jj|\}}n&|dkr(td|  | jj|\}}|s:tj| n
| jd t  dS )z]
        Import LDIF
        import <filename> [type]
        [type] = vcf|ldif|auto
        z+You must specify a filename to import from.Nr   r   autoldifvcfvcardzInvalid type: %szFile %s not found.z.vcfz.ldifri   zbegin:vcardzImporting from LDIF file %s...zImporting from VCF file %s...r   )rx   ry   rz   r{   )r{   rz   )r#   r8   r   r)   rE   r-   ospathexistssplitextopenreadr   r   Zimport_ldifZimport_vcardr!   )r   r   filenametypZextheadrb   Z	error_strr   r   r   	do_import  sD    



zConsole.do_importN)TF)TF)&__name__
__module____qualname__r   r   r   r   r   r   r    r"   r%   r'   r(   r*   r3   r7   r!   Zdo_dirr:   r;   rf   Z	do_modifyrh   Zdo_modifygrprk   Zdo_newrl   Z	do_newgrpro   r]   Z
do_detailsrs   Zdo_delrt   Z	do_delgrprw   r   r   r   r   r   r	   4   sH   	
0
5 /gr	   F)Zhandle_device_printerZqt3zU%s GUI mode requires GUI support (try running with --qt4). Entering interactive mode.zU%s GUI mode requires GUI support (try running with --qt3). Entering interactive mode.zhp-fab(qt3))FaxAddrBookFormz,Unable to load Qt3 support. Is it installed?r   uilocsystemzUsing system locale: %sr+   utf8.z&Trying to load .qm file for %s locale.zhplip_%s.qmzName of .qm file: %szUsing default 'C' localezUsing locale: %szStarting GUI loop...z
.fabwindowzhp-fab(qt4))r   z1Fax address book disabled - Python 2.3+ required.z
User exit.r   zDone.)\rv   ru   __mod____doc__r
   Zgetoptr}   Zbase.gbaser   r   r   Zbase.sixext.movesr   	importlibr   ImportErrorrP   r   rD   Z
base.utilsr   r   r	   ZModuleZGUI_MODEZINTERACTIVE_MODEZUI_TOOLKIT_QT3ZUI_TOOLKIT_QT4ZUI_TOOLKIT_QT5modZsetUsageZUSAGE_FLAG_NONEZparseStdOptsZoptsZ
device_uriZprinter_namemodeZ
ui_toolkitr   ZcanEnterGUIModer)   ZcanEnterGUIMode4Z
set_moduleZqtZui.faxaddrbookformr   sysexitZappZaddrbookZQApplicationargvZ	user_confr/   r-   strZ
QTextCodecZlocaler8   lrT   rK   
ValueErrorZQTranslatorZtransZqm_fileloadZpropZlocalization_dirZloadedZinstallTranslatorZQLocaleZ
setDefault	setlocaleLC_ALLZ	normalizeErrorZshowZsetMainWidgetZ	exec_loopKeyboardInterruptZimport_dialogZ
ui_packager   Z	FABWindowZfabZexec_r   ZconsoleZcmdloopinfor   r   r   r   <module>   s   
	     S

















