3
NZ4a              B   @   sh  d Z dZdZdZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZyddlZW n ek
r   Y nX ddlT ddlmZ erejd ejd	 dd
lmZmZmZmZ ddlmZ G dd de	jZdd Zej eee ede!e"e#fe$fddd
Z%e%j&ej'dLgdgd e%j(ddg\Z)Z*Z+Z,Z-Z.ddl/m0Z0 ej1 Z2x e)D ]\Z3Z4e3dMkrZe4Z2qZW e,e!krej5 se"Z,e,e!kre-dkrejd ejd	 e,e"e#fkr:yNe%j6e*e+dej7d	fidZ*e*sejd	 ej8d e*  ye0j9de*e+Z:W n> e;k
rP Z< z ejd!e<j=  ejd	 W Y ddZ<[<X nX e:j>e? ye:j@  W n2 e;k
r   ejd" e:jA  ejd	 Y nX ej8ejBd#e:jj*  ej8ejBd$ ejCjDejCjEejCjFe2Z2yejGe2 W n2 eHk
r    eIejBd%e2  ejd	 Y nX e,e"kree:ZJzbyeJjK  W nP eLk
rb   ejd& Y n2 eMk
r Z< zejd'e<  W Y ddZ<[<X nX W de:jA  X nleId(ej1   zNe:jN ZOeI  ePeOdkrdZQx eOD ]ZReSeQePeRd ZQqW ejTeQd) d)d*d+d)ejTjUd,d+d)d*fZVeI  eIejBeVjWdN dZXxPeOD ]HZReIeVjWd0eRd  ejYeRd	 d1eRd) eRd2 f f eXeRd	 7 ZXqLW eIejBd3ePeOejYeXdf  eIejBd4 e:jZeOedd\ZXZ[Z\eIejBd5ejYeXe[eXd6 e[ f  W de:jA  X W n  eLk
r4   ejd7 Y nX nyddl]T dd8l^m_Z_ W n* ek
r|   ejd9 ejd	 Y nX e`ejaZbecjdebeed:ebefd; e.dkregjhd<d=d>Z.e.ji d>krejekjl Z.ejmd?e.  e.ji d@krdAZ<y e.jndB\ZoZpdBjqeoe<gZ.W n( erk
r:   e.ZodBjqe.e<gZ.Y nX ejmdCe.  esdZtdDeo ZuejmdEeu  etjveuewjxZyeyrebjzet nd@Z.e.d@krejmdF nTejmdGe.  e{j|e{e. e.ew_lyelj}elj~elje. W n elj;k
r   Y nX ye_jdHge*e+ZW n* e;k
r4   ejdI ejd	 Y nX ebje ej  ebj  ej8dJ ej8dK dS )Oz3.3z	hp-unloadzPhoto Card Access UtilityzAccess inserted photo cards on supported HPLIP printers. This provides an alternative for older devices that do not support USB mass storage or for access to photo cards over a network.    N)*)PY3z:This functionality is not spported in python3 environment.   )deviceutilstuimodule)cupsc               @   sX  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 Zdd Zdd Zdd  Zd!d" ZdVd$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!dWdAdBZ"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)dQdR Z*dSdT Z+dUS )XConsolec             C   sV   t jj|  d| _|| _| jj }|d |_|jr<tjd tj	d| jj
  | _d S )Nz8Type 'help' for a list of commands. Type 'exit' to quit.   zPhoto card is write protected.zpcard: %s > )cmdCmd__init__Zintropcinfowrite_protectlogZwarningboldpwdprompt)selfr   	disk_info r   /usr/share/hplip/unload.pyr   @   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_histK   s    zConsole.do_histc             C   s   dS )zExits from the consoler   r   )r   r   r   r   r   do_exitO   s    zConsole.do_exitc             C   s   dS )zExits from the consoler   r   r   )r   r   r   r   r   do_quitS   s    zConsole.do_quitc             C   s
   | j |S )z$Exit on system end of file character)r   )r   r   r   r   r   do_EOFX   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"   \   s    zConsole.do_helpc             C   s"   t jj|  g | _i | _i | _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)r   r   r   r   r#   e   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$   n   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   precmdu   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   postcmd}   s    zConsole.postcmdc             C   s   dS )zDo nothing on empty input lineNr   )r   r   r   r   	emptyline   s    zConsole.emptylinec             C   s   t tjd d S )Nz9ERROR: Unrecognized command. Use 'help' to list commands.)r   r   r   )r   r&   r   r   r   default   s    zConsole.defaultc             C   s   t jd dS )z List local directory contents.zls -lN)ossystem)r   r   r   r   r   do_ldir   s    zConsole.do_ldirc             C   s   t jd dS )z List local directory contents.zls -lN)r,   r-   )r   r   r   r   r   do_lls   s    zConsole.do_llsc             C   s
   | j |S )zSynonym for the ls command.)do_ls)r   r   r   r   r   do_dir   s    zConsole.do_dirc             C   s2  |j  j }| jjd|}d}tjdddddtjjddddf}t  ttj	|j
d d}xT| jj D ]F}|d dkrt|j
|d ddf qpt|j
|d d ddf qpW xT| jj D ]F}t|j
|d tj|d | jj|d f |d7 }||d 7 }qW ttj	d|tj|df  dS )z#List photo card directory contents.Tr         )widthmargin   )r4   r5   	alignment   NameSizeType... Z	directory/r   z% d files, %sN)r9   r:   r;   )r<   r=   )r%   lowerr   lsr   TextFormatterRIGHTr   r   r   composeZcurrent_directoriesZcurrent_filesformat_bytesclassify_file)r   r   filesZ
total_size	formatterZ	num_filesdfr   r   r   r0      s&     .zConsole.do_lsc             C   s@   | j j }|j j dkr&tj|}n
tj|}td|  dS )zgDisplay free space on photo card.
        Options:
        -h	Display in human readable format
        z-hzFreespace = %s BytesN)r   Zdfr%   r@   r   rE   commafyr   )r   r   Z	freespaceZfsr   r   r   do_df   s
    

zConsole.do_dfFc             C   sr   |j  j }| jj|}t|dkr.td n@| jj||| j| j\}}tt	j
dtj|||d | f  dS )a  Copy files from photo card to current local directory.
        Usage:
        	cp FILENAME(S)|GLOB PATTERN(S)
        Example:
        	Copy all JPEG and GIF files and a file named thumbs.db from photo card to local directory:
        	cp *.jpg *.gif thumbs.db
        r   zERROR: File(s) not found.z$
%s transfered in %d sec (%d KB/sec)i   N)r%   r@   r   match_fileslenr   Zcp_multiplecp_status_callbackrm_status_callbackr   r   r   rE   )r   r   Zremove_after_copymatched_filestotaldeltar   r   r   do_cp   s    
zConsole.do_cpc       
   	   C   s  |j  j j }d}d|kr6| jjr2tjd dS d}| jj }t  t	|dkrd|kr@d}x|D ]}t
|t	|d }qhW tj|d dd	d
dtjjdd
dd	f}t  ttj|jd d}xN|D ]F}t|jd|d  tj|d d|d |d f f ||d 7 }qW ttjdt	|tj|df  nXttjdt	|  | jj|| j| j|\}}}	ttjdtj|||d | f  ntd dS )zUnload all image files from photocard to current local directory.
        Note:
        	Subdirectories on photo card are not preserved
        Options:
        -x	Don't remove files after copy
        -p	Print unload list but do not copy or remove filesFz-xz.Photo card is write protected. -x not allowed.NTr   z-pr3   )r4   r5   r6   )r4   r5   r7   r9   r:   r;   z%sr   z%s/%s   zFound %d files to unload, %szUnloading %d files...z"
%s unloaded in %d sec (%d KB/sec)i   z&No image, audio, or video files found.)r9   r:   r;   )r@   r%   splitr   r   r   errorget_unload_listr   rN   maxr   rB   rC   r   rD   rE   unloadrO   rP   )
r   r   Zdont_removeunload_listmax_lenurH   rR   rS   was_cancelledr   r   r   	do_unload   s:    




6$(zConsole.do_unloadc             C   s>   |dkr"t   t tjd|  nt d||tj|f  d S )Nr   zCopying %s...z
Copied %s to %s (%s)...)r   r   r   r   rE   )r   srctrgsizer   r   r   rO     s    zConsole.cp_status_callbackc             C   s   t d|  d S )NzRemoving %s...)r   )r   r`   r   r   r   rP     s    zConsole.rm_status_callbackc             C   sn   | j jrtjd dS |j j }| j j|}t|dkrDtd nx|D ]}| j j	|d qJW | j j
  dS )zRemove files from photo card.z.Photo card is write protected. rm not allowed.Nr   zERROR: File(s) not found.F)r   r   r   rW   r%   r@   rM   rN   r   ZrmrA   )r   r   rQ   rJ   r   r   r   do_rm  s    


