Package peggy :: Module base
[hide private]
[frames] | no frames]

Source Code for Module peggy.base

  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 locale, gettext 
 27   
 28   
29 -class PeggyBase(object):
30 """Base class for Peggy""" 31
32 - def __init__(self, package):
33 """ 34 :Parameters: 35 package 36 the name of the module/package you want to get 37 ressources from 38 """ 39 self.package=package
40
41 - def exists(self, name):
42 """ 43 Does the ressource ``name`` exist? 44 45 :Parameters: 46 name 47 resource name 48 """ 49 return pkg_resources.resource_exists(self.package, name)
50
51 - def is_dir(self, name):
52 """ 53 Is the ressource ``name`` a directory? 54 55 :Parameters: 56 name 57 resource name 58 """ 59 return pkg_resources.resource_isdir(self.package, name)
60
61 - def list_dir(self, name):
62 """ 63 list list the content of ressource ``name``, 64 like os.listdir 65 66 :Parameters: 67 name 68 resource name 69 """ 70 return pkg_resources.resource_listdir(self.package, name)
71
72 - def get_stream(self, name):
73 """ 74 get ressource ``name`` as a file-like object 75 76 :Parameters: 77 name 78 resource name 79 """ 80 return pkg_resources.resource_stream(self.package, name)
81
82 - def get_string(self, name):
83 """ 84 get ressource ``name`` as a string 85 86 :Parameters: 87 name 88 resource name 89 """ 90 return pkg_resources.resource_string(self.package, name)
91
92 - def get_filename(self, name):
93 """ 94 get a true filesystem filename for ressource ``name`` 95 96 :Parameters: 97 name 98 resource name 99 """ 100 return pkg_resources.resource_filename(self.package, name)
101
102 - def get_locale_dir(self, dir=None):
103 """get ressource ``dir`` for use as a locale's directory 104 105 :Parameters: 106 dir 107 ressource name 108 109 if `None` or not given ``locale`` is used 110 """ 111 if dir is None: 112 dir = 'locale' 113 return self.get_filename(dir)
114
115 - def _init_locale_variables(self, domain, dir):
116 """ 117 Used by setup_locales(). Don't use it directly. 118 """ 119 if domain is None: 120 domain = self.package 121 dir = self.get_locale_dir(dir) 122 return domain, dir
123 124
125 - def setup_locales(self, domain=None, dir=None, use_unicode=1):
126 """ 127 setup everything to use locales. 128 129 :Parameters: 130 domain 131 defaults to the ``package`` used with `__init__()` 132 133 dir 134 is used by `get_locale_dir()` 135 """ 136 domain, dir = self._init_locale_variables(domain, dir) 137 locale.setlocale(locale.LC_ALL, '') 138 139 gettext.install(domain, dir, unicode=use_unicode)
140