File: FtCore.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (58 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (12)
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
"""
Contains various definitions common to modules acquired from 4Suite
"""

__all__ = ["FtException", "get_translator"]


class FtException(Exception):
    def __init__(self, errorCode, messages, args):
        # By defining __str__, args will be available.  Otherwise
        # the __init__ of Exception sets it to the passed in arguments.
        self.params = args
        self.errorCode = errorCode
        self.message = messages[errorCode] % args
        Exception.__init__(self, self.message, args)

    def __str__(self):
        return self.message


# What follows is used to provide support for I18N in the rest of the
# 4Suite-derived packages in PyXML.
#
# Each sub-package of the top-level "xml" package that contains 4Suite
# code is really a separate text domain, but they're all called
# '4Suite'.  For each domain, a translation object is provided using
# message catalogs stored inside the package.  The code below defines
# a get_translator() function that returns an appropriate gettext
# function to be used as _() in the sub-package named by the
# parameter.  This handles all the compatibility issues related to
# Python versions (whether the gettext module can be found) and
# whether the message catalogs can actually be found.

def _(msg):
    return msg

try:
    import gettext

except (ImportError, IOError):
    def get_translator(pkg):
        return _

else:
    import os

    _cache = {}
    _top = os.path.dirname(os.path.abspath(__file__))

    def get_translator(pkg):
        if not _cache.has_key(pkg):
            locale_dir = os.path.join(_top, pkg.replace(".", os.sep))
            try:
                f = gettext.translation('4Suite', locale_dir).gettext
            except IOError:
                f = _
            _cache[pkg] = f
        return _cache[pkg]