Print

Print


Hi Jacek,

I'm not sure that we should support all possible use cases.
That method is a convenience method and it's not that complicated,
it should be possible to implement something similar in client
code which does something non-conventional. Still if you want to 
do it I would not use URL instance to pass overrides to the method,
URL class is not designed to provide only individual pieces, it's
supposed to be complete URL. I'd probably just pass a dictionary
with keywords:

def getEngineFromFile(fileName, overrides={}):
    ....

getEngineFromFile("myFile.ini", overrides=dict(database="mydb"))


or even better specify overrides as regular keyword parameters:

def getEngineFromFile(fileName,
                      drivername=None,
                      username=None,
                      password=None,
                      host=None,
                      port=None,
                      database=None,
                      query=None):
    ...


getEngineFromFile("myFile.ini", database="mydb")

Cheers,
Andy


Becla, Jacek wrote on 2015-09-17:
> imgserv has a new usecase, John is reading config file
> (host/port/user/password), and adds database name.
> While we could probably tweak imgserv to avoid that
> (because he is only working with one database at the
> moment), I do think it is a generic use case: there is
> nothing wrong with defining host/port/credentials in
> a file, and connecting to different databases using
> the file + db name, right?
> 
> Our API does not support it now, and I want to avoid
> disassembling / assembling URLs.
> 
> We now have
> 
> https://github.com/lsst/db/blob/tickets/DM-2299/python/lsst/db/engineFactory.py
> 
> How about tweaking getEngineFromFile as follows (pseudo, untested code):
> 
> def getEngineFromFile(fileName, urlExtras):
>      if fileName.startswith('~'):	
>          fileName = os.path.expanduser(fileName)
>      parser = ConfigParser()
>      parser.readfp(open(fileName), fileName)
>      try:
>          options = dict(parser.items("database"))
>      except NoSectionError:
>          log.error("File %s does not contain section 'database'" % fileName)
>          raise
>      if urlExtras:
>          url = URL.make_url(options['url'])
>          if urlExtras.host:
>              url.host = urlExtras.host
>          if urlExtras.port:
>              url.port = urlExtras.port
>          if urlExtras.user:
>              url.user = urlExtras.user
>          if urlExtras.password:
>              url.password = urlExtras.password
>          if urlExtras.database:
>              url.database = urlExtras.database
>          options.url = url
>      return sqlalchemy.create_engine(options, "")
> and then we could just call:
> 
> getEngineFromFile(
>      "myFile.ini",
>      URL(drivername="mysql+mysqldb", database = "mydb"))
> (BTW, I don't like that we have to specify driver name here)
> 
> (docs for URL:
> https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/engine/ur
> l .py)
> 
> Thoughts?
> 
> Jacek
  


########################################################################
Use REPLY-ALL to reply to list

To unsubscribe from the QSERV-L list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=QSERV-L&A=1