""" This module creates a DSN (Data Source Name) on Win32 only. The DSN is an MS Access database (.mdb) where we'll store all our data. The DB is accessible through ars_data, which uses ODBC. """ import os import os.path import sys import win32api import win32con _driver_name = 'Microsoft Access Driver (*.mdb)' def find_msaccess_driver(): """ Returns the path to the driver dll. Raises exception if the driver is not present. """ hKey = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\ODBC\\ODBCINST.INI\\%s" % _driver_name) (driverPath, type) = win32api.RegQueryValueEx( hKey, "Driver") return driverPath def create_msaccess_dsn(dsn, file_path): """ Creates registry key for the given DSN. The DSN will be an MS Access file located at the given path. This will NOT create the .mdb file. dsn - the data source name file_path - path to the .mdb file """ hKey = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\\ODBC\\ODBC.INI\\' + dsn) win32api.RegSetValueEx(hKey, "DBQ", 0, win32con.REG_SZ, file_path) win32api.RegSetValueEx(hKey, "Description", 0, win32con.REG_SZ, 'Arsenal Database') win32api.RegSetValueEx(hKey, "Driver", 0, win32con.REG_SZ, find_msaccess_driver()) win32api.RegSetValueEx(hKey, "FIL", 0, win32con.REG_SZ, "MS Access;") hEngKey = win32api.RegCreateKey(hKey, "Engines") hJetKey = win32api.RegCreateKey(hEngKey, "Jet") win32api.RegCloseKey(hJetKey) win32api.RegCloseKey(hEngKey) win32api.RegCloseKey(hKey) hKey = win32api.RegCreateKey( win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources') win32api.RegSetValueEx(hKey, dsn, 0, win32con.REG_SZ, _driver_name) win32api.RegCloseKey(hKey) return if __name__ == '__main__': if len(sys.argv) < 2: file = 'c:\\test.mdb' else: file = sys.argv[1] create_msaccess_dsn('test', file)