This file is indexed.

/usr/lib/python2.7/dist-packages/kiwi/environ.py is in python-kiwi 1.9.22-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2005-2006 Async Open Source
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): Johan Dahlin <jdahlin@async.com.br>
#

"""Environment helpers: path mangling and resource management"""

import gettext
import imp
import locale
import os
import sys

from kiwi.log import Logger
from kiwi.python import namedAny

__all__ = ['Application', 'Library', 'app', 'environ', 'require_gazpacho',
           'is_gazpacho_required']

log = Logger('environ')

EnvironmentError = EnvironmentError

# From http://tinyurl.com/77ukj
def _is_frozen():
    "Helper function to check if we're frozen in a py2exe'd file"
    return (hasattr(sys, "frozen") or # new py2exe
            hasattr(sys, "importers") # old py2exe
            or imp.is_frozen("__main__")) # tools/freeze

class Environment:
    """Environment control

    When you want to access a resource on the filesystem, such as
    an image or a glade file you use this object.

    External libraries or applications are free to add extra directories"""

    def __init__(self, root='.'):
        self._resources = {}
        self._extensions = {}
        self._root = root

        # Add some compressed formats as alternative extensions to
        # "glade" resources. A patch has been added to gazpacho trunk
        # (rev. 2251) to support loading those compressed formats.
        self._add_extensions("glade", ".bz2", ".gz")

        self._add_resource_variable("glade", "KIWI_GLADE_PATH")
        self._add_resource_variable("image", "KIWI_IMAGE_PATH")

    def get_root(self):
        return self._root

    def get_log_level(self):
        return os.environ.get('KIWI_LOG')

    def _add_extensions(self, resource, *args):
        exts = self._extensions.setdefault(resource, [])
        exts.extend(list(args))

    def _add_resource_variable(self, resource, variable):
        """Add resources from an environment variable"""
        env = os.environ.get(variable, '')
        for path in env.split(os.pathsep):
            if not path:
                continue
            self.add_resource(resource, env)

    def get_resource_paths(self, resource):
        if not resource in self._resources:
            raise EnvironmentError("No resource called: %s" % resource)
        return self._resources[resource]

    def add_resource(self, resource, path):
        path = os.path.join(self._root, path)

        if not os.path.isdir(path):
            raise EnvironmentError("path %s must be a directory" % path)

        reslist = self._resources.setdefault(resource, [])
        if not path in reslist:
            reslist.append(path)

    def add_resources(self, **kwargs):
        for resource, path in kwargs.items():
            if resource == 'locale':
                try:
                    self.add_resource(resource, path)
	        except EnvironmentError:
                    continue

            self.add_resource(resource, path)

    def find_resource(self, resource, name):
        """Locate a specific resource of called name of type resource"""

        resource_paths = self.get_resource_paths(resource)

        # Look for alternative extensions for this resource.
        # But check without extensions first
        exts = [""] + self._extensions.get(resource, [])

        # Check "scriptdir", which is the directory the script is ran from
        # and the working directory ("") after all the others fail
        scriptdir = os.path.dirname(os.path.abspath(sys.argv[0]))
        for path in resource_paths + [scriptdir, ""]:
            for ext in exts:
                filename = os.path.join(self._root, path, "".join((name, ext)))
                if os.path.exists(filename) and os.path.isfile(filename):
                    return filename

        raise EnvironmentError("Could not find %s resource: %s" % (
            resource, name))

    def _get_epydoc(self):
        if sys.argv == ['IGNORE']:
            return True
        elif os.path.basename(sys.argv[0]) == 'epyrun':
            return True
        return False

    epydoc = property(_get_epydoc)

