"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.71.1/zim/config/__init__.py" (21 May 2019, 3372 Bytes) of package /linux/privat/zim-0.71.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "__init__.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 
    2 # Copyright 2009-2013 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    3 
    4 import logging
    5 
    6 logger = logging.getLogger('zim.config')
    7 
    8 
    9 from .basedirs import *
   10 from .dicts import *
   11 from .manager import *
   12 
   13 
   14 '''This module defines all functions and objects related to the
   15 application config.
   16 
   17 The sub module L{zim.config.manager} contains that L{ConfigManager}
   18 object, which is the main object to access configuration files. In
   19 L{zim.config.dicts} a number of classes are defined that represent
   20 configuration files as dictionaries. And classes to define the config
   21 options that are used and how to validate those.
   22 
   23 The file system paths where to search for config files are defined
   24 in L{zim.config.basedirs}.
   25 '''
   26 
   27 
   28 # FIXME - when XDG variables in basedirs.py change, they don't change
   29 # in this module ...
   30 # should they move to their own module ?
   31 
   32 # TODO: Define a ResourceManager for loading resources (icons,
   33 # images, templates, ..)
   34 # Singleton class
   35 # Avoid using basedirs directly elsewhere in the code
   36 
   37 # TODO: resources like icons etc can be managed by a Singleton ResourceManager
   38 
   39 
   40 
   41 def data_dirs(path=None):
   42     '''Generator listing paths that contain zim data files in the order
   43     that they should be searched. These will be the equivalent of
   44     e.g. "~/.local/share/zim", "/usr/share/zim", etc.
   45     @param path: a file path relative to to the data dir, including this
   46     will list sub-folders with this relative path.
   47     @returns: yields L{Dir} objects for the data dirs
   48     '''
   49     zimpath = ['zim']
   50     if path:
   51         if isinstance(path, str):
   52             path = [path]
   53         assert not path[0] == 'zim'
   54         zimpath.extend(path)
   55 
   56     yield XDG_DATA_HOME.subdir(zimpath)
   57 
   58     if ZIM_DATA_DIR:
   59         if path:
   60             yield ZIM_DATA_DIR.subdir(path)
   61         else:
   62             yield ZIM_DATA_DIR
   63 
   64     for dir in XDG_DATA_DIRS:
   65         yield dir.subdir(zimpath)
   66 
   67 
   68 def data_dir(path):
   69     '''Get an data dir sub-folder.  Will look up C{path} relative
   70     to all data dirs and return the first one that exists. Use this
   71     function to find any folders from the "data/" folder in the source
   72     package.
   73     @param path:  a file path relative to to the data dir
   74     @returns: a L{Dir} object or C{None}
   75     '''
   76     for dir in data_dirs(path):
   77         if dir.exists():
   78             return dir
   79     else:
   80         return None
   81 
   82 
   83 def data_file(path):
   84     '''Get a data file. Will look up C{path} relative to all data dirs
   85     and return the first one that exists. Use this function to find
   86     any files from the "data/" folder in the source package.
   87     @param path:  a file path relative to to the data dir (e.g. "zim.png")
   88     @returns: a L{File} object or C{None}
   89     '''
   90     for dir in data_dirs():
   91         file = dir.file(path)
   92         if file.exists():
   93             return file
   94     else:
   95         return None
   96 
   97 
   98 def user_dirs():
   99     '''Get the XDG user dirs.
  100     @returns: a dict with directories for the XDG user dirs. These are
  101     typically defined in "~/.config/user-dirs.dirs". Common user dirs
  102     are: "XDG_DESKTOP_DIR", "XDG_DOWNLOAD_DIR", etc. If no definition
  103     is found an empty dict will be returned.
  104     '''
  105     dirs = {}
  106     file = XDG_CONFIG_HOME.file('user-dirs.dirs')
  107     try:
  108         for line in file.readlines():
  109             line = line.strip()
  110             if line.isspace() or line.startswith('#'):
  111                 continue
  112             else:
  113                 try:
  114                     assert '=' in line
  115                     key, value = line.split('=', 1)
  116                     value = os.path.expandvars(value.strip('"'))
  117                     dirs[key] = Dir(value)
  118                 except:
  119                     logger.exception('Exception while parsing %s', file)
  120     except FileNotFoundError:
  121         pass
  122     return dirs