__init__.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 23 Jun 2008 13:42:22 +0200
changeset 6 6aad8b499e86
parent 2 9f6d1f30aad8
permissions -rw-r--r--
fix reST markup

# Copyright (c) 2004 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""A file system objects browser"""

__revision__ = '$Id: __init__.py,v 1.17 2004-11-10 11:46:41 adim Exp $'

import time
import gtk, gtk.glade

from logilab.common import logservice
from logilab.common.logger import INDICATORS, AbstractLogger
from os.path import join, dirname, normpath, exists

__metatype__ = type

COLORS = ['red', 'red', 'red', 'red', 'orange', 'blue', 'black', 'green']
class OOBLogger(AbstractLogger):
    """a logilab.common.logger compatible logger"""
    def __init__(self, window):
        AbstractLogger.__init__(self)
        self.win = window
        logservice.init_log(self.win.cfg['log-threshold'], logger = self)

    def log(self, priority, message, substs=None):
        """overridden from AbstractLogger to redirect message to the log
        service window
        """
        if priority <= self.win.cfg['log-threshold'] :
            if substs is not None:
                message = message % substs
        self.win.append_line(
            (time.asctime(), INDICATORS[priority], message, COLORS[priority]),
            scroll_to_line=True)


import sys
## FIXME: sys.prefix is not the right variable to use
## It declares where Python is installed, not where the
## program has been installed.
## We should probably use __LOCAL_OOBBASE and guess from this
if sys.prefix == '/usr':
    __etc_prefix = "/etc"
else:
    __etc_prefix = join(sys.prefix, 'etc')
    
__LOCAL_OOBBASE = dirname(__file__)
__LOCAL_PATHS = {'i18n': join(__LOCAL_OOBBASE, 'i18n'),
                 'pixmaps' : join(__LOCAL_OOBBASE, 'pixmaps'),
                 'glade': join(__LOCAL_OOBBASE,
                               'oobrowser.glade'),
                 'config': normpath(join(__LOCAL_OOBBASE, '..', 'config')),
                 }

__SYSTEM_PATHS = {'i18n': join(sys.prefix, 'share', 'locale'),
                  'pixmaps' : join(sys.prefix, 'share', 'oobrother', 'pixmaps'),
                  'glade': join(sys.prefix, 'share', 'oobrother',
                                'oobrowser.glade'),
                  'config': join(__etc_prefix, 'oobrother'),
                 }

__PREFIX_DIR = (__LOCAL_OOBBASE, ) + ('..', ) * 4
__HOME_DIR = (__LOCAL_OOBBASE, ) + ('..', ) * 3
__SHARE_PATH = ('share', 'oobrother')


# --prefix install scheme
__PREFIX_PATHS = {
    'i18n'    : normpath(join(*(__PREFIX_DIR + ('share', 'locale', )))),
    'pixmaps' : normpath(join(*(__PREFIX_DIR + __SHARE_PATH + ('pixmaps', )))),
    'glade'   : normpath(join(*(__PREFIX_DIR + __SHARE_PATH + ('oobrowser.glade', )))),
    # User should have changed the absolute '/etc/helicsation' path in
    # a relative one in the setup.py script
    'config'  : normpath(join(*(__PREFIX_DIR + ('etc', 'oobrother')))),
    }

# --home install scheme
__HOME_PATHS = {
    'i18n'    : normpath(join(*(__HOME_DIR + ('share', 'locale', )))),
    'pixmaps' : normpath(join(*(__HOME_DIR + __SHARE_PATH + ('pixmaps', )))),
    'glade'   : normpath(join(*(__HOME_DIR + __SHARE_PATH + ('oobrowser.glade',)))),
    # User should have changed the absolute '/etc/helicsation' path in
    # a relative one in the setup.py script
    'config'  : normpath(join(*(__HOME_DIR + ('etc', 'oobrother')))),
    }

del __etc_prefix, __PREFIX_DIR, __HOME_DIR, __SHARE_PATH


def get_path(key):
    """pass a key and get the element from the local path or system path
    """
    lookup_chain = __LOCAL_PATHS, __PREFIX_PATHS, __HOME_PATHS, __SYSTEM_PATHS
    tested_path = []
    for path_scheme in lookup_chain:
        filepath = path_scheme[key]
        if exists(filepath):
            return filepath
        tested_path.append(filepath)
    raise RuntimeError('Unable to find %s file. Tested paths : \n%s.'
                       'This software is probably not installed correctly' %
                       (key, '\n'.join(tested_path)))


## def __load_config():
##     """loads the oob_config module
##     Returns the module object"""
##     import imp
##     file, pathname, desc = imp.find_module('oob_config',
##                                            [get_path('config')])
##     try:
##         oob_config = imp.load_module('oob_config', file, pathname, desc)
##     finally:
##         file.close()
##     return oob_config

## oob_config = __load_config()
## del __load_config
# OOBROTHER_BASE = dirname(__file__)


def localize_app():
    """use gettext to localize application (glade + python strings)"""
    import gettext
    i18n_dir = get_path('i18n')
    gettext.bindtextdomain('oobrother', i18n_dir)
    gettext.textdomain('oobrother')
    gettext.install('oobrother', i18n_dir, unicode = 1)
    gtk.glade.bindtextdomain('oobrother', i18n_dir)
    gtk.glade.textdomain('oobrother')

localize_app()