12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- """
- Factory to create Database instances
- A Generic interface for all DB engines.
- Usage:
- import pwman.data.factory as DBFactory
- db = DBFactory.create(params)
- db.open()
- .....
- """
- from pwman.data.database import DatabaseException
- from pwman.data.drivers import sqlite
- class FactoryException(Exception):
- def __init__(self, message):
- self.message
- def __str__(self):
- return self.message
- def check_db_version(ftype, filename):
- if ftype == "SQLite":
- ver = sqlite.SQLiteDatabaseNewForm.check_db_version(filename)
- try:
- return float(ver.strip("\'"))
- except ValueError:
- return 0.3
-
- def create(dbtype, version=None, filename=None):
- """
- create(params) -> Database
- Create a Database instance.
- 'type' can only be 'SQLite' at the moment
- """
- if dbtype == "SQLite":
- from pwman.data.drivers import sqlite
- db = sqlite.SQLiteDatabaseNewForm(filename)
- elif dbtype == "Postgresql":
- try:
- from pwman.data.drivers import postgresql
- db = postgresql.PostgresqlDatabase()
- except ImportError:
- raise DatabaseException("python-pygresql not installed")
- elif dbtype == "MySQL":
- try:
- from pwman.data.drivers import mysql
- db = mysql.MySQLDatabase()
- except ImportError:
- raise DatabaseException("python-mysqldb not installed")
- else:
- raise DatabaseException("Unknown database type specified")
- return db
|