Module peggy
[hide private]
[frames] | no frames]

Source Code for Module peggy

 1  #-*- coding: utf-8 -*- 
 2  ## 
 3  ## 
 4  ##    peggy - tools for working with PyGtk and setuptools 
 5  ##                    <http://www.florian-diesch.de/software/peggy/> 
 6  ##    Copyright (C) 2009  Florian Diesch <devel@florian-diesch.de> 
 7  ## 
 8  ##    This program is free software; you can redistribute it and/or modify 
 9  ##    it under the terms of the GNU General Public License as published by 
10  ##    the Free Software Foundation; either version 2 of the License, or 
11  ##    (at your option) any later version. 
12  ## 
13  ##    This program is distributed in the hope that it will be useful, 
14  ##    but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  ##    GNU General Public License for more details. 
17  ## 
18  ##    You should have received a copy of the GNU General Public License along 
19  ##    with this program; if not, write to the Free Software Foundation, Inc., 
20  ##    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
21  ## 
22  ## 
23   
24   
25  import pkg_resources 
26  import gtk 
27  import gtk.glade as glade 
28  import locale, gettext 
29   
30 -class Peggy(object):
31 - def __init__(self, package):
32 """ ``package``: the name of the module/package you want to 33 get ressources from""" 34 self.package=package
35
36 - def exists(self, name):
37 """ Does the ressource ``name`` exist?""" 38 return pkg_resources.resource_exists(self.package, name)
39
40 - def is_dir(self, name):
41 """ Is the ressource ``name`` a directory?""" 42 return pkg_resources.resource_isdir(self.package, name)
43
44 - def list_dir(self, name):
45 """list list the content of ressource ``name``, like os.listdir""" 46 return pkg_resources.resource_listdir(self.package, name)
47
48 - def get_stream(self, name):
49 """get ressource ``name`` as a file-like object""" 50 return pkg_resources.resource_stream(self.package, name)
51
52 - def get_string(self, name):
53 """get ressource ``name`` as a string""" 54 return pkg_resources.resource_string(self.package, name)
55
56 - def get_filename(self, name):
57 """get a true filesystem filename for ressource ``name``""" 58 return pkg_resources.resource_filename(self.package, name)
59
60 - def get_glade(self, name, *args, **kwargs):
61 """get ressource ``name`` as a gtk.glade.XML instance""" 62 xml = self.get_string(name) 63 return glade.xml_new_from_buffer(xml, len(xml), *args, **kwargs)
64
65 - def get_pixbuf(self, name):
66 """get ressource ``name`` as a gtk.gdk.pixbuf instance""" 67 fname = self.get_filename(name) 68 return gtk.gdk.pixbuf_new_from_file(fname)
69 70
71 - def get_locale_dir(self, dir=None):
72 """get ressource ``dir`` for use as a locale's directory 73 if ``dir`` is ``None`` or not given ``locale`` is used 74 """ 75 if dir is None: 76 dir = 'locale' 77 return self.get_filename(dir)
78
79 - def setup_locales(self, domain=None, dir=None, use_unicode=1):
80 """ 81 setup everything to use locales. 82 ``domain`` defaults to the ``package`` used with ``__init__`` 83 ``dir`` is used by ``get_locale_dir`` 84 """ 85 if domain is None: 86 domain = self.package 87 locale.setlocale(locale.LC_ALL, '') 88 89 dir = self.get_locale_dir(dir) 90 gettext.install(domain, dir, unicode=use_unicode) 91 gtk.glade.bindtextdomain(domain, dir) 92 gtk.glade.textdomain(domain)
93