28 from VirtualMailManager.constants import MIN_GID, MIN_UID, \ |
26 from VirtualMailManager.constants import MIN_GID, MIN_UID, \ |
29 ACCOUNT_EXISTS, ALIAS_EXISTS, CONF_NOFILE, CONF_NOPERM, CONF_WRONGPERM, \ |
27 ACCOUNT_EXISTS, ALIAS_EXISTS, CONF_NOFILE, CONF_NOPERM, CONF_WRONGPERM, \ |
30 DATABASE_ERROR, DOMAINDIR_GROUP_MISMATCH, DOMAIN_INVALID, \ |
28 DATABASE_ERROR, DOMAINDIR_GROUP_MISMATCH, DOMAIN_INVALID, \ |
31 FOUND_DOTS_IN_PATH, INVALID_ARGUMENT, MAILDIR_PERM_MISMATCH, \ |
29 FOUND_DOTS_IN_PATH, INVALID_ARGUMENT, MAILDIR_PERM_MISMATCH, \ |
32 NOT_EXECUTABLE, NO_SUCH_ACCOUNT, NO_SUCH_ALIAS, NO_SUCH_BINARY, \ |
30 NOT_EXECUTABLE, NO_SUCH_ACCOUNT, NO_SUCH_ALIAS, NO_SUCH_BINARY, \ |
33 NO_SUCH_DIRECTORY, NO_SUCH_RELOCATED, RELOCATED_EXISTS |
31 NO_SUCH_DIRECTORY, NO_SUCH_RELOCATED, RELOCATED_EXISTS, VMM_ERROR |
34 from VirtualMailManager.domain import Domain, get_gid |
32 from VirtualMailManager.domain import Domain, get_gid |
35 from VirtualMailManager.emailaddress import EmailAddress |
33 from VirtualMailManager.emailaddress import EmailAddress |
36 from VirtualMailManager.errors import \ |
34 from VirtualMailManager.errors import \ |
37 DomainError, NotRootError, PermissionError, VMMError |
35 DomainError, NotRootError, PermissionError, VMMError |
38 from VirtualMailManager.mailbox import new as new_mailbox |
36 from VirtualMailManager.mailbox import new as new_mailbox |
141 {'cfg_file': self._cfg_fname, |
142 {'cfg_file': self._cfg_fname, |
142 'option': opt}, err.code) |
143 'option': opt}, err.code) |
143 else: |
144 else: |
144 raise |
145 raise |
145 |
146 |
146 def _db_connect(self): |
147 def _set_db_connect(self): |
|
148 """check which module to use and set self._db_connect""" |
|
149 global _db_mod |
|
150 if self._cfg.dget('database.module').lower() == 'psycopg2': |
|
151 try: |
|
152 _db_mod = __import__('psycopg2') |
|
153 except ImportError: |
|
154 raise VMMError(_(u"Unable to import database module '%s'") % |
|
155 'psycopg2', VMM_ERROR) |
|
156 self._db_connect = self._psycopg2_connect |
|
157 else: |
|
158 try: |
|
159 tmp = __import__('pyPgSQL', globals(), locals(), ['PgSQL']) |
|
160 except ImportError: |
|
161 raise VMMError(_(u"Unable to import database module '%s'") % |
|
162 'pyPgSQL', VMM_ERROR) |
|
163 _db_mod = tmp.PgSQL |
|
164 self._db_connect = self._pypgsql_connect |
|
165 |
|
166 def _pypgsql_connect(self): |
147 """Creates a pyPgSQL.PgSQL.connection instance.""" |
167 """Creates a pyPgSQL.PgSQL.connection instance.""" |
148 if self._dbh is None or (isinstance(self._dbh, PgSQL.Connection) and |
168 if self._dbh is None or (isinstance(self._dbh, _db_mod.Connection) and |
149 not self._dbh._isOpen): |
169 not self._dbh._isOpen): |
150 try: |
170 try: |
151 self._dbh = PgSQL.connect( |
171 self._dbh = _db_mod.connect( |
152 database=self._cfg.dget('database.name'), |
172 database=self._cfg.dget('database.name'), |
153 user=self._cfg.pget('database.user'), |
173 user=self._cfg.pget('database.user'), |
154 host=self._cfg.dget('database.host'), |
174 host=self._cfg.dget('database.host'), |
|
175 port=self._cfg.dget('database.port'), |
155 password=self._cfg.pget('database.pass'), |
176 password=self._cfg.pget('database.pass'), |
156 client_encoding='utf8', unicode_results=True) |
177 client_encoding='utf8', unicode_results=True) |
157 dbc = self._dbh.cursor() |
178 dbc = self._dbh.cursor() |
158 dbc.execute("SET NAMES 'UTF8'") |
179 dbc.execute("SET NAMES 'UTF8'") |
159 dbc.close() |
180 dbc.close() |
160 except PgSQL.libpq.DatabaseError, err: |
181 except _db_mod.libpq.DatabaseError, err: |
|
182 raise VMMError(str(err), DATABASE_ERROR) |
|
183 |
|
184 def _psycopg2_connect(self): |
|
185 """Return a new psycopg2 connection object.""" |
|
186 if self._dbh is None or \ |
|
187 (isinstance(self._dbh, _db_mod.extensions.connection) and |
|
188 self._dbh.closed): |
|
189 try: |
|
190 self._dbh = _db_mod.connect( |
|
191 host=self._cfg.dget('database.host'), |
|
192 sslmode=self._cfg.dget('database.sslmode'), |
|
193 port=self._cfg.dget('database.port'), |
|
194 database=self._cfg.dget('database.name'), |
|
195 user=self._cfg.pget('database.user'), |
|
196 password=self._cfg.pget('database.pass')) |
|
197 self._dbh.set_client_encoding('utf8') |
|
198 _db_mod.extensions.register_type(_db_mod.extensions.UNICODE) |
|
199 dbc = self._dbh.cursor() |
|
200 dbc.execute("SET NAMES 'UTF8'") |
|
201 dbc.close() |
|
202 except _db_mod.DatabaseError, err: |
161 raise VMMError(str(err), DATABASE_ERROR) |
203 raise VMMError(str(err), DATABASE_ERROR) |
162 |
204 |
163 def _chk_other_address_types(self, address, exclude): |
205 def _chk_other_address_types(self, address, exclude): |
164 """Checks if the EmailAddress *address* is known as `TYPE_ACCOUNT`, |
206 """Checks if the EmailAddress *address* is known as `TYPE_ACCOUNT`, |
165 `TYPE_ALIAS` or `TYPE_RELOCATED`, but not as the `TYPE_*` specified |
207 `TYPE_ALIAS` or `TYPE_RELOCATED`, but not as the `TYPE_*` specified |