ó
Ø\c           @   sª   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ	 d
 e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d S(   s  Files and folders in a project are represented as resource objects.

Files and folders are access through `Resource` objects. `Resource` has
two subclasses: `File` and `Folder`. What we care about is that
refactorings and `rope.base.change.Change`s use resources.

There are two options to create a `Resource` for a path in a project.
Note that in these examples `path` is the path to a file or folder
relative to the project's root. A project's root folder is represented
by an empty string.

  1) Use the `rope.base.Project.get_resource()` method. E.g.:

       myresource = myproject.get_resource(path)


  2) Use the `rope.base.libutils` module. `libutils` has a function
     named `path_to_resource()`. It takes a project and a path:

       from rope.base import libutils

       myresource = libutils.path_to_resource(myproject, path)

Once we have a `Resource`, we can retrieve information from it, like
getting the path relative to the project's root (via `path`), reading
from and writing to the resource, moving the resource, etc.
iÿÿÿÿN(   t   change(   t
   exceptions(   t
   fscommandst   Resourcec           B   s¤   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e	 d „  ƒ Z
 e	 d „  ƒ Z e	 d	 „  ƒ Z e	 d
 „  ƒ Z d „  Z d „  Z d „  Z d „  Z RS(   s)   Represents files and folders in a projectc         C   s   | |  _  | |  _ d  S(   N(   t   projectt   _path(   t   selfR   t   path(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   __init__(   s    	c         C   s-   |  j  t j |  | ƒ d |  j | f ƒ d S(   s   Move resource to `new_location`s   Moving <%s> to <%s>N(   t   _perform_changeR    t   MoveResourceR   (   R   t   new_location(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   move,   s    c         C   s$   |  j  t j |  ƒ d |  j ƒ d S(   s    Remove resource from the projects   Removing <%s>N(   R	   R    t   RemoveResourceR   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   remove1   s    c         C   s   d S(   s'   Return true if the resource is a folderN(    (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt	   is_folder6   t    c         C   s   d S(   s   Create this resourceN(    (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   create9   R   c         C   s   t  j j |  j ƒ S(   N(   t   osR   t   existst	   real_path(   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   <   s    c         C   s2   d j  |  j j d ƒ d d !ƒ } |  j j | ƒ S(   Nt   /i    iÿÿÿÿ(   t   joinR   t   splitR   t
   get_folder(   R   t   parent(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   ?   s    "c         C   s   |  j  S(   s²   Return the path of this resource relative to the project root

        The path is the list of parent directories separated by '/' followed
        by the resource name.
        (   R   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   D   s    c         C   s   |  j  j d ƒ d S(   s    Return the name of this resourceR   iÿÿÿÿ(   R   R   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   nameM   s    c         C   s   |  j  j |  j ƒ S(   s,   Return the file system path of this resource(   R   t   _get_resource_pathR   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   R   s    c         C   s"   |  j  | j  k o! |  j | j k S(   N(   t	   __class__R   (   R   t   obj(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   __eq__W   s    c         C   s   |  j  | ƒ S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   __ne__Z   s    c         C   s   t  |  j ƒ S(   N(   t   hashR   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   __hash__]   s    c         C   s0   t  j | ƒ } | j | ƒ |  j j | ƒ d  S(   N(   R    t	   ChangeSett
   add_changeR   t   do(   R   t   change_t   descriptiont   changes(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR	   `   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   t   propertyR   R   R   R   R   R   R!   R	   (    (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   %   s   										t   Filec           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Represents a filec         C   s   t  t |  ƒ j | | ƒ d  S(   N(   t   superR,   R   (   R   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   i   s    c         C   sO   |  j  ƒ  } y t j | ƒ SWn+ t k
 rJ } t j |  j | j ƒ ‚ n Xd  S(   N(   t
   read_bytesR   t   file_data_to_unicodet   UnicodeDecodeErrorR   t   ModuleDecodeErrorR   t   reason(   R   t   datat   e(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   readl   s
    c         C   s2   t  |  j d ƒ } z | j ƒ  SWd  | j ƒ  Xd  S(   Nt   rb(   t   openR   R5   t   close(   R   t   handle(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR.   s   s    c         C   sU   y | |  j  ƒ  k r d  SWn t k
 r- n X|  j t j |  | ƒ d |  j ƒ d  S(   Ns   Writing file <%s>(   R5   t   IOErrorR	   R    t   ChangeContentsR   (   R   t   contents(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   writez   s    c         C   s   t  S(   N(   t   False(   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   ƒ   s    c         C   s   |  j  j |  j ƒ d  S(   N(   R   t   create_fileR   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   †   s    (	   R(   R)   R*   R   R5   R.   R=   R   R   (    (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR,   f   s   						t   Folderc           B   sz   e  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 RS(   s   Represents a folderc         C   s   t  t |  ƒ j | | ƒ d  S(   N(   R-   R@   R   (   R   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR      s    c         C   s   t  S(   N(   t   True(   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR      s    c         C   sž   y t  j |  j ƒ } Wn t k
 r* g  SXg  } xf | D]^ } y |  j | ƒ } Wn t j k
 rj q8 n X|  j j | ƒ s8 | j	 |  j | ƒ ƒ q8 q8 W| S(   s"   Return the children of this folder(
   R   t   listdirR   t   OSErrort	   get_childR   t   ResourceNotFoundErrorR   t
   is_ignoredt   append(   R   t   childrent   resultR   t   child(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   get_children“   s    c         C   s6   |  j  t j |  | ƒ d |  j | ƒ ƒ |  j | ƒ S(   Ns   Creating file <%s>(   R	   R    t
   CreateFilet   _get_child_pathRD   (   R   t	   file_name(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR?   £   s    c         C   s6   |  j  t j |  | ƒ d |  j | ƒ ƒ |  j | ƒ S(   Ns   Creating folder <%s>(   R	   R    t   CreateFolderRM   RD   (   R   t   folder_name(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   create_folder©   s    c         C   s    |  j  r |  j  d | S| Sd  S(   NR   (   R   (   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyRM   ¯   s    	c         C   s   |  j  j |  j | ƒ ƒ S(   N(   R   t   get_resourceRM   (   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyRD   µ   s    c         C   s1   y |  j  | ƒ t SWn t j k
 r, t SXd  S(   N(   RD   RA   R   RE   R>   (   R   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt	   has_child¸   s
    c         C   s)   g  |  j  ƒ  D] } | j ƒ  s | ^ q S(   N(   RK   R   (   R   t   resource(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt	   get_files¿   s    c         C   s)   g  |  j  ƒ  D] } | j ƒ  r | ^ q S(   N(   RK   R   (   R   RT   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   get_foldersÃ   s    c         C   s6   |  | k r t  S|  j d k p5 | j j |  j d ƒ S(   NR   R   (   R>   R   t
   startswith(   R   RT   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   containsÇ   s    c         C   s   |  j  j |  j ƒ d  S(   N(   R   RQ   R   (   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   Ì   s    (   R(   R)   R*   R   R   RK   R?   RQ   RM   RD   RS   RU   RV   RX   R   (    (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR@   Š   s   											t   _ResourceMatcherc           B   s;   e  Z d  „  Z d „  Z d „  Z d „  Z e d „  ƒ Z RS(   c         C   s   g  |  _  g  |  _ d  S(   N(   t   patternst   _compiled_patterns(   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyR   Ò   s    	c         C   s   d |  _ | |  _ d S(   s¡   Specify which resources to match

        `patterns` is a `list` of `str`\s that can contain ``*`` and
        ``?`` signs for matching resource names.

        N(   t   NoneR[   RZ   (   R   RZ   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   set_patternsÖ   s    	c         C   sa   | j  d d ƒ j  d d ƒ j  d d ƒ j  d d ƒ } d	 | d
 } |  j j t j | ƒ ƒ d  S(   Nt   .s   \.t   *s   [^/]*t   ?s   [^/]s   //s   /(.*/)?s   ^(.*/)?s   (/.*)?$(   t   replacet   compiled_patternsRG   t   ret   compile(   R   t   patternt
   re_pattern(    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   _add_patternà   s
    c         C   sk   x' |  j  D] } | j | j ƒ r
 t Sq
 Wt j j | j j | j j d ƒ Œ } t j j	 | ƒ rg t St
 S(   NR   (   Rb   t   matchR   RA   R   R   R   t   addressR   t   islinkR>   (   R   RT   Re   R   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt
   does_matchç   s    c         C   sC   |  j  d  k r< g  |  _  x! |  j D] } |  j | ƒ q" Wn  |  j  S(   N(   R[   R\   RZ   Rg   (   R   Re   (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyRb   ñ   s
    	(   R(   R)   R   R]   Rg   Rk   R+   Rb   (    (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyRY   Ð   s
   		
		
(   R*   R   Rc   t	   rope.baseR    R   R   t   objectR   R,   R@   RY   (    (    (    s2   lib/python2.7/site-packages/rope/base/resources.pyt   <module>   s   A$F