class Library(Environment):
    """A Library is a local environment object, it's a subclass of the
    Environment class.
    It's used by libraries and applications (through the Application class)

    It provides a way to manage local resources, which should only be seen
    in the current context.

    Libraries are usually instantiated in __init__.py in the topmost package
    in your library, an example usage is kiwi itself which does:

    >>> from kiwi.environ import Library
    >>> lib = Library('kiwi')
    >>> if lib.uninstalled:
    >>>     lib.add_global_resource('glade', 'glade')
    >>>     lib.add_global_resource('pixmap', 'pixmaps')

    which is combined with the following class in setup.py:

    >>> from kiwi.dist import InstallLib
    >>> class InstallLib(TemplateInstallLib):
    >>>    name = 'kiwi'
    >>>    global_resources = dict(glade='$datadir/glade',
    >>>                            pixmap='$datadir/pixmaps')
    >>>
    >>> setup(...,
    >>>       data_files=[('share/kiwi/glade',
    >>>                   listfiles('glade', '*.glade')),
    >>>                   ('share/kiwi/pixmaps',
    >>>                   listfiles('pixmaps', '*.png')),
    >>>       cmdclass=dict(install_lib=InstallLib))

    It may seems like a bit of work, but this is everything that's needed
    for kiwi to figure out how to load resources when installed and when
    running in an uninstalled mode, eg directly from the source tree.
    To locate a pixmap called kiwi.png the following is enough:

    >>> from kiwi.environ import environ
    >>> environ.find_resource('pixmap', 'kiwi.png')
    '/usr/share/kiwi/pixmaps/kiwi.png' # installed mode

    Which will lookup the resource kiwi.png in the domain pixmap, which
    points to $datadir/pixmaps (eg $prefix/share/kiwi/pixmaps) when running
    in installed mode and from $builddir/pixmaps otherwise.

    """
    def __init__(self, name, root='..', dirname=None):
        """
        Creates a new library, this is usually called in __init__.py in a
        toplevel package. All resources will be relative to the I{root}
        directory.

        @param name: name of the library
        @param root: root directory
        @param dirname:
        """
        self.name = name

        # py2exe
        if _is_frozen():
            log.info('py2exe found')
            executable = os.path.realpath(os.path.abspath(sys.executable))
            root = os.path.dirname(executable)
        # normal
        else:
            if dirname == None:
                # Figure out the absolute path to the caller
                caller = sys._getframe(1).f_locals['__file__']
                dirname = os.path.split(caller)[0]

            dirname = os.path.realpath(os.path.abspath(dirname))
            root = os.path.abspath(os.path.join(dirname, root))
        Environment.__init__(self, root=root)

        basedir = os.path.join(root, 'lib', 'python%d.%d' %
                               sys.version_info[:2], 'site-packages')
        if os.path.exists(basedir):
            sys.path.insert(0, basedir)
        g = globals()
        l = locals()
        try:
            module = __import__(name, g, l)
        except ImportError:
            raise ImportError("Failed to import module %s" % name)

        # Load installed
        try:
            module = __import__(name + '.__installed__', g, l, [name])
        except ImportError:
            uninstalled = True
        else:
            uninstalled = False
            if not hasattr(module, 'resources'):
                raise ValueError("module %s.__installed__ must define a "
                                 "resources attribute" % name)
            if not hasattr(module, 'global_resources'):
                raise ValueError("module %s.__installed__ must define a "
                                 "global_resources attribute" % name)
            self.add_resources(**module.resources)
            self.add_global_resources(**module.global_resources)
            self.prefix = module.prefix

        self.uninstalled = uninstalled

    def _check_translation(self, domain, directory):
        loc = locale.getlocale()[0]

        # We're not interested in warnings for these locales
        if loc in (None, 'C', 'en_US'):
            return

        # check sv_SE and sv
        locales = [loc]
        if '_' in loc:
            locales.append(loc.split('_')[0])

        for l in locales:
            path = os.path.join(directory, l, 'LC_MESSAGES', domain + '.mo')
            if os.path.exists(path):
                break
        else:
            log.warn('No %s translation found for domain %s' % (loc, domain))

    def enable_translation(self, domain=None, localedir=None):
        """
        Enables translation for a library

        @param domain: optional, if not specified name sent to constructor
          will be used
        @param localedir: directory to get locales from when running in
          uninstalled mode. If not specified a directory called 'locale' in
          the root will be used.
        """
        if not domain:
            domain = self.name

        if not localedir:
            localedir = 'locale'

        if self.uninstalled:
            try:
                self.add_resource('locale', localedir)
            except EnvironmentError:
                pass

        # XXX: locale should not be a list
        localedir = self._resources.get('locale')
        if not localedir:
            # Only complain when running installed
            if not self.uninstalled:
                log.warn('no localedir for: %s' % domain)

            return
        directory = gettext.bindtextdomain(domain, localedir[0])
        self._check_translation(domain, directory)
        # For libglade, but only on non-win32 systems
        if hasattr(locale, 'bindtextdomain'):
            locale.bindtextdomain(domain, localedir[0])

        # Gtk+ only supports utf-8, it makes no sense to support
        # other encodings in kiwi it self
        # This is not present in Python 2.3
        if hasattr(gettext, 'bind_textdomain_codeset'):
            gettext.bind_textdomain_codeset(domain, 'utf-8')

    def set_application_domain(self, domain):
        """
        Sets the default application domain
        @param domain: the domain
        """
        gettext.textdomain(domain)
        # For libglade, but only on non-win32 systems
        if hasattr(locale, 'textdomain'):
            locale.textdomain(domain)

    def add_global_resource(self, resource, path):
        """Convenience method to add a global resource.
        This is the same as calling kiwi.environ.environ.add_resource
        """
        global environ
        environ.add_resource(resource, os.path.join(self._root, path))

    def add_global_resources(self, **kwargs):
        for resource, path in kwargs.items():
            self.add_global_resource(resource, path)

