File: __init__.py

package info (click to toggle)
thuban 1.2.2-14
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,176 kB
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 145
file content (142 lines) | stat: -rw-r--r-- 5,651 bytes parent folder | download | duplicates (4)
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
# Copyright (c) 2001, 2003, 2005 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
# Jan-Oliver Wagner <jan@intevation.de>
# Bernhard Reiter <bernhard@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

import os
import sys

# Thuban Message Translation
#
# This is somewhat tricky. On the one hand we want to use the wx
# facilities because that way the standard wx messages are also
# translated and we get automatic conversion of encodings so the we can
# use e.g. an UTF po/mo file in a Latin 1 environment. OTOH, we do not
# want to import the wxPython modules at all when running the test suite
# because otherwise the test suite would require a working X server
# connection.
#
# Therefore this module only provides the hooks for installing the
# correct translation function with a default translation function that
# does nothing and simply returns the string it gets as argument.
#
# The front end to the installed translation function is _ (see it's
# doc-string).
#
# The Thuban.UI module is responsible for installing the wx translation
# function. It must take care to install the translation function as
# early as possible, i.e. when Thuban/UI/__init__.py is executed so that
# strings translated at module import time are translated (this also
# means that a program built on top of Thuban and which uses Thuban.UI
# should start by importing Thuban.UI before any other Thuban module.
#
# At the same time Thuban/UI/__init__.py should not import any wxPython
# module unless it really has to install the translation function, i.e.
# when no other translation function has already been installed. That
# way the test suite can override the wx translation by installing its
# own translation function before importing anything from Thuban.UI and
# actually before importing anything but the Thuban module itself.

# Thedirectory holding the translation files (actually they're in
# language specific subdirectories of _message_dir)



def _(s):
    """Return a localized version of the the string s

    This is the function to use in the sources to translate strings and
    it simply delegates the translation to the installable translation
    function. It's done this way so that _ may be imported with 'from
    Thuban import _' even when the correct translation function hasn't
    been installed yet.
    """
    return _translation_function(s)

#from Lib.fileutil import get_thuban_dir;
#_message_dir = os.path.join(get_thuban_dir(), "Resources", "Locale")
# 
# The directory where the default projections are stored has to be changed
# for the debian package to be conformant with the FHS
#_message_dir = os.path.join(os.path.dirname(__file__), os.pardir, "Resources",
#                            "Locale")
_message_dir = "/usr/share/thuban/Resources/Locale"

def gettext_identity(s):
    """Default gettext implementation which returns the string as is"""
    return s

_translation_function = gettext_identity

def translation_function_installed():
    """Return whether a translation function has been installed."""
    return _translation_function is not gettext_identity

def install_translation_function(function):
    """Install function as the translation function

    If a translation has already been installed that is not the default
    implementation (gettext_identity) do nothing.
    """
    global _translation_function
    if not translation_function_installed():
        _translation_function = function



# String representation in Thuban
#
# Thuban has an internal representation for textual data that all text
# that comes into Thuban has to be converted into.  Any text written by
# Thuban has to be converted to whatever is needed by the output device.
#
# Currently, the internal representation is usually a byte-string in a
# particuler encoding.  For more details see the file
# Doc/technotes/string_representation.txt.

# The encoding to use for the internal string representation.  Usually
# it's a string with the encoding name to use when converting between
# Python byte-strings and unicode objects.  The special value "unicode"
# means the use unicode objects as the internal representation.
_internal_encoding = None

def internal_from_unicode(unistr):
    """Return Thuban's internal representation for the unicode object unistr"""
    if _internal_encoding != "unicode":
        # we use replace so that we don't get exceptions when the
        # conversion can't be done properly.
        return unistr.encode(_internal_encoding, "replace")
    else:
        return unistr

def unicode_from_internal(s):
    """Return the unicode object for the string s in internal representation"""
    if _internal_encoding != "unicode":
        return unicode(s, _internal_encoding)
    else:
        return s


def set_internal_encoding(encoding):
    """Set the encoding to use for the internal string representation

    The parameter should be the name of an encoding known to Python so
    that it can be used with e.g. the encode method of unicode objects.
    As a special case it can be the string 'unicode' to indicate that
    the internal representation are unicode objects.
    """
    global _internal_encoding
    _internal_encoding = encoding

    # and now let us test, if we can go back and forth
    # it is better to complain now than to have runtime problems later
    unicode_from_internal(internal_from_unicode(u''))

def get_internal_encoding():
    """Return the encoding used for Thuban's internal string representation."""
    global _internal_encoding
    return _internal_encoding