
    ucu                     t   d Z ddlZddlZej                  j	                  d      Zej                  j                  d      xs ej                  j                  edd      Z	e	gej                  j                  d      xs dj                  d	      z   Zej                  j                  d
      xs ej                  j                  ed      Zegej                  j                  d      xs dj                  d	      z   Zej                  j                  d      xs ej                  j                  ed      Zej                  j                  d      xs ej                  j                  edd      ZeD  cg c]  } | s| 	 c} ZeD  cg c]  } | s| 	 c} Zd Zd Zd Zd Zd Zd Zd ZddZyc c} w c c} w )a  
This module is based on a rox module (LGPL):

http://cvs.sourceforge.net/viewcvs.py/rox/ROX-Lib2/python/rox/basedir.py?rev=1.9&view=log

The freedesktop.org Base Directory specification provides a way for
applications to locate shared data and configuration:

    http://standards.freedesktop.org/basedir-spec/

(based on version 0.6)

This module can be used to load and save from and to these directories.

Typical usage:

    from rox import basedir
    
    for dir in basedir.load_config_paths('mydomain.org', 'MyProg', 'Options'):
        print "Load settings from", dir

    dir = basedir.save_config_path('mydomain.org', 'MyProg')
    print >>file(os.path.join(dir, 'Options'), 'w'), "foo=2"

Note: see the rox.Options module for a higher-level API for managing options.
    N~XDG_DATA_HOMEz.localshareXDG_DATA_DIRSz/usr/local/share:/usr/share:XDG_CONFIG_HOMEz.configXDG_CONFIG_DIRSz/etc/xdgXDG_CACHE_HOMEz.cacheXDG_STATE_HOMEstatec                     t        j                  j                  |  } | j                  d      rJ t         j                  j                  t        |       }t         j                  j                  |      st        j                  |d       |S )zEnsure ``$XDG_CONFIG_HOME/<resource>/`` exists, and return its path.
    'resource' should normally be the name of your application. Use this
    when saving configuration settings.
    /  )ospathjoin
startswithxdg_config_homeisdirmakedirsresourcer   s     3/usr/lib/python3/dist-packages/xdg/BaseDirectory.pysave_config_pathr   4   sa    
 ww||X&H""3'''77<<2D77==
D% K    c                     t        j                  j                  |  } | j                  d      rJ t         j                  j                  t        |       }t         j                  j                  |      st        j                  |       |S )zEnsure ``$XDG_DATA_HOME/<resource>/`` exists, and return its path.
    'resource' should normally be the name of your application or a shared
    resource. Use this when saving or updating application data.
    r   )r   r   r   r   xdg_data_homer   r   r   s     r   save_data_pathr   @   s_    
 ww||X&H""3'''77<<x0D77==
DKr   c                     t        j                  j                  |  } | j                  d      rJ t         j                  j                  t        |       }t         j                  j                  |      st        j                  |       |S )zEnsure ``$XDG_CACHE_HOME/<resource>/`` exists, and return its path.
    'resource' should normally be the name of your application or a shared
    resource.r   )r   r   r   r   xdg_cache_homer   r   r   s     r   save_cache_pathr!   L   _     ww||X&H""3'''77<<1D77==
DKr   c                     t        j                  j                  |  } | j                  d      rJ t         j                  j                  t        |       }t         j                  j                  |      st        j                  |       |S )zEnsure ``$XDG_STATE_HOME/<resource>/`` exists, and return its path.
    'resource' should normally be the name of your application or a shared
    resource.r   )r   r   r   r   xdg_state_homer   r   r   s     r   save_state_pathr%   W   r"   r   c               '      K   t        j                  j                  |  } t        D ]F  }t         j                  j                  ||       }t         j                  j	                  |      sC| H yw)zReturns an iterator which gives each directory named 'resource' in the
    configuration search path. Information provided by earlier directories should
    take precedence over later ones, and the user-specific config dir comes
    first.N)r   r   r   xdg_config_dirsexists)r   
config_dirr   s      r   load_config_pathsr*   b   sQ     
 ww||X&H% ,
ww||J177>>$t,   A%A/(A/c                  $    t        |  D ]  }|c S  y)zYReturns the first result from load_config_paths, or None if there is nothing
    to load.N)r*   )r   xs     r   load_first_configr.   l   s      ) r   c               '      K   t        j                  j                  |  } t        D ]F  }t         j                  j                  ||       }t         j                  j	                  |      sC| H yw)zReturns an iterator which gives each directory named 'resource' in the
    application data search path. Information provided by earlier directories
    should take precedence over later ones.N)r   r   r   xdg_data_dirsr(   )r   data_dirr   s      r   load_data_pathsr2   s   sQ      ww||X&H! ,ww||Hh/77>>$t,r+   c                    	 t         j                  d   S # t        $ r$ | r ddl}d|j	                         z   }d}	 t        j
                  |      }t        j                  |j                        st        j                  |       d}n|j                  t        j                         k7  s.|j                  t        j                  t        j                  z  z  rOt        j                  |       d}n7# t        $ r+}ddl}|j                   |j"                  k(  rd}n Y d}~nd}~ww xY w|rt        j$                  |d       |cY S w xY w)a   Returns the value of $XDG_RUNTIME_DIR, a directory path.
    
    This directory is intended for 'user-specific non-essential runtime files
    and other file objects (such as sockets, named pipes, ...)', and
    'communication and synchronization purposes'.
    
    As of late 2012, only quite new systems set $XDG_RUNTIME_DIR. If it is not
    set, with ``strict=True`` (the default), a KeyError is raised. With 
    ``strict=False``, PyXDG will create a fallback under /tmp for the current
    user. This fallback does *not* provide the same guarantees as the
    specification requires for the runtime directory.
    
    The strict default is deliberately conservative, so that application
    developers can make a conscious decision to allow the fallback.
    XDG_RUNTIME_DIRr   Nz /tmp/pyxdg-runtime-dir-fallback-FTr   )r   environKeyErrorgetpassgetuserlstatstatS_ISDIRst_modeunlinkst_uidgetuidS_IRWXGS_IRWXOrmdirOSErrorerrnoENOENTmkdir)strictr7   fallbackcreatesterD   s          r   get_runtime_dirrL   |   s    "zz+,,  58II	 (#B <<

+		(#))ryy{***t|| ;<"  	ww%,,& 	" HHXu%A s9    &EC/BE/	D#8!DED##EE)T)__doc__r   r:   r   
expanduser_homer5   getr   r   splitr0   r   r'   r    r$   r   r   r!   r%   r*   r.   r2   rL   )r-   s   0r   <module>rR      s  6 
3

/ 3GGLL'2  ZZ^^O$E(ELLSQR **..!23 +GGLL	*  ##ZZ^^%&4*;;C@A  01 *GGLL)   01 3GGLL'2  */qQ/-313

		,,2W 03s   4F0<F0F5F5