Changeset 3413 for pykota/trunk/pykota/utils.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/utils.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 41 41 try : 42 42 charset = charset or locale.getpreferredencoding() 43 except locale.Error : 43 except locale.Error : 44 44 charset = sys.stdout.encoding or sys.getfilesystemencoding() 45 45 46 46 if (not charset) or charset in ("ASCII", "ANSI_X3.4-1968") : 47 47 charset = "UTF-8" 48 48 49 49 return (language, charset) 50 50 … … 53 53 if value is None : 54 54 value = "None" 55 os.environ[varname] = value.encode(charset, "replace") 56 55 os.environ[varname] = value.encode(charset, "replace") 56 57 57 def initgettext(lang, cset) : 58 58 """Initializes gettext translations for PyKota.""" … … 60 60 try : 61 61 trans = gettext.translation("pykota", \ 62 languages=["%s.%s" % (lang, 63 cset)], 62 languages=["%s.%s" % (lang, 63 cset)], 64 64 codeset=cset) 65 65 except TypeError : # Python <2.4 66 trans = gettext.translation("pykota", 67 languages=["%s.%s" % (lang, 66 trans = gettext.translation("pykota", 67 languages=["%s.%s" % (lang, 68 68 cset)]) 69 69 trans.install(unicode=True) 70 70 except : 71 71 gettext.NullTranslations().install(unicode=True) 72 72 73 73 def getpreferredlanguage() : 74 74 """Returns the preferred language.""" … … 76 76 langs = [l.strip().split(';')[0] for l in languages.split(",")] 77 77 return langs[0].replace("-", "_") 78 78 79 79 def getpreferredcharset() : 80 80 """Returns the preferred charset.""" … … 83 83 return charsets[0] 84 84 85 def reinitcgilocale() : 85 def reinitcgilocale() : 86 86 """Reinitializes the locale and gettext translations for CGI scripts, according to browser's preferences.""" 87 87 initgettext(*initlocale(getpreferredlanguage(), getpreferredcharset())) 88 88 89 89 def N_(message) : 90 90 """Fake translation marker for translatable strings extraction.""" … … 93 93 def databaseToUnicode(text) : 94 94 """Converts from database format (UTF-8) to unicode. 95 95 96 96 We use "replace" to accomodate legacy datas which may not 97 97 have been recorded correctly. … … 100 100 if not isinstance(text, UnicodeType) : 101 101 return text.decode("UTF-8", "replace") 102 else : 102 else : 103 103 # MySQL already returns unicode objects 104 104 return text 105 else : 105 else : 106 106 return None 107 107 108 108 def unicodeToDatabase(text) : 109 109 """Converts from unicode to database format (UTF-8).""" 110 if text is not None : 110 if text is not None : 111 111 return text.encode("UTF-8") 112 else : 112 else : 113 113 return None 114 115 def getdefaultcharset() : 114 115 def getdefaultcharset() : 116 116 """Returns the default charset to use.""" 117 117 return sys.stdout.encoding or locale.getlocale()[1] or "ANSI_X3.4-1968" 118 118 119 119 def logerr(text) : 120 120 """Logs an unicode text to stderr.""" 121 121 sys.stderr.write(text.encode(getdefaultcharset(), "replace")) 122 122 sys.stderr.flush() 123 123 124 124 def loginvalidparam(opt, value, defaultvalue=None, additionalinfo=None) : 125 125 """Logs an error when an invalid parameter to a command line option 126 126 is encountered. 127 """ 127 """ 128 128 message = _("Invalid value '%(value)s' for the %(opt)s command line option") \ 129 129 % locals() 130 if defaultvalue is not None : 130 if defaultvalue is not None : 131 131 message += ", using default '%(defaultvalue)s' instead" % locals() 132 132 if additionalinfo : 133 133 logerr("%s (%s)\n" % (message, additionalinfo)) 134 else : 134 else : 135 135 logerr("%s\n" % message) 136 137 def crashed(message="Bug in PyKota") : 136 137 def crashed(message="Bug in PyKota") : 138 138 """Minimal crash method.""" 139 139 import traceback … … 146 146 logerr(msg) 147 147 return msg 148 148 149 149 def run(optparser, workclass, requireargs=False) : 150 150 """Runs a PyKota command line tool.""" 151 151 appname = os.path.basename(sys.argv[0]) 152 152 retcode = 0 153 (options, arguments) = optparser.parse_args() 153 (options, arguments) = optparser.parse_args() 154 154 if requireargs and not arguments : 155 155 logerr("%s\n" % (_("%(appname)s requires arguments, please use --help") \ 156 156 % locals())) 157 157 retcode = -1 158 application = None 158 application = None 159 159 try : 160 160 try : … … 162 162 application.deferredInit() 163 163 retcode = application.main(arguments, options) 164 except KeyboardInterrupt : 164 except KeyboardInterrupt : 165 165 logerr("\nInterrupted with Ctrl+C !\n") 166 166 retcode = -3 167 except PyKotaCommandLineError, msg : 167 except PyKotaCommandLineError, msg : 168 168 logerr("%s : %s\n" % (sys.argv[0], msg)) 169 169 retcode = -2 170 except SystemExit : 170 except SystemExit : 171 171 pass 172 172 except : … … 174 174 try : 175 175 application.crashed(title) 176 except : 176 except : 177 177 crashed(title) 178 178 retcode = -1 179 finally : 179 finally : 180 180 try : 181 181 application.storage.close() 182 except : 182 except : 183 183 pass 184 185 sys.exit(retcode) 184 185 sys.exit(retcode)