/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/__init__.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/__init__.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2642 - (hide annotations)
Fri Jul 1 20:49:04 2005 UTC (19 years, 8 months ago) by bh
Original Path: trunk/thuban/Thuban/__init__.py
File MIME type: text/x-python
File size: 4944 byte(s)
First step towards unicode.  With this roughly we're at step 1
string_representation.txt

* Doc/technotes/string_representation.txt: New.  Document how
strings are represented in Thuban and how to get to a Unicode
Thuban.

* Thuban/__init__.py (set_internal_encoding)
(unicode_from_internal, internal_from_unicode): New. The first few
functions for the internal string representation

* Thuban/UI/about.py (unicodeToLocale): Removed.  Use
internal_from_unicode instead.

* Thuban/UI/__init__.py (install_wx_translation): Determine the
encoding to use for the internal string representation.  Also,
change the translation function to return strings in internal
representation even on unicode builds of wxPython

* Thuban/Model/load.py (SessionLoader.check_attrs): Decode
filenames too.
(SessionLoader.start_clrange): Use check_attrs to decode and check
the attributes.

* Thuban/Model/xmlreader.py (XMLReader.encode): Use
internal_from_unicode to convert unicode strings.

* Thuban/Model/xmlwriter.py (XMLWriter.encode): Use
unicode_from_internal when applicable

* test/runtests.py (main): New command line option:
internal-encoding to specify the internal string encoding to use
in the tests.

* test/support.py (initthuban): Set the internal encoding to
latin-1

* test/test_load.py (TestSingleLayer.test, TestClassification.test)
(TestLabelLayer.test): Use the internal string representation when
dealing with non-ascii characters

* test/test_load_1_0.py (TestSingleLayer.test)
(TestClassification.test, TestLabelLayer.test): Use the internal
string representation when dealing with non-ascii characters

* test/test_load_0_9.py (TestSingleLayer.test)
(TestClassification.test): Use the internal string representation
when dealing with non-ascii characters

* test/test_load_0_8.py (TestUnicodeStrings.test): Use the
internal string representation when dealing with non-ascii
characters

* test/test_save.py (XMLWriterTest.testEncode)
(SaveSessionTest.testClassifiedLayer): Use the internal string
representation when dealing with non-ascii characters where
applicable

1 bh 2642 # Copyright (c) 2001, 2003, 2005 by Intevation GmbH
2 bh 6 # Authors:
3     # Bernhard Herzog <[email protected]>
4 jan 373 # Jan-Oliver Wagner <[email protected]>
5 bh 6 #
6     # This program is free software under the GPL (>=v2)
7     # Read the file COPYING coming with Thuban for details.
8 jan 373
9 bh 671 import os
10    
11 bh 1871 # Thuban Message Translation
12 bh 671 #
13 bh 1871 # This is somewhat tricky. On the one hand we want to use the wx
14     # facilities because that way the standard wx messages are also
15     # translated and we get automatic conversion of encodings so the we can
16     # use e.g. an UTF po/mo file in a Latin 1 environment. OTOH, we do not
17     # want to import the wxPython modules at all when running the test suite
18     # because otherwise the test suite would require a working X server
19     # connection.
20 bh 671 #
21 bh 1871 # Therefore this module only provides the hooks for installing the
22     # correct translation function with a default translation function that
23     # does nothing and simply returns the string it gets as argument.
24     #
25     # The front end to the installed translation function is _ (see it's
26     # doc-string).
27     #
28     # The Thuban.UI module is responsible for installing the wx translation
29     # function. It must take care to install the translation function as
30     # early as possible, i.e. when Thuban/UI/__init__.py is executed so that
31     # strings translated at module import time are translated (this also
32     # means that a program built on top of Thuban and which uses Thuban.UI
33     # should start by importing Thuban.UI before any other Thuban module.
34     #
35     # At the same time Thuban/UI/__init__.py should not import any wxPython
36     # module unless it really has to install the translation function, i.e.
37     # when no other translation function has already been installed. That
38     # way the test suite can override the wx translation by installing its
39     # own translation function before importing anything from Thuban.UI and
40     # actually before importing anything but the Thuban module itself.
41    
42     # Thedirectory holding the translation files (actually they're in
43     # language specific subdirectories of _message_dir)
44 bh 671 _message_dir = os.path.join(os.path.dirname(__file__), os.pardir, "Resources",
45     "Locale")
46    
47 bh 1871 def _(s):
48     """Return a localized version of the the string s
49 bh 671
50 bh 1871 This is the function to use in the sources to translate strings and
51     it simply delegates the translation to the installable translation
52     function. It's done this way so that _ may be imported with 'from
53     Thuban import _' even when the correct translation function hasn't
54     been installed yet.
55     """
56     return _translation_function(s)
57    
58     def gettext_identity(s):
59     """Default gettext implementation which returns the string as is"""
60     return s
61    
62     _translation_function = gettext_identity
63    
64     def translation_function_installed():
65     """Return whether a translation function has been installed."""
66     return _translation_function is not gettext_identity
67    
68     def install_translation_function(function):
69     """Install function as the translation function
70    
71     If a translation has already been installed that is not the default
72     implementation (gettext_identity) do nothing.
73     """
74     global _translation_function
75     if not translation_function_installed():
76     _translation_function = function
77 bh 2642
78    
79    
80     # String representation in Thuban
81     #
82     # Thuban has an internal representation for textual data that all text
83     # that comes into Thuban has to be converted into. Any text written by
84     # Thuban has to be converted to whatever is needed by the output device.
85     #
86     # Currently, the internal representation is usually a byte-string in a
87     # particuler encoding. For more details see the file
88     # Doc/technotes/string_representation.txt.
89    
90     # The encoding to use for the internal string representation. Usually
91     # it's a string with the encoding name to use when converting between
92     # Python byte-strings and unicode objects. The special value "unicode"
93     # means the use unicode objects as the internal representation.
94     _internal_encoding = None
95    
96     def internal_from_unicode(unistr):
97     """Return Thuban's internal representation for the unicode object unistr"""
98     if _internal_encoding != "unicode":
99     # we use replace so that we don't get exceptions when the
100     # conversion can't be done properly.
101     return unistr.encode(_internal_encoding, "replace")
102     else:
103     return unistr
104    
105     def unicode_from_internal(s):
106     """Return the unicode object for the string s in internal representation"""
107     if _internal_encoding != "unicode":
108     return unicode(s, _internal_encoding)
109     else:
110     return s
111    
112    
113     def set_internal_encoding(encoding):
114     """Set the encoding to use for the internal string representation
115    
116     The parameter should be the name of an encoding known to Python so
117     that it can be used with e.g. the encode method of unicode objects.
118     As a special case it can be the string 'unicode' to indicate that
119     the internal representation are unicode objects.
120     """
121     global _internal_encoding
122     _internal_encoding = encoding

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26