~ubuntu-branches/debian/sid/guake/sid

« back to all changes in this revision

Viewing changes to src/guake/common.py

  • Committer: Package Import Robot
  • Author(s): Daniel Echeverry
  • Date: 2015-04-26 19:15:06 UTC
  • mfrom: (1.1.7)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20150426191506-mo8037vk6pueer5b
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8; -*-
 
2
"""
 
3
Copyright (C) 2007-2013 Guake authors
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License as
 
7
published by the Free Software Foundation; either version 2 of the
 
8
License, or (at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public
 
16
License along with this program; if not, write to the
 
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
Boston, MA 02110-1301 USA
 
19
"""
 
20
from __future__ import absolute_import
 
21
 
 
22
import gconf
 
23
import gettext
 
24
import gtk
 
25
import guake.globals
 
26
import os
 
27
import sys
 
28
 
 
29
# Internationalization purposes.
 
30
_ = gettext.gettext
 
31
 
 
32
__all__ = ['_', 'ShowableError', 'test_gconf',
 
33
           'pixmapfile', 'gladefile', 'hexify_color',
 
34
           'get_binaries_from_path']
 
35
 
 
36
 
 
37
class ShowableError(Exception):
 
38
 
 
39
    def __init__(self, title, msg, exit_code=1):
 
40
        d = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
 
41
                              buttons=gtk.BUTTONS_CLOSE)
 
42
        d.set_markup('<b><big>%s</big></b>' % title)
 
43
        d.format_secondary_markup(msg)
 
44
        d.run()
 
45
        d.destroy()
 
46
        if exit_code != -1:
 
47
            sys.exit(exit_code)
 
48
 
 
49
 
 
50
def test_gconf():
 
51
    c = gconf.client_get_default()
 
52
    return c.dir_exists('/apps/guake')
 
53
 
 
54
 
 
55
def pixmapfile(x):
 
56
    f = os.path.join(guake.globals.IMAGE_DIR, x)
 
57
    if not os.path.exists(f):
 
58
        raise IOError('No such file or directory: %s' % f)
 
59
    return os.path.abspath(f)
 
60
 
 
61
 
 
62
def gladefile(x):
 
63
    f = os.path.join(guake.globals.GLADE_DIR, x)
 
64
    if not os.path.exists(f):
 
65
        raise IOError('No such file or directory: %s' % f)
 
66
    return os.path.abspath(f)
 
67
 
 
68
 
 
69
def hexify_color(c):
 
70
    h = lambda x: hex(x).replace('0x', '').zfill(4)
 
71
    return '#%s%s%s' % (h(c.red), h(c.green), h(c.blue))
 
72
 
 
73
 
 
74
def get_binaries_from_path(compiled_re):
 
75
    ret = []
 
76
    for i in os.environ.get('PATH', '').split(os.pathsep):
 
77
        if os.path.isdir(i):
 
78
            for j in os.listdir(i):
 
79
                if compiled_re.match(j):
 
80
                    ret.append(os.path.join(i, j))
 
81
    return ret
 
82
 
 
83
 
 
84
def shell_quote(text):
 
85
    """ quote text (filename) for inserting into a shell """
 
86
    return r"\'".join("'%s'" % p for p in text.split("'"))
 
87
 
 
88
 
 
89
def clamp(value, lower, upper):
 
90
    return max(min(value, upper), lower)