7 |
7 |
8 VirtualMailManager package initialization code |
8 VirtualMailManager package initialization code |
9 """ |
9 """ |
10 |
10 |
11 import gettext |
11 import gettext |
12 import os |
|
13 import locale |
12 import locale |
14 |
13 |
15 |
|
16 from VirtualMailManager.constants.ERROR import \ |
|
17 NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY |
|
18 from VirtualMailManager.constants.version import __author__, __date__, \ |
14 from VirtualMailManager.constants.version import __author__, __date__, \ |
19 __version__ |
15 __version__ |
20 from VirtualMailManager.errors import VMMError |
|
21 |
|
22 |
16 |
23 __all__ = [ |
17 __all__ = [ |
24 # version information from VERSION |
18 # version information from VERSION |
25 '__author__', '__date__', '__version__', |
19 '__author__', '__date__', '__version__', |
26 # defined stuff |
20 # defined stuff |
27 'ENCODING', 'Configuration', 'exec_ok', 'expand_path', 'get_unicode', |
21 'ENCODING', 'Configuration', 'set_configuration', |
28 'is_dir', 'set_configuration', |
|
29 ] |
22 ] |
30 |
23 |
31 |
24 |
32 # Try to set all of the locales according to the current |
25 # Try to set all of the locales according to the current |
33 # environment variables and get the character encoding. |
26 # environment variables and get the character encoding. |
40 Configuration = None |
33 Configuration = None |
41 |
34 |
42 gettext.install('vmm', '/usr/local/share/locale', unicode=1) |
35 gettext.install('vmm', '/usr/local/share/locale', unicode=1) |
43 |
36 |
44 |
37 |
45 _ = lambda msg: msg |
|
46 |
|
47 |
|
48 def set_configuration(cfg_obj): |
38 def set_configuration(cfg_obj): |
49 """Assigns the *cfg_obj* to the global `Configuration`. |
39 """Assigns the *cfg_obj* to the global `Configuration`. |
50 *cfg_obj* has to be a `VirtualMailManager.Config.Config` instance.""" |
40 *cfg_obj* has to be a `VirtualMailManager.Config.Config` instance.""" |
51 from VirtualMailManager.Config import Config |
41 from VirtualMailManager.Config import Config |
52 assert isinstance(cfg_obj, Config) |
42 assert isinstance(cfg_obj, Config) |
53 global Configuration |
43 global Configuration |
54 Configuration = cfg_obj |
44 Configuration = cfg_obj |
55 |
|
56 |
|
57 def get_unicode(string): |
|
58 """Converts `string` to `unicode`, if necessary.""" |
|
59 if isinstance(string, unicode): |
|
60 return string |
|
61 return unicode(string, ENCODING, 'replace') |
|
62 |
|
63 |
|
64 def expand_path(path): |
|
65 """Expands paths, starting with ``.`` or ``~``, to an absolute path.""" |
|
66 if path.startswith('.'): |
|
67 return os.path.abspath(path) |
|
68 if path.startswith('~'): |
|
69 return os.path.expanduser(path) |
|
70 return path |
|
71 |
|
72 |
|
73 def is_dir(path): |
|
74 """Checks if `path` is a directory. |
|
75 |
|
76 Throws a `VMMError` if `path` is not a directory. |
|
77 |
|
78 """ |
|
79 path = expand_path(path) |
|
80 if not os.path.isdir(path): |
|
81 raise VMMError(_(u"'%s' is not a directory") % get_unicode(path), |
|
82 NO_SUCH_DIRECTORY) |
|
83 return path |
|
84 |
|
85 |
|
86 def exec_ok(binary): |
|
87 """Checks if the `binary` exists and if it is executable. |
|
88 |
|
89 Throws a `VMMError` if the `binary` isn't a file or is not |
|
90 executable. |
|
91 |
|
92 """ |
|
93 binary = expand_path(binary) |
|
94 if not os.path.isfile(binary): |
|
95 raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), |
|
96 NO_SUCH_BINARY) |
|
97 if not os.access(binary, os.X_OK): |
|
98 raise VMMError(_(u"File is not executable: '%s'") % |
|
99 get_unicode(binary), NOT_EXECUTABLE) |
|
100 return binary |
|
101 |
|
102 |
|
103 del _ |
|