zConsole.do_rmc             C   s&   | j jrtjd dS | j|d dS )zMove files off photocardz.Photo card is write protected. mv not allowed.NT)r   r   r   rW   rT   )r   r   r   r   r   do_mv,  s    
zConsole.do_mvc             C   s   t tj  dS )z.Print name of local current/working directory.N)r   r,   getcwd)r   r   r   r   r   do_lpwd3  s    zConsole.do_lpwdc             C   sF   yt j|j  W n" tk
r4   ttjd Y nX tt j  dS )z'Change current local working directory.zERROR: Directory not found.N)r,   chdirr%   OSErrorr   r   r   re   )r   r   r   r   r   do_lcd7  s
    zConsole.do_lcdc             C   s   t | jj  dS )zOPrint name of photo card current/working directory
        Usage:
        	>pwdN)r   r   r   )r   r   r   r   r   do_pwd?  s    zConsole.do_pwdc             C   s   |j  j }|dkr.| jj dkr| jj  nh|dkr8n^|dkrN| jjd nH| jj|}t|dkrptd n&t|dkrtd n| jj|d  t	j
d| jj  | _d	S )
zChange current working directory on photo card.
        Note:
        	You may only specify one directory level at a time.
        Usage:
        	cd <directory>
        z..r?   r<   r   zDirectory not foundr   z'Pattern matches more than one directoryzpcard: %s > N)r@   r%   r   r   ZcdupZcdZ
