"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.71.1/zim/__init__.py" (23 May 2019, 7022 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 latest Fossies "Diffs" side-by-side code changes report: 0.71.0_vs_0.71.1.

    1 
    2 # Copyright 2008-2018 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    3 
    4 # This program is free software; you can redistribute it and/or modify
    5 # it under the terms of the GNU General Public License as published by
    6 # the Free Software Foundation; either version 2 of the License, or
    7 # (at your option) any later version.
    8 
    9 # This program is distributed in the hope that it will be useful,
   10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 # GNU General Public License for more details.
   13 
   14 '''
   15 This is the development documentation of zim.
   16 
   17 B{NOTE:} There is also some generic development documentation in the
   18 "HACKING" folder in the source distribution. Please also have a look
   19 at that if you want to help with zim development.
   20 
   21 In this API documentation many of the methods with names starting with
   22 C{do_} and C{on_} are not documented. The reason is that these are
   23 signal handlers that are not part of the external API. They act upon
   24 a signal but should never be called directly by other objects.
   25 
   26 
   27 Overview
   28 ========
   29 
   30 The script C{zim.py} is a thin wrapper around the C{main()} function
   31 defined in L{zim.main}. This main function constructs a C{Command}
   32 object that implements a specific commandline command. The C{Command}
   33 object then either connects to a running instance of zim, or executes
   34 the application.
   35 
   36 To execute the application, the command typically constructs a C{Notebook} and
   37 depending on the command the graphical interface is constructed, a webserver is
   38 started or some other action is executed on the notebook.
   39 
   40 The C{Notebook} object is found in L{zim.notebook} and implements the
   41 API for accessing and storing pages, attachments and other data in
   42 the notebook folder.
   43 
   44 The notebook works together with an C{Index} object which keeps a
   45 SQLite database of all the pages to speed up notebook access and allows
   46 to e.g. show a list of pages in the side pane of the user interface.
   47 
   48 Another aspect of the notebook is the parsing of the wiki text in the
   49 pages such that it can be shown in the interface or exported to another
   50 format. See L{zim.formats} for implementations of different parsers.
   51 
   52 All classes related to configuration are located in L{zim.config}.
   53 The C{ConfigManager} handles looking up config files and provides them
   54 for all components.
   55 
   56 Plugins are defined as sub-modules of L{zim.plugins}. The
   57 C{PluginManager} manages the plugins that are loaded and objects that
   58 can be extended by plugins.
   59 
   60 The graphical user interface is implemented in the L{zim.gui} module
   61 and it's sub-modules. The webinterface is implemented in L{zim.www}.
   62 
   63 Functionality for exporting content is implemented in L{zim.exporter}.
   64 And search functionality can be found in L{zim.search}.
   65 
   66 
   67 Many classes in zim have signals which allow other objects to connect
   68 to a listen for specific events. This allows for an event driven chain
   69 of control, which is mainly used in the graphical interface, but is
   70 also used elsewhere. If you are not familiar with event driven programs
   71 please refer to a Gtk manual.
   72 
   73 
   74 Infrastructure classes
   75 ----------------------
   76 
   77 All functions and objects to interact with the file system can be
   78 found in L{zim.fs}.
   79 
   80 For executing external applications see L{zim.applications} or
   81 L{zim.gui.applications}.
   82 
   83 Some generic base classes and functions can be found in L{zim.utils}
   84 
   85 @newfield signal: Signal, Signals
   86 @newfield emits: Emits, Emits
   87 @newfield implementation: Implementation
   88 '''
   89 # New epydoc fields defined above are inteded as follows:
   90 # @signal: signal-name (param1, param2): description
   91 # @emits: signal
   92 # @implementation: must implement / optional for sub-classes
   93 
   94 
   95 # Bunch of meta data, used at least in the about dialog
   96 __version__ = '0.71.1'
   97 __url__ = 'https://www.zim-wiki.org'
   98 __author__ = 'Jaap Karssenberg <jaap.karssenberg@gmail.com>'
   99 __copyright__ = 'Copyright 2008 - 2019 Jaap Karssenberg <jaap.karssenberg@gmail.com>'
  100 __license__ = '''\
  101 This program is free software; you can redistribute it and/or modify
  102 it under the terms of the GNU General Public License as published by
  103 the Free Software Foundation; either version 2 of the License, or
  104 (at your option) any later version.
  105 
  106 This program is distributed in the hope that it will be useful,
  107 but WITHOUT ANY WARRANTY; without even the implied warranty of
  108 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  109 '''
  110 
  111 import os
  112 import sys
  113 import gettext
  114 import logging
  115 import locale
  116 
  117 
  118 logger = logging.getLogger('zim')
  119 
  120 
  121 ########################################################################
  122 
  123 ## Note: all init here must happen before importing any other zim
  124 ##       modules, so can not use zim.fs utilities etc.
  125 ##       therefore ZIM_EXECUTABLE is a string, not an object
  126 
  127 
  128 ## Check executable and relative data dir
  129 ## (sys.argv[0] should always be correct, even for compiled exe)
  130 ZIM_EXECUTABLE = os.path.abspath(sys.argv[0])
  131 
  132 
  133 ## Initialize locale  (needed e.g. for natural_sort)
  134 try:
  135     locale.setlocale(locale.LC_ALL, '')
  136 except locale.Error:
  137     logger.exception('Could not set locale settings')
  138 
  139 
  140 ## Initialize gettext  (maybe make this optional later for module use ?)
  141 
  142 if os.name == "nt" and not os.environ.get('LANG'):
  143     # Set locale config for gettext (other platforms have this by default)
  144     # Using LANG because it is lowest prio - do not override other params
  145     lang, enc = locale.getlocale()
  146     if lang is not None:
  147         os.environ['LANG'] = lang + '.' + enc if enc else lang
  148         logger.info('Locale set to: %s', os.environ['LANG'])
  149 
  150 
  151 _localedir = os.path.join(os.path.dirname(ZIM_EXECUTABLE), 'locale')
  152 
  153 try:
  154     if os.path.isdir(_localedir):
  155         # We are running from a source dir - use the locale data included there
  156         gettext.install('zim', _localedir, names=('_', 'gettext', 'ngettext'))
  157     else:
  158         # Hope the system knows where to find the data
  159         gettext.install('zim', None, names=('_', 'gettext', 'ngettext'))
  160 except:
  161     logger.exception('Error loading translation')
  162     trans = gettext.NullTranslations()
  163     trans.install(names=('_', 'gettext', 'ngettext'))
  164 
  165 
  166 ## Check environment
  167 
  168 if os.name == 'nt':
  169     # Windows specific environment variables
  170     # os.environ does not support setdefault() ...
  171     if not 'USER' in os.environ or not os.environ['USER']:
  172         os.environ['USER'] = os.environ['USERNAME']
  173 
  174     if not 'HOME' in os.environ or not os.environ['HOME']:
  175         if 'USERPROFILE' in os.environ:
  176             os.environ['HOME'] = os.environ['USERPROFILE']
  177         elif 'HOMEDRIVE' in os.environ and 'HOMEPATH' in os.environ:
  178             os.environ['HOME'] = \
  179                 os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
  180 
  181     if not 'APPDATA' in os.environ or not os.environ['APPDATA']:
  182         os.environ['APPDATA'] = os.environ['HOME'] + '\\Application Data'
  183 
  184 if not os.path.isdir(os.environ['HOME']):
  185     logger.error('Environment variable $HOME does not point to an existing folder: %s', os.environ['HOME'])
  186 
  187 if not 'USER' in os.environ or not os.environ['USER']:
  188     os.environ['USER'] = os.path.basename(os.environ['HOME'])
  189     logger.info('Environment variable $USER was not set, set to "%s"', os.environ['USER'])