Browse Source

Remove the seqvault.rc configuration file.

Make the seqvault program use the same configuration file as the
default DatabaseProvider object.

Also remove the possibility to specify server settings on the command
line. This effectively makes the presence of a configuration file
mandatory.
master
Damien Goutte-Gattat 5 months ago
parent
commit
b3d14b925f
  1. 2
      README.md
  2. 71
      incenp/bio/seq/seqvault.py

2
README.md

@ -43,7 +43,7 @@ Initialize the newly created database by running the provided
$ psql -h localhost -U <username> <dbname> < biosql/biosqldb-pg.sql
Next, create a plaintext file in `$XDG_CONFIG_HOME/bioutils/seqvault.rc`
Next, create a plaintext file in `$XDG_CONFIG_HOME/bioutils/databases.ini`
and fill it as follows::
[Server]

71
incenp/bio/seq/seqvault.py

@ -17,7 +17,7 @@
"""A tool to access a BioSQL-based sequence database."""
from configparser import ConfigParser
from configparser import ConfigParser, NoOptionError
from hashlib import md5
import os.path
from subprocess import run
@ -41,7 +41,7 @@ This program is released under the GNU General Public License.
See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.
"""
default_config = '{}/seqvault.rc'.format(click.get_app_dir('bioutils'))
default_config = '{}/databases.ini'.format(click.get_app_dir('bioutils'))
def _get_database(ctx, param, value):
@ -76,42 +76,55 @@ def _check_config_file(ctx, param, value):
@click.option('--config', '-c', type=click.Path(), default=default_config,
callback=_check_config_file,
help="Path to the configuration file.")
@click.option('--driver', metavar="DRIVER",
help="Specify the database driver.")
@click.option('--host', '-H', metavar="HOST",
help="Specify the database host.")
@click.option('--user', '-u', metavar="USER",
help="Specify the database user name.")
@click.option('--name', '-n', metavar="NAME",
help="Specify the database name.")
@click.option('--server', '-s', metavar="SERVER",
help="""The server section to use in the configuration
file (defaults to the first section).""")
@click.version_option(version=__version__, message=prog_notice)
@click.pass_context
def seqvault(ctx, config, driver, host, user, name):
def seqvault(ctx, config, server):
"""Access a BioSQL sequence database."""
cfg = ConfigParser()
cfg.add_section('Server')
cfg.set('Server', 'driver', 'psycopg2')
cfg.set('Server', 'host', 'localhost')
cfg.set('Server', 'user', 'seqvault')
cfg.set('Server', 'database', 'seqvault')
cfg.read(config)
try:
cfg.read(config)
except Exception as e:
raise click.ClickException(f"Cannot read configuration file: {e}")
if server and not cfg.has_section(server):
raise click.ClickException(f"No server {server!r} in "
"configuration file")
if not server:
sections = cfg.sections()
if len(sections) == 0:
raise click.ClickException("No default server configured")
server = sections[0]
if driver:
cfg.set('Server', 'driver', driver)
if host:
cfg.set('Server', 'host', host)
if user:
cfg.set('Server', 'user', user)
if name:
cfg.set('Server', 'database', name)
if cfg.get(server, 'type', fallback=None) != 'biosql':
raise click.ClickException(f"Server {server!r} is not a BioSQL "
"server")
if cfg.has_option(server, 'server'):
server = cfg.get(server, 'server')
if not cfg.has_section(server):
raise click.ClickException(f"Missing referred server "
f"{server!r}")
conn_settings = {}
try:
driver = cfg.get(server, 'driver')
conn_settings['driver'] = driver
if driver == 'sqlite3':
conn_settings['database'] = cfg.get(server, 'database')
else:
conn_settings['host'] = cfg.get(server, 'host')
conn_settings['user'] = cfg.get(server, 'user')
conn_settings['database'] = cfg.get(server, 'database')
conn_settings['password'] = cfg.get(server, 'password',
fallback=None)
except NoOptionError:
raise click.ClickException(f"Incomplete configuration for {server!r}")
try:
server = open_database(**dict(cfg.items('Server')))
server = open_database(**conn_settings)
except Exception as e:
raise click.ClickException(f"Cannot connect to server: {e}")

Loading…
Cancel
Save