match_dirsrN   r   r   r   r   )r   r   Zmatched_dirsr   r   r   do_cdE  s    

zConsole.do_cdc             C   s   | j d dS )zChange to parent directory.z..N)rk   )r   r   r   r   r   do_cdupf  s    zConsole.do_cdupc             C   s   |j  j }|dkr"| jjd n|dkr8| jjd n| jj r| jj }t|j }|j  t	  x |D ]}t	d||| f  qlW t	t
jdtjt|d tjtjd f  t	t
jdtjt|tjtjf  nt	d	 d
S )zDisplay current cache entries, or turn cache on/off.
        Usage:
        	Display: cache
        	Turn on: cache on
        	Turn off: cache off
        ZonTZoffFzsector %d (%d hits)z"Total cache usage: %s (%s maximum)i   zTotal cache sectors: %s of %szCache is off.N)r%   r@   r   Zcache_controlZcache_state
cache_infolistkeyssortr   r   r   r   rE   rN   	photocardZ	MAX_CACHErK   )r   r   rm   tsr   r   r   do_cachen  s    


0*zConsole.do_cachec             C   sj   |j  j }d}yt|}W n tk
r8   td dS X | jj|dkrRtd tt| jj| dS )zIDisplay sector data.
        Usage:
        	sector <sector num>
        Fz$Sector must be specified as a numberNr   zCached sector)	r%   r@   int
