ó
Ò¬Qc           @   s  d  Z  d d l m Z e d d d ƒd d l Z d d l Z y d d l Z d d l Z Wn- d d$ d „  ƒ  YZ d	 d% d
 „  ƒ  YZ n Xd e f d „  ƒ  YZ	 d Z
 d Z d Z d Z d Z d Z d Z d Z d& Z d e f d  „  ƒ  YZ e e d! „ Z d" „  Z e d# k re ƒ  n  d S('   s%   Routines to decode AppleSingle files
iÿÿÿÿ(   t   warnpy3ks*   In 3.x, the applesingle module is removed.t
   stackleveli   Nt   MacOSc           B   s   e  Z d  „  Z e e ƒ Z RS(   c         C   s   t  |  d | ƒ S(   Ns   .rsrc(   t   open(   t   patht   mode(    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   openrf   s    (   t   __name__t
   __module__R   t   classmethod(    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   	t   Carbonc           B   s   e  Z d  d d „  ƒ  YZ RS(   t   Filec           B   sA   e  Z d  d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ RS(	   t   FSSpecc           B   s   e  Z RS(    (   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   t   FSRefc           B   s   e  Z RS(    (   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   t   Aliasc           B   s   e  Z RS(    (   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   (    (    (    (   R   R   R   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   (    (   R   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR
      s   t   Errorc           B   s   e  Z RS(    (   R   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR      s   s   >LL16shi   i  i   s   >llli   i   i   i   i   i   i   i	   i
   i   i   i   i   t   AppleSinglec           B   s,   e  Z d Z d Z e d  „ Z e d „ Z RS(   c         C   s  | j  t ƒ } y" t j t | ƒ \ } } } } Wn# t k
 rV } t d | f ‚ n X| r„ d | f GHd | f GHd | f GHn  | t k r£ t d | f ‚ n  | t k rÂ t d | f ‚ n  | d k rÚ t d ‚ n  g  t	 | ƒ D] }	 | j  t
 ƒ ^ qç }
 g  |  _ xþ |
 D]ö } y t j t | ƒ \ } } } Wn# t k
 r\} t d	 | f ‚ n X| rxd
 | | | f GHn  | j | ƒ | j  | ƒ } t | ƒ | k rÂt d | t | ƒ f ‚ n  |  j j | | f ƒ | t k rð| |  _ q| t k r| |  _ qqWd  S(   Ns   Unpack header error: %ss   Magic:   0x%8.8xs   Version: 0x%8.8xs   Entries: %ds(   Unknown AppleSingle magic number 0x%8.8xs*   Unknown AppleSingle version number 0x%8.8xi    s"   AppleSingle file contains no forkss   Unpack entry error: %ss   Fork %d, offset %d, length %ds$   Short read: expected %d bytes got %d(   t   readt   AS_HEADER_LENGTHt   structt   unpackt   AS_HEADER_FORMATt
   ValueErrorR   t   AS_MAGICt
   AS_VERSIONt   xranget   AS_ENTRY_LENGTHt   forkst   AS_ENTRY_FORMATt   seekt   lent   appendt   AS_DATAFORKt   dataforkt   AS_RESOURCEFORKt   resourcefork(   t   selft   fileobjt   verboset   headert   magict   versiont   igt   nentryt   argt   it   headerst   hdrt   restypet   offsett   lengtht   data(    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   __init__3   sB    "(	c         C   s	  t  | d ƒ } t } | rb |  j d  k r6 t d ‚ n  t  | d ƒ } | j |  j ƒ | j ƒ  n£ |  j d  k rŒ |  j d  k rŒ t d ‚ ny |  j d  k	 rÇ t  | d ƒ } | j |  j ƒ | j ƒ  n  |  j d  k	 rt j	 | d ƒ } | j |  j ƒ | j ƒ  n  d  S(   Nt   wbs   No resource fork founds   No useful forks founds   *wb(
   R   t   FalseR#   t   NoneR   t   writet   closeR!   R   R   (   R$   R   t   resonlyt   outfileR3   t   fp(    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   tofileV   s$    N(   R   R   R7   R!   R#   R6   R4   R=   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyR   /   s   #c         C   sÛ   t  |  d ƒ s² t |  t j j ƒ r7 |  j ƒ  d }  n  t  t j d ƒ r| t |  t j j t j j f ƒ r  |  j ƒ  }  q  n$ t |  t j j ƒ r  |  j ƒ  }  n  t	 |  d ƒ }  n  t
 |  d | ƒ} | j | d | ƒd S(   së  decode(infile, outpath [, resonly=False, verbose=False])

    Creates a decoded file from an AppleSingle encoded file.
    If resonly is True, then it will create a regular file at
    outpath containing only the resource fork from infile.
    Otherwise it will create an AppleDouble file at outpath
    with the data and resource forks from infile.  On platforms
    without the MacOS module, it will create inpath and inpath+'.rsrc'
    with the data and resource forks respectively.

    R   i    R   t   rbR&   R:   N(   t   hasattrt
   isinstanceR
   R   R   t   ResolveAliasR   R   t   as_pathnameR   R   R=   (   t   infilet   outpathR:   R&   t   asfile(    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   decodek   s    !c          C   s£   t  t j ƒ d k  s= t j d d k rR t  t j ƒ d k rR d GHt j d ƒ n  t j d d k rx t }  t j d =n t }  t t j d t j d d |  ƒd  S(   Ni   i   s   -ri   s6   Usage: applesingle.py [-r] applesinglefile decodedfilei   R:   (   R   t   syst   argvt   exitt   TrueR6   RF   (   R:   (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   _test†   s    =t   __main__(    (    (   i   i   i   i   i   i	   i
   i   i   i   i   i   (   t   __doc__t   warningsR    R   RG   R   t   Carbon.FileR
   R   R   R   R   R   R   R   R   R    R"   t	   AS_IGNOREt   objectR   R6   RF   RK   R   (    (    (    sA   /Users/tom/newbuild/install/lib/python2.7/plat-mac/applesingle.pyt   <module>   s2   <	