"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.70/zim/notebook/__init__.py" (14 Mar 2019, 4630 Bytes) of package /linux/privat/zim-0.70.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 latest Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 
    2 # Copyright 2008-2015 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    3 
    4 '''
    5 This module contains the main Notebook class and related classes.
    6 
    7 The C{Notebook} interface is the generic API for accessing and storing
    8 pages and other data in the notebook. The interface uses L{Path} objects
    9 to indicate a specific page. See L{Notebook.pages.lookup_from_user_input()}
   10 to obtain a L{Path} from a page name as string. Pages in the notebook
   11 are represented by the L{Page} object, which allows to access the page
   12 contents.
   13 
   14 The notebook keeps track of all pages using an C{Index} which is stored
   15 in a C{sqlite} database. Methods that need a list of pages in the
   16 notebook always use the index rather than a direct lookup. See
   17 L{zim.notebook.index} for more details.
   18 
   19 The C{NotebookInfoList} is defined to help access known notebooks and
   20 a C{NotebookInfo} object can be used to access the notebook properties
   21 without instantiating a full L{Notebook} object. Use the convience
   22 methods L{get_notebook_list()} and L{resolve_notebook()} to obtain these
   23 objects.
   24 
   25 @note: To open a notebook based on e.g. a commandline option it is
   26 almost always better to use L{build_notebook()} rather than istantiating
   27 the notebook directly.
   28 
   29 @note: for more information about threading and concurency,
   30 see L{zim.notebook.operations}
   31 
   32 '''
   33 
   34 from zim.fs import FilePath, File, Dir, FileNotFoundError
   35 from zim.parsing import url_decode
   36 
   37 
   38 from .info import NotebookInfo, NotebookInfoList, \
   39     resolve_notebook, get_notebook_list, get_notebook_info, interwiki_link
   40 
   41 from .operations import NotebookOperation, SimpleAsyncOperation, \
   42     NotebookOperationOngoing, NotebookState
   43 
   44 from .notebook import Notebook, NotebookExtension, TrashNotSupportedError, \
   45     PageNotFoundError, PageNotAllowedError, PageExistsError, PageReadOnlyError
   46 
   47 from .page import Path, Page, \
   48     HRef, HREF_REL_ABSOLUTE, HREF_REL_FLOATING, HREF_REL_RELATIVE
   49 
   50 from .layout import encode_filename, decode_filename
   51 
   52 from .index import IndexNotFoundError, \
   53     LINK_DIR_BACKWARD, LINK_DIR_BOTH, LINK_DIR_FORWARD
   54 
   55 
   56 
   57 def build_notebook(location):
   58     '''Create a L{Notebook} object for a file location
   59     Tries to automount file locations first if needed
   60     @param location: a L{FilePath} or a L{NotebookInfo}
   61     @returns: a L{Notebook} object and a L{Path} object or C{None}
   62     @raises FileNotFoundError: if file location does not exist and could not be mounted
   63     '''
   64     uri = location.uri
   65     page = None
   66 
   67     # Decipher zim+file:// uris
   68     if uri.startswith('zim+file://'):
   69         uri = uri[4:]
   70         if '?' in uri:
   71             uri, page = uri.split('?', 1)
   72             page = url_decode(page)
   73             page = Path(page)
   74 
   75     # Automount if needed
   76     filepath = FilePath(uri)
   77     if not filepath.exists():
   78         mount_notebook(filepath)
   79         if not filepath.exists():
   80             raise FileNotFoundError(filepath)
   81 
   82     # Figure out the notebook dir
   83     if filepath.isdir():
   84         dir = Dir(uri)
   85         file = None
   86     else:
   87         file = File(uri)
   88         dir = file.dir
   89 
   90     if file and file.basename == 'notebook.zim':
   91         file = None
   92     else:
   93         parents = list(dir)
   94         parents.reverse()
   95         for parent in parents:
   96             if parent.file('notebook.zim').exists():
   97                 dir = parent
   98                 break
   99 
  100     # Resolve the page for a file
  101     if file:
  102         path = file.relpath(dir)
  103         if '.' in path:
  104             path, _ = path.rsplit('.', 1) # remove extension
  105         path = path.replace('/', ':')
  106         page = Path(path)
  107 
  108     # And finally create the notebook
  109     notebook = Notebook.new_from_dir(dir)
  110     return notebook, page
  111 
  112 
  113 def mount_notebook(filepath):
  114     from zim.config import ConfigManager, String
  115 
  116     configdict = ConfigManager.get_config_dict('automount.conf')
  117 
  118     groups = sorted([k for k in list(configdict.keys()) if k.startswith('Path')])
  119     for group in groups:
  120         path = group[4:].strip() # len('Path') = 4
  121         dir = Dir(path)
  122         if filepath.path == dir.path or filepath.ischild(dir):
  123             configdict[group].define(mount=String(None))
  124             handler = ApplicationMountPointHandler(dir, **configdict[group])
  125             if handler(filepath):
  126                 break
  127 
  128 
  129 class ApplicationMountPointHandler(object):
  130     # TODO add password prompt logic, provide to cmd as argument, stdin
  131 
  132     def __init__(self, dir, mount, **a):
  133         self.dir = dir
  134         self.mount = mount
  135 
  136     def __call__(self, path):
  137         if path.path == self.dir.path or path.ischild(self.dir) \
  138         and not self.dir.exists() \
  139         and self.mount:
  140             from zim.applications import Application
  141             Application(self.mount).run()
  142             return path.exists()
  143         else:
  144             return False
  145 
  146 
  147 def init_notebook(dir, name=None):
  148     '''Initialize a new notebook in a directory'''
  149     assert isinstance(dir, Dir)
  150     from .notebook import NotebookConfig
  151     dir.touch()
  152     config = NotebookConfig(dir.file('notebook.zim'))
  153     config['Notebook']['name'] = name or dir.basename
  154     config.write()