class Application(Library):
    """Application extends a L{Library}. It's meant to be used
    by applications

    Libraries are usually instantiated in __init__.py in the topmost package
    in your library, an example usage is kiwi itself which does:

    >>> from kiwi.environ import Application
    >>> app = Application('gnomovision')
    >>> if app.uninstalled:
    >>>     app.add_global_resource('glade', 'glade')
    >>>     app.add_global_resource('pixmap', 'pixmaps')

    If you want to do translations, you also need to do the following:

    >>> app.enable_translation()

    @see: L{Library} for more information on how to integrate it with
      the standard distutils configuration.
    """

    def __init__(self, name, root='..', path='main', dirname=None):
        global app
        if app is not None:
            raise TypeError("Application is already set to %r" % app)
        app = self

        if not dirname:
            dirname = os.path.abspath(os.path.dirname(sys.argv[0]))
        Library.__init__(self, name, root, dirname)
        self._path = path

    def _get_main(self):
        try:
            module = namedAny(self._path)
        except:
            log.warn('importing %s' % self._path)
            raise

        main = getattr(module, 'main', None)
        if not main or not callable(main):
            raise SystemExit("ERROR: Could not find item '%s' in module %s" %
                             'main', self._path)
        return main

    def enable_translation(self, domain=None, localedir=None):
        """
        Enables translation for a application
        See L{Library.enable_translation}.

        """
        Library.enable_translation(self, domain, localedir)
        old_domain = gettext.textdomain()
        if old_domain  != 'messages':
            log.warn('overriding default domain, was %s' % old_domain)

        self.set_application_domain(domain)

    def run(self):
        main = self._get_main()

        try:
            sys.exit(main(sys.argv))
        except KeyboardInterrupt:
            raise SystemExit

_require_gazpacho_loader = False

def require_gazpacho():
    global _require_gazpacho_loader
    _require_gazpacho_loader = True

def is_gazpacho_required():
    global _require_gazpacho_loader
    return _require_gazpacho_loader

# Global variables
environ = Environment()

app = None