1#!/usr/bin/python3
2# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3#   MenuLibre - Advanced fd.o Compliant Menu Editor
4#   Copyright (C) 2012-2021 Sean Davis <sean@bluesabre.org>
5#
6#   This program is free software: you can redistribute it and/or modify it
7#   under the terms of the GNU General Public License version 3, as published
8#   by the Free Software Foundation.
9#
10#   This program is distributed in the hope that it will be useful, but
11#   WITHOUT ANY WARRANTY; without even the implied warranties of
12#   MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13#   PURPOSE.  See the GNU General Public License for more details.
14#
15#   You should have received a copy of the GNU General Public License along
16#   with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18import logging
19import os
20
21import gi
22gi.require_version("Gtk", "3.0")
23from gi.repository import Gtk
24
25from . menulibreconfig import get_data_file
26
27
28def get_builder(builder_file_name):
29    """Return a fully-instantiated Gtk.Builder instance from specified ui
30    file
31
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.
34    """
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):
38        ui_filename = None
39
40    builder = Gtk.Builder()
41    builder.add_from_file(ui_filename)
42    builder.set_translation_domain('menulibre')
43    builder.add_from_file(ui_filename)
44    return builder
45
46
47# lint:disable
48class NullHandler(logging.Handler):
49    def emit(self, record):
50        pass
51# lint:enable
52
53
54def set_up_logging(opts):
55    """Set up logging for menulibre"""
56    # add a handler to prevent basicConfig
57    root = logging.getLogger()
58    null_handler = NullHandler()
59    root.addHandler(null_handler)
60
61    formatter = logging.Formatter("%(levelname)s:%(name)s: "
62                                  "%(funcName)s() '%(message)s'")
63
64    logger = logging.getLogger('menulibre')
65    logger_sh = logging.StreamHandler()
66    logger_sh.setFormatter(formatter)
67    logger.addHandler(logger_sh)
68
69    lib_logger = logging.getLogger('menulibre_lib')
70    lib_logger_sh = logging.StreamHandler()
71    lib_logger_sh.setFormatter(formatter)
72    lib_logger.addHandler(lib_logger_sh)
73
74    # Set the logging level to show debug messages.
75    try:
76        if opts.verbose:
77            logger.setLevel(logging.DEBUG)
78            logger.debug('logging enabled')
79        if opts.verbose > 1:
80            lib_logger.setLevel(logging.DEBUG)
81    except TypeError:
82        pass
83
84
85def show_uri(parent, link):
86    """Open a web browser to the specified link."""
87    from gi.repository import Gtk  # pylint: disable=E0611
88    screen = parent.get_screen()
89    Gtk.show_uri(screen, link, Gtk.get_current_event_time())
90
91
92def alias(alternative_function_name):
93    '''see http://www.drdobbs.com/web-development/184406073#l9'''
94    def decorator(function):
95        '''attach alternative_function_name(s) to function'''
96        if not hasattr(function, 'aliases'):
97            function.aliases = []
98        function.aliases.append(alternative_function_name)
99        return function
100    return decorator
101