# HG changeset patch # User Pascal Volk # Date 1272077385 0 # Node ID 6eea85d8b91db879aa1be8be028541225f69a924 # Parent 1c2241dde94274bb82c794724e48fc201a0d7f59 VMM: moved some non-init functions to the new common module. Adjusted imports in the Config and Handler module. diff -r 1c2241dde942 -r 6eea85d8b91d VirtualMailManager/Config.py --- a/VirtualMailManager/Config.py Fri Apr 23 03:18:44 2010 +0000 +++ b/VirtualMailManager/Config.py Sat Apr 24 02:49:45 2010 +0000 @@ -14,7 +14,7 @@ ParsingError, RawConfigParser from cStringIO import StringIO# TODO: move interactive stff to cli -from VirtualMailManager import exec_ok, get_unicode, is_dir +from VirtualMailManager.common import exec_ok, get_unicode, is_dir from VirtualMailManager.constants.ERROR import CONF_ERROR from VirtualMailManager.errors import ConfigError diff -r 1c2241dde942 -r 6eea85d8b91d VirtualMailManager/Handler.py --- a/VirtualMailManager/Handler.py Fri Apr 23 03:18:44 2010 +0000 +++ b/VirtualMailManager/Handler.py Sat Apr 24 02:49:45 2010 +0000 @@ -21,10 +21,11 @@ from pyPgSQL import PgSQL # python-pgsql - http://pypgsql.sourceforge.net import VirtualMailManager.constants.ERROR as ERR -from VirtualMailManager import ENCODING, exec_ok, set_configuration +from VirtualMailManager import ENCODING, set_configuration from VirtualMailManager.Account import Account from VirtualMailManager.Alias import Alias from VirtualMailManager.AliasDomain import AliasDomain +from VirtualMailManager.common import exec_ok from VirtualMailManager.Config import Config as Cfg from VirtualMailManager.Domain import Domain, ace2idna, get_gid from VirtualMailManager.EmailAddress import EmailAddress diff -r 1c2241dde942 -r 6eea85d8b91d VirtualMailManager/__init__.py --- a/VirtualMailManager/__init__.py Fri Apr 23 03:18:44 2010 +0000 +++ b/VirtualMailManager/__init__.py Sat Apr 24 02:49:45 2010 +0000 @@ -9,23 +9,16 @@ """ import gettext -import os import locale - -from VirtualMailManager.constants.ERROR import \ - NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY from VirtualMailManager.constants.version import __author__, __date__, \ __version__ -from VirtualMailManager.errors import VMMError - __all__ = [ # version information from VERSION '__author__', '__date__', '__version__', # defined stuff - 'ENCODING', 'Configuration', 'exec_ok', 'expand_path', 'get_unicode', - 'is_dir', 'set_configuration', + 'ENCODING', 'Configuration', 'set_configuration', ] @@ -42,9 +35,6 @@ gettext.install('vmm', '/usr/local/share/locale', unicode=1) -_ = lambda msg: msg - - def set_configuration(cfg_obj): """Assigns the *cfg_obj* to the global `Configuration`. *cfg_obj* has to be a `VirtualMailManager.Config.Config` instance.""" @@ -52,52 +42,3 @@ assert isinstance(cfg_obj, Config) global Configuration Configuration = cfg_obj - - -def get_unicode(string): - """Converts `string` to `unicode`, if necessary.""" - if isinstance(string, unicode): - return string - return unicode(string, ENCODING, 'replace') - - -def expand_path(path): - """Expands paths, starting with ``.`` or ``~``, to an absolute path.""" - if path.startswith('.'): - return os.path.abspath(path) - if path.startswith('~'): - return os.path.expanduser(path) - return path - - -def is_dir(path): - """Checks if `path` is a directory. - - Throws a `VMMError` if `path` is not a directory. - - """ - path = expand_path(path) - if not os.path.isdir(path): - raise VMMError(_(u"'%s' is not a directory") % get_unicode(path), - NO_SUCH_DIRECTORY) - return path - - -def exec_ok(binary): - """Checks if the `binary` exists and if it is executable. - - Throws a `VMMError` if the `binary` isn't a file or is not - executable. - - """ - binary = expand_path(binary) - if not os.path.isfile(binary): - raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), - NO_SUCH_BINARY) - if not os.access(binary, os.X_OK): - raise VMMError(_(u"File is not executable: '%s'") % - get_unicode(binary), NOT_EXECUTABLE) - return binary - - -del _ diff -r 1c2241dde942 -r 6eea85d8b91d VirtualMailManager/common.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VirtualMailManager/common.py Sat Apr 24 02:49:45 2010 +0000 @@ -0,0 +1,72 @@ +# -*- coding: UTF-8 -*- +# Copyright (c) 2010, Pascal Volk +# See COPYING for distribution information. + +""" + VirtualMailManager.common + + Some common functions +""" + +import os + +from VirtualMailManager import ENCODING +from VirtualMailManager.constants.ERROR import \ + NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY +from VirtualMailManager.errors import VMMError + +_ = lambda msg: msg + + +def expand_path(path): + """Expands paths, starting with ``.`` or ``~``, to an absolute path.""" + if path.startswith('.'): + return os.path.abspath(path) + if path.startswith('~'): + return os.path.expanduser(path) + return path + + +def get_unicode(string): + """Converts `string` to `unicode`, if necessary.""" + if isinstance(string, unicode): + return string + return unicode(string, ENCODING, 'replace') + + +def is_dir(path): + """Checks if `path` is a directory. + + Throws a `VMMError` if `path` is not a directory. + """ + path = expand_path(path) + if not os.path.isdir(path): + raise VMMError(_(u"'%s' is not a directory") % get_unicode(path), + NO_SUCH_DIRECTORY) + return path + + +def exec_ok(binary): + """Checks if the `binary` exists and if it is executable. + + Throws a `VMMError` if the `binary` isn't a file or is not + executable. + """ + binary = expand_path(binary) + if not os.path.isfile(binary): + raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), + NO_SUCH_BINARY) + if not os.access(binary, os.X_OK): + raise VMMError(_(u"File is not executable: '%s'") % + get_unicode(binary), NOT_EXECUTABLE) + return binary + + +def version_hex(version_string): + """Convert the version string '1.2.3' to an int. + hex(version_hex('1.2.3')) -> '0x10203' + """ + major, minor, patch = map(int, version_string.split('.')) + return (major << 16) + (minor << 8) + patch + +del _