Changeset 3009


Ignore:
Timestamp:
Sep 8, 2010 10:32:18 PM (9 years ago)
Author:
wehart
Message:

Setting up plugins for database interfaces.

Location:
coopr.pyomo/trunk/coopr/pyomo/data
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/data/__init__.py

    r2304 r3009  
    1414import parse_datacmds
    1515import csv_table
     16import odbc_table
    1617#import db
  • coopr.pyomo/trunk/coopr/pyomo/data/odbc_table.py

    r3008 r3009  
    1313from pyutilib.component.core import alias
    1414
     15# format=
     16# using=
     17# query=
     18# user=
     19# password=
    1520
    16 class odbcTable(TableData):
    17 
    18     alias("odbc", "Manage IO with an ODBC interface.")
     21class db_Table(TableData):
    1922
    2023    def __init__(self):
     
    2225
    2326    def open(self):
    24         # WEH - Do we specify a filename?  If not, we should use self.options.dburl
    2527        if self.filename is None:
    26             raise IOError, "No filename specified"
    27         if not os.path.exists(self.filename):
    28             raise IOError, "Cannot find file '%s'" % self.filename
     28            raise IOError, "No data source name was specified"
    2929        #
    3030        # Initialize self.db
     
    3535        else:
    3636            try:
    37                 # WEH - Create a database here
    38                 #self.db = pyodbc.connect() #
    39                 pass
     37                self.db = self.connect(self.filename, self.options)
    4038            except pyutilib.ApplicationError:
    4139                raise
     
    6866            del self.db
    6967
     68    def connect(self, dsn, options):
     69        try:
     70            mod = __import__(options.using)
     71            args = [dsn]
     72            if not options.user is None:
     73                args.append(options.user)
     74            if not options.password is None:
     75                args.append(options.password)
     76            return mod.connect(*args)
     77        except ImportError:
     78            return None
     79       
    7080
     81for module in ['pyodbc', 'pymysql']:
     82    try:
     83        __import__(module)
     84        class tmp(db_Table):
     85            alias(module, "Manage IO with a %s database interface" % module)
     86            def __init__(self):
     87                db_Table.__init__(self)
     88                self.using = module
     89
     90    except ImportError:
     91        pass
     92
  • coopr.pyomo/trunk/coopr/pyomo/data/process_data.py

    r2585 r3009  
    473473    options = Options(**cmd[1])
    474474    for key in options:
    475         if not key in ['range','filename','format','using','query']:
     475        if not key in ['range','filename','format','using','query','user','password']:
    476476            raise ValueError, "Unknown import option '%s'" % key
    477477
     
    484484    # TODO: process mapping info
    485485    #
    486     tmp = options.filename.split(".")[-1]
    487     data = DataManagerFactory(tmp)
     486    if options.using is None:
     487        tmp = options.filename.split(".")[-1]
     488        data = DataManagerFactory(tmp)
     489    else:
     490        data = DataManagerFactory(options.using)
    488491    set_name=None
    489492    param_name=None
Note: See TracChangeset for help on using the changeset viewer.