root/MGET/Trunk/PythonPackage/src/GeoEco/Internationalization.py

Revision 65, 4.5 kB (checked in by jjr8, 3 years ago)

Partial fix for ArcGIS 9.2 support (#51):

* GeoprocessorManager?.GetGeoprocessor? does not return a weakref to the object returned by arcgisscripting.create(). That fails because the object does not have a weakref attribute.

* The methods of the object returned by arcgisscripting.create() cannot be inspected with Python's inspect module, because they are builtin functions. Changed _ArcGISObjectWrapper.getattr to assueme these are prototyped as func(*args) instead of using inspect.

* Changed GeoEco?.ArcGIS module to pass Unicode to the geoprocessor object returned by win32com and 8-bit strings to the object returned by arcgisscripting.create(). The caller still always passes in Unicode and receives it back.

* Changed the handling of the OverwriteOutput? environment variable to expect a bool from the geoprocessor object returned by win32com and an int from the object returned by arcgisscripting.create().

Also fixed #50: Renamed OverriddenByArcGISGeoprocessorVariable to InitializeToArcGISGeoprocessorVariable. Changed the design such that any parameter flagged with that is only initialized to the specified variable when the method is invoked as an ArcGIS tool. In all other circumstances (e.g. Python, COM) the caller is responsible for setting the variable explicitly.

Line 
1 # Internationalization.py - Provides internationalization/localization
2 # infrastructure for the GeoEco Python package.
3 #
4 # Copyright (C) 2007 Jason J. Roberts
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License (available in the file LICENSE.TXT)
15 # for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 import codecs
22 import locale
23 import gettext
24 import inspect
25 import os
26 import os.path
27 import sys
28 import types
29
30 def _InitializeTranslationsClass():
31
32     # Build a list of languages, prioritized according to what the user might
33     # want to view.
34
35     languages = []
36
37     # First use the current process-wide locale. If we're being called from a
38     # language-aware application, it may have set the locale. If not, this
39     # function will return None.
40     
41     lc = locale.getlocale()[0]
42     if lc is not None:
43         languages.append(lc)
44
45     # Next try the default locale.
46     
47     lc = locale.getdefaultlocale()[0]
48     if lc is not None:
49         languages.append(lc)
50
51     # Next search the environment variables that might specify languages. These
52     # are typically only present on UNIX. locale.getdefaultlocale also searches
53     # these but returns only the first one it finds. We want the others to be in
54     # our list, so if the first one is not available, we'll fall back to the
55     # others.
56
57     for variable in ['LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG']:
58         if os.environ.has_key(variable):
59             value = os.environ[variable]
60             languages.extend(value.split(':'))
61
62     # Return the Translations instance.
63
64     gettext.translation(domain='GeoEco',
65                         localedir=os.path.join(os.path.dirname(inspect.getfile(sys.modules[__name__])), u'LanguageFiles'),
66                         languages=languages,
67                         fallback=True)
68
69 _Translations = None
70 try:
71     _Translations = _InitializeTranslationsClass()
72 except:
73     pass
74
75 def _gettext(s):
76     return s
77
78 if _Translations is not None:
79     _ = _Translations.ugettext
80 else:
81     _ = _gettext
82
83 _UserPreferredEncoder = None
84
85 def UnicodeToUserPreferredEncoding(obj):
86     if isinstance(obj, types.ListType):
87         return _EncodeList(obj)
88     if isinstance(obj, types.TupleType):
89         l = list(obj)
90         l = _EncodeList(l)
91         return tuple(l)
92     if not isinstance(obj, types.UnicodeType):
93         return obj
94     if globals()['_UserPreferredEncoder'] is None:
95         try:
96             globals()['_UserPreferredEncoder'] = codecs.getencoder(locale.getpreferredencoding(do_setlocale=False))
97         except:
98             globals()['_UserPreferredEncoder'] = codecs.getencoder('ascii')
99     return globals()['_UserPreferredEncoder'](obj, 'strict')[0]
100
101 def _EncodeList(obj):
102     for i in range(len(obj)):
103         obj[i] = UnicodeToUserPreferredEncoding(obj[i])
104     return obj
105
106 _UserPreferredDecoder = None
107
108 def UserPreferredEncodingToUnicode(obj):
109     if isinstance(obj, types.ListType):
110         return _DecodeList(obj)
111     if isinstance(obj, types.TupleType):
112         l = list(obj)
113         l = _DecodeList(l)
114         return tuple(l)
115     if not isinstance(obj, types.StringType):
116         return obj
117     if globals()['_UserPreferredDecoder'] is None:
118         try:
119             globals()['_UserPreferredDecoder'] = codecs.getdecoder(locale.getpreferredencoding(do_setlocale=False))
120         except:
121             globals()['_UserPreferredDecoder'] = codecs.getdecoder('ascii')
122     return globals()['_UserPreferredDecoder'](obj, 'strict')[0]
123
124 def _DecodeList(obj):
125     for i in range(len(obj)):
126         obj[i] = UserPreferredEncodingToUnicode(obj[i])
127     return obj
128        
129
130 ###############################################################################
131 # Names exported by this module
132 ###############################################################################
133
134 __all__ = ['_', 'UnicodeToUserPreferredEncoding', 'UserPreferredEncodingToUnicode']
Note: See TracBrowser for help on using the browser.