Driver selection

Stuart D. Gathman stuart at bmsi.com
Sat Dec 9 11:15:02 EST 2006


On Fri, 08 Dec 2006 21:35:41 -0800, Gabriel Genellina wrote:

> On 9 dic, 00:53, "Stuart D. Gathman" <stu... at bmsi.com> wrote:

>> Or you can modify the source to "from drivermodule import DNSLookup".
>>
>> What is the friendliest way to make this configurable?  Currently, users
>> are modifying the source to supply the desired driver.  Yuck.  I would
>> like to supply several drivers, and have a simple way to select one at
>> installation or run time.
> 
> You can store the user's choice in a configuration file (like the ones
> supported by ConfigParser).
> Then you can put all the logic to select and import the right function
> in a separate module, and export the DNSLookup name; make all the other
> parts of the application say "from mymodule import DNSLookup"

pyspf is a library, not an application.  It would be nasty to make it have
a config file.  We already have "from pydns_driver import DNSLookup" in
the pyspf module.  If only users could import *for* a module from
*outside* the module.  I suppose you can do something like this:

app.py:

import spf
# select dnspython driver for spf module
from spf.dnspython_driver import DNSLookup
spf.DNSLookup = DNSLookup
del DNSLookup

...

That is ugly.  I'm looking for better ideas.  Is there a clean way to make
a setdriver function?  Used like this, say:

app.py:

import spf
spf.set_driver('dnspython')
...

Can a function replace itself?  For instance, in spf.py, could DNSLookup
do this to provide a default:

def set_driver(d):
  if d == 'pydns':
    from pydns_driver import DNSLookup
  elif d == 'dnspython':
    from dnspython_driver import DNSLookup
  else: raise Exception('Unknown DNS driver')

def DNSLookup(name,t):
  from pydns_driver import DNSLookup
  return DNSLookup(name,t)

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Python-list mailing list