1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (c) 2012, Peter Levi <peterlevi@peterlevi.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
"""Helpers for an Ubuntu application."""
21
from . varietyconfig import get_data_file
22
from . Builder import Builder
25
from gettext import gettext as _
26
gettext.textdomain('variety')
28
def get_builder(builder_file_name):
29
"""Return a fully-instantiated Gtk.Builder instance from specified ui
32
:param builder_file_name: The name of the builder file, without extension.
33
Assumed to be in the 'ui' directory under the data path.
35
# Look for the ui file that describes the user interface.
36
ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
37
if not os.path.exists(ui_filename):
41
builder.set_translation_domain('variety')
42
builder.add_from_file(ui_filename)
46
# Owais Lone : To get quick access to icons and stuff.
47
def get_media_file(media_file_name):
48
media_filename = get_data_file('media', '%s' % (media_file_name,))
49
if not os.path.exists(media_filename):
52
return "file:///"+media_filename
54
class NullHandler(logging.Handler):
55
def emit(self, record):
58
def set_up_logging(opts):
59
# add a handler to prevent basicConfig
60
root = logging.getLogger()
61
null_handler = NullHandler()
62
root.addHandler(null_handler)
64
formatter = logging.Formatter("%(levelname)s: %(asctime)s: %(funcName)s() '%(message)s'")
66
logger = logging.getLogger('variety')
67
logger_sh = logging.StreamHandler()
68
logger_sh.setFormatter(formatter)
69
logger.addHandler(logger_sh)
72
logger_file = logging.FileHandler(
73
os.path.join(os.path.expanduser("~/.config/variety/"), "variety.log"), "w")
74
logger_file.setFormatter(logging.Formatter("%(levelname)s: %(funcName)s() '%(message)s'"))
75
logger.addHandler(logger_file)
77
logger.exception("Could not create file logger")
80
lib_logger = logging.getLogger('variety_lib')
81
lib_logger_sh = logging.StreamHandler()
82
lib_logger_sh.setFormatter(formatter)
83
lib_logger.addHandler(lib_logger_sh)
85
# Set the logging level to show debug messages.
87
logger.setLevel(logging.INFO)
88
logger.debug('logging enabled')
90
logger.setLevel(logging.DEBUG)
92
lib_logger.setLevel(logging.DEBUG)
94
def get_help_uri(page=None):
95
# help_uri from source tree - default language
96
here = os.path.dirname(__file__)
97
help_uri = os.path.abspath(os.path.join(here, '..', 'help', 'C'))
99
if not os.path.exists(help_uri):
100
# installed so use gnome help tree - user's language
103
# unspecified page is the index.page
105
help_uri = '%s#%s' % (help_uri, page)
109
def show_uri(parent, link):
110
from gi.repository import Gtk # pylint: disable=E0611
111
screen = parent.get_screen()
112
Gtk.show_uri(screen, link, Gtk.get_current_event_time())
114
def alias(alternative_function_name):
115
'''see http://www.drdobbs.com/web-development/184406073#l9'''
116
def decorator(function):
117
'''attach alternative_function_name(s) to function'''
118
if not hasattr(function, 'aliases'):
119
function.aliases = []
120
function.aliases.append(alternative_function_name)