Reorganized imports, eliminated a potential UnicodeEncodeError.
authorPascal Volk <neverseen@users.sourceforge.net>
Mon, 31 Aug 2009 04:58:45 +0000
changeset 136 fb61f64e6351
parent 135 32ad0c79a6ef
child 137 fc09f657082d
Reorganized imports, eliminated a potential UnicodeEncodeError. Removed double definition of function w_std() in Config class.
VirtualMailManager/Config.py
VirtualMailManager/VirtualMailManager.py
vmm
--- a/VirtualMailManager/Config.py	Tue Aug 25 12:17:56 2009 +0000
+++ b/VirtualMailManager/Config.py	Mon Aug 31 04:58:45 2009 +0000
@@ -9,23 +9,14 @@
 
 from constants.VERSION import *
 
-import locale
-import sys
 from shutil import copy2
 from ConfigParser import ConfigParser, MissingSectionHeaderError, ParsingError
 from cStringIO import StringIO
 
+from __main__ import ENCODING, w_std
 from Exceptions import VMMConfigException
 import constants.ERROR as ERR
 
-locale.setlocale(locale.LC_ALL, '')
-ENCODING = locale.nl_langinfo(locale.CODESET)
-
-def w_std(*args):
-    for arg in args:
-        sys.stdout.write(arg.encode(ENCODING, 'replace'))
-        sys.stdout.write('\n')
-
 class Config(ConfigParser):
     """This class is for reading and modifying vmm's configuration file."""
 
--- a/VirtualMailManager/VirtualMailManager.py	Tue Aug 25 12:17:56 2009 +0000
+++ b/VirtualMailManager/VirtualMailManager.py	Mon Aug 31 04:58:45 2009 +0000
@@ -8,8 +8,6 @@
 
 import os
 import re
-import sys
-import locale
 from encodings.idna import ToASCII, ToUnicode
 from getpass import getpass
 from shutil import rmtree
@@ -18,6 +16,7 @@
 from pyPgSQL import PgSQL # python-pgsql - http://pypgsql.sourceforge.net
 
 import constants.ERROR as ERR
+from __main__ import ENCODING, w_std
 from ext.Postconf import Postconf
 from Account import Account
 from Alias import Alias
@@ -34,8 +33,6 @@
 RE_DOMAIN_SRCH = """^[a-z0-9-\.]+$"""
 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
 RE_MBOX_NAMES = """^[\x20-\x25\x27-\x7E]*$"""
-locale.setlocale(locale.LC_ALL, '')
-ENCODING = locale.nl_langinfo(locale.CODESET)
 
 class VirtualMailManager(object):
     """The main class for vmm"""
@@ -60,7 +57,7 @@
             self.__cfgSections = self.__Cfg.getsections()
             self.__scheme = self.__Cfg.get('misc', 'passwdscheme')
             self._postconf = Postconf(self.__Cfg.get('bin', 'postconf'))
-        if not sys.argv[1] in ['cf', 'configure']:
+        if not os.sys.argv[1] in ['cf', 'configure']:
             self.__chkenv()
 
     def __findCfgFile(self):
@@ -204,17 +201,17 @@
     relocatedExists = staticmethod(relocatedExists)
 
     def _readpass(self):
+        readp_msg0 = _(u'Enter new password: ').encode(ENCODING, 'replace')
+        readp_msg1 = _(u'Retype new password: ').encode(ENCODING, 'replace')
         mismatched = True
         while mismatched:
-            clear0 = getpass(prompt=_('Enter new password: '))
-            clear1 = getpass(prompt=_('Retype new password: '))
+            clear0 = getpass(prompt=readp_msg0)
+            clear1 = getpass(prompt=readp_msg1)
             if clear0 != clear1:
-                msg = _('Sorry, passwords do not match')
-                sys.stderr.write('%s\n' % msg.encode(ENCODING, 'replace'))
+                w_std(_(u'Sorry, passwords do not match'))
                 continue
             if len(clear0) < 1 or len(clear1) < 1:
-                msg = _('Sorry, empty passwords are not permitted')
-                sys.stderr.write('%s\n' % msg.encode(ENCODING, 'replace'))
+                w_std(_(u'Sorry, empty passwords are not permitted'))
                 continue
             mismatched = False
         return clear0
--- a/vmm	Tue Aug 25 12:17:56 2009 +0000
+++ b/vmm	Mon Aug 31 04:58:45 2009 +0000
@@ -8,8 +8,16 @@
 from VirtualMailManager.constants.VERSION import *
 
 import locale
+# do it early - for import in in the other files
+locale.setlocale(locale.LC_ALL, '')
+ENCODING = locale.nl_langinfo(locale.CODESET)
+
+def w_std(*args):
+    for arg in args:
+        os.sys.stdout.write(arg.encode(ENCODING, 'replace'))
+        os.sys.stdout.write('\n')
+
 import os
-import sys
 import gettext
 from time import strftime, strptime
 
@@ -21,14 +29,9 @@
 
 def w_err(code, *args):
     for arg in args:
-        sys.stderr.write(arg.encode(ENCODING, 'replace'))
-        sys.stderr.write('\n')
-    sys.exit(code)
-
-def w_std(*args):
-    for arg in args:
-        sys.stdout.write(arg.encode(ENCODING, 'replace'))
-        sys.stdout.write('\n')
+        os.sys.stderr.write(arg.encode(ENCODING, 'replace'))
+        os.sys.stderr.write('\n')
+    os.sys.exit(code)
 
 def usage(excode=0, errMsg=None):
     u_head = _("""\
@@ -73,7 +76,7 @@
             w_err(excode, u_head, u_body, '%s: %s\n' % (_('Error'), errMsg))
     else:
         w_std(u_head, u_body)
-        sys.exit(excode)
+        os.sys.exit(excode)
 
 def get_vmm():
     try:
@@ -411,17 +414,15 @@
     w_std('%s, %s %s (%s %s)\nPython %s %s %s\n\n%s %s' % (__prog__,
         _('version'), __version__, _('from'), strftime(
             locale.nl_langinfo(locale.D_FMT), strptime(__date__, '%Y-%m-%d')),
-        sys.version.split()[0], _(u'on'), os.uname()[0], __prog__,
+        os.sys.version.split()[0], _(u'on'), os.uname()[0], __prog__,
         'is free software and comes with ABSOLUTELY NO WARRANTY.'))
 
 #def main():
 if __name__ == '__main__':
-    __prog__ = os.path.basename(sys.argv[0])
-    locale.setlocale(locale.LC_ALL, '')
-    ENCODING = locale.nl_langinfo(locale.CODESET)
+    __prog__ = os.path.basename(os.sys.argv[0])
     gettext.install(__prog__, '/usr/local/share/locale', unicode=1)
-    argv = [unicode(arg, ENCODING) for arg in sys.argv]
-    argc = len(sys.argv)
+    argv = [unicode(arg, ENCODING) for arg in os.sys.argv]
+    argc = len(os.sys.argv)
 
     if argc < 2:
         usage(EXIT.MISSING_ARGS)