~daubers/+junk/rdghackauthbot

« back to all changes in this revision

Viewing changes to rdghacksignbot/rdghacksignbot_lib/helpers.py

  • Committer: Matt Daubney
  • Date: 2011-12-29 22:25:47 UTC
  • Revision ID: matt@daubers.co.uk-20111229222547-e10vqkoc0y6pnr5w
* GTK stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# This file is in the public domain
 
4
### END LICENSE
 
5
 
 
6
"""Helpers for an Ubuntu application."""
 
7
import logging
 
8
import os
 
9
 
 
10
from . rdghacksignbotconfig import get_data_file
 
11
from . Builder import Builder
 
12
 
 
13
import gettext
 
14
from gettext import gettext as _
 
15
gettext.textdomain('rdghacksignbot')
 
16
 
 
17
def get_builder(builder_file_name):
 
18
    """Return a fully-instantiated Gtk.Builder instance from specified ui 
 
19
    file
 
20
    
 
21
    :param builder_file_name: The name of the builder file, without extension.
 
22
        Assumed to be in the 'ui' directory under the data path.
 
23
    """
 
24
    # Look for the ui file that describes the user interface.
 
25
    ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
 
26
    if not os.path.exists(ui_filename):
 
27
        ui_filename = None
 
28
 
 
29
    builder = Builder()
 
30
    builder.set_translation_domain('rdghacksignbot')
 
31
    builder.add_from_file(ui_filename)
 
32
    return builder
 
33
 
 
34
 
 
35
# Owais Lone : To get quick access to icons and stuff.
 
36
def get_media_file(media_file_name):
 
37
    media_filename = get_data_file('media', '%s' % (media_file_name,))
 
38
    if not os.path.exists(media_filename):
 
39
        media_filename = None
 
40
 
 
41
    return "file:///"+media_filename
 
42
 
 
43
class NullHandler(logging.Handler):
 
44
    def emit(self, record):
 
45
        pass
 
46
 
 
47
def set_up_logging(opts):
 
48
    # add a handler to prevent basicConfig
 
49
    root = logging.getLogger()
 
50
    null_handler = NullHandler()
 
51
    root.addHandler(null_handler)
 
52
 
 
53
    formatter = logging.Formatter("%(levelname)s:%(name)s: %(funcName)s() '%(message)s'")
 
54
 
 
55
    logger = logging.getLogger('rdghacksignbot')
 
56
    logger_sh = logging.StreamHandler()
 
57
    logger_sh.setFormatter(formatter)
 
58
    logger.addHandler(logger_sh)
 
59
 
 
60
    lib_logger = logging.getLogger('rdghacksignbot_lib')
 
61
    lib_logger_sh = logging.StreamHandler()
 
62
    lib_logger_sh.setFormatter(formatter)
 
63
    lib_logger.addHandler(lib_logger_sh)
 
64
 
 
65
    # Set the logging level to show debug messages.
 
66
    if opts.verbose:
 
67
        logger.setLevel(logging.DEBUG)
 
68
        logger.debug('logging enabled')
 
69
    if opts.verbose > 1:
 
70
        lib_logger.setLevel(logging.DEBUG)
 
71
 
 
72
def get_help_uri(page=None):
 
73
    # help_uri from source tree - default language
 
74
    here = os.path.dirname(__file__)
 
75
    help_uri = os.path.abspath(os.path.join(here, '..', 'help', 'C'))
 
76
 
 
77
    if not os.path.exists(help_uri):
 
78
        # installed so use gnome help tree - user's language
 
79
        help_uri = 'rdghacksignbot'
 
80
 
 
81
    # unspecified page is the index.page
 
82
    if page is not None:
 
83
        help_uri = '%s#%s' % (help_uri, page)
 
84
 
 
85
    return help_uri
 
86
 
 
87
def show_uri(parent, link):
 
88
    from gi.repository import Gtk # pylint: disable=E0611
 
89
    screen = parent.get_screen()
 
90
    Gtk.show_uri(screen, link, Gtk.get_current_event_time())
 
91
 
 
92
def 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