ValueErrorr   r   Zcache_checkreprsector)r   r   cachedrx   r   r   r   	do_sector  s    zConsole.do_sectorc             C   s   | j j }t  | j| dS )z"Display photo card directory tree.N)r   treer   
print_tree)r   r   r{   r   r   r   do_tree  s    
zConsole.do_treer   c             C   sV   xP|D ]H}t || t i krtdjd| d |dg | j|| |d  qW d S )Nr>       r?   r   )typer   joinr|   )r   r{   levelrI   r   r   r   r|     s    
zConsole.print_treec             C   s   | j j  dS )zReset the cache.N)r   Zcache_reset)r   r   r   r   r   do_reset  s    zConsole.do_resetc             C   s   t   t d| jjj  t d| jjj  t d| jj   | jj }t d|d   t d|d   t d|d	   t d
|d   t d|d   t d|d   t d|d   t d|d   t d|d   t dtjt	| jj
   dS )zPrint info about photocard.zDevice URI = %sz
Model = %szWorking dir = %szOEM ID = %sr   zBytes/sector = %dr   zSectors/cluster = %dr3   zReserved sectors = %drU   zRoot entries = %dr   zSectors/FAT = %d   zVolume label = %s   zSystem ID = %s   zWrite protected = %dr   zCached sectors = %sN)r   r   r   
device_uriZmodel_uir   r   r   rK   rN   rm   )r   r   r   r   r   r   do_card  s    
zConsole.do_cardc             C   s   |j  j }| jj|}t|dkr| jj|jdd }|dkrvtj \}}| jj	|| t
jd|  t
j| qtd nt|dkrtd ntd d	S )
zMDisplay an image with ImageMagick.
        Usage:
        	display <filename>r   r?   r   imagez
display %szFile is not an image.zFile not found.z5Only one file at a time may be specified for display.N)r%   r@   r   rM   rN   rF   rV   r   make_temp_filecpr,   r-   remover   )r   r   rQ   typfdZ	temp_namer   r   r   
do_display  s    

zConsole.do_displayc             C   s   | j | dS )z Synonym for the display command.N)r   )r   r   r   r   r   do_show  s    zConsole.do_showc             C   s8  |j  j }| jj|}t|dkr| jj|jd\}}|dkoJ|dkr
| jj|}tj	j|\}}tj	j
|\}}	d|krtj \}
}t|dj|d  tjd|  tj| nLd	|kr tj \}
}t|dj|d	  tjd|  tj| ntd
 ntd n t|dkr,td ntd dS )zDisplay an embedded thumbnail image with ImageMagick.
        Note:
        	Only works with JPEG/JFIF images with embedded JPEG/TIFF thumbnails
        Usage:
        	thumbnail <filename>r   r?   r   jpegtiffJPEGThumbnailwbz
display %sTIFFThumbnailzNo thumbnail found.z"Incorrect file type for thumbnail.r   zFile not found.z?Only one file at a time may be specified for thumbnail display.N)r   r   )r%   r@   r   rM   rN   rF   rV   get_exifr,   pathsplitextr   r   openwriter-   r   r   )r   r   rQ   r   subtyp	exif_infoZdir_name	file_nameZ
photo_nameZ	photo_extZtemp_file_fdZtemp_file_namer   r   r   do_thumbnail  s.    



zConsole.do_thumbnailc             C   s   | j | dS )z"Synonym for the thumbnail command.N)r   )r   r   r   r   r   do_thumb  s    zConsole.do_thumbc       	      C   s   |j  j }| jj|}t|dkr| jj|jd\}}|dkr|dkr| jj|}tj	ddddddf}t
  t
tj|jd t|j }|j  x8|D ]&}|dkrt
|j|d||  f qW nt
d nt|dkrt
d nt
d dS )zCDisplay EXIF info for file.
        Usage:
        	exif <filename>r   r?   r   r   r   (   r3   )r4   r5   TagValuer   r   Filenamez%sz"Incorrect file type for thumbnail.r   zFile not found.z?Only one file at a time may be specified for thumbnail display.N)r   r   )r   r   )r   r   r   )r%   r@   r   rM   rN   rF   rV   r   r   rB   r   r   r   rD   rn   ro   rp   )	r   r   rQ   r   r   r   rH   Zeeer   r   r   do_exif  s(    
 

zConsole.do_exifc             C   s   | j | dS )zSynonym for the exif command.N)r   )r   r   r   r   r   do_info7  s    zConsole.do_infoc             C   s   t jtt d S )N)r   Z	log_title	__title____version__)r   r   r   r   r   do_about;  s    zConsole.do_aboutN)F)r   ),__name__
__module____qualname__r   r   r   r    r!   r"   r#   r$   r'   r)   r*   r+   r.   r/   r1   r0   rL   rT   r_   rO   rP   rc   rd   rf   ri   rj   rk   rl   rt   rz   r}   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   >   sR   		 
6!
)(r
   c             C   s>   |dkr"t   t tjd|   nt d| |tj|f  d S )Nr   zCopying %s...z
Copied %s to %s (%s)...)r   r   r   r   rE   )r`   ra   rb   r   r   r   status_callback?  s    r   FTOutput directory:V-o<dir> or --output=<dir> (Defaults to current directory)(Only used for non-GUI modes)optionz
hp-toolbox)Zextra_optionsZsee_also_listozoutput=)rq   -o--outputZqt4zA%s does not support Qt4. Please use Qt3 or run in -i or -n modes.z
pcard-type)filterzUsing device : %s
z%Unable to start photocard session: %szkUnable to mount photo card on device. Check that device is powered on and photo card is correctly inserted.z
Photocard on device %s mountedz4DO NOT REMOVE PHOTO CARD UNTIL YOU EXIT THIS PROGRAMz%ERROR: Output directory %s not found.zAborted.zAn error occured: %szOutput directory is %sr3   )r4   r5   r6   )r4   r5   r7   r9   r:   r;   z%sz%s/%srU   zFound %d files to unload, %s
zUnloading files...
z"
%s unloaded in %d sec (%d KB/sec)i   z	User exit)
unloadformz,Unable to load Qt3 support. Is it installed?zlastWindowClosed()zquit()uilocr-   zUsing system locale: %scutf8r<   z&Trying to load .qm file for %s locale.zhplip_%s.qmzName of .qm file: %szUsing default 'C' localezUsing locale: %sr	   zEUnable to connect to HPLIP I/O. Please (re)start HPLIP and try again.r>   zDone.)r   r   r   F)r   r   )r9   r:   r;   )r   __mod__r   __doc__sysr,   os.pathZgetoptrer   ZtimeZfnmatchstringoperatorreadlineImportErrorZbase.gZbase.sixextr   r   rW   exitbaser   r   r   r   Zprntr	   r   r
   r   ZModuleZGUI_MODEZINTERACTIVE_MODEZNON_INTERACTIVE_MODEZUI_TOOLKIT_QT3modZsetUsageZUSAGE_FLAG_DEVICE_ARGSZparseStdOptsZoptsr   Zprinter_namemodeZ
ui_toolkitr   Zpcardrq   re   Z
output_dirr   aZcanEnterGUIModeZgetDeviceUrieqr   Z	PhotoCardr   Errorr   msgZset_callbackZupdate_spinnerZmountZumountr   r   realpathnormpath
expanduserrg   rh   r   ZconsoleZcmdloopKeyboardInterrupt	ExceptionrX   r[   rN   r\   r]   rY   rB   rC   rH   rD   rR   rE   rZ   rS   r^   Zqtr   r   ZQApplicationargvZappZQObjectZconnectZSIGNALZSLOTZ	user_confgetr@   strZ
QTextCodecZlocaledebugrV   lxr   rv   ZQTranslatorZtransZqm_fileloadZpropZlocalization_dirZloadedZinstallTranslatorZQLocaleZ
setDefault	setlocaleLC_ALLZ	normalizeZ
UnloadFormwZsetMainWidgetZshowZ	exec_loopr   r   r   r   <module>   s>  

	    	












$

6"*






