/usr/bin/dbaexport is in dballe 7.7-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #!/usr/bin/python
# coding: utf-8
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
import argparse
import logging
import os
import sys
import io
try:
import dballe
HAS_DBALLE = True
except ImportError:
HAS_DBALLE = False
log = logging.getLogger("main")
class CommandError(Exception):
pass
def setup_logging(args):
FORMAT = "%(levelname)s: %(message)s"
if args.verbose:
logging.basicConfig(level=logging.INFO, stream=sys.stderr, format=FORMAT)
else:
logging.basicConfig(level=logging.WARNING, stream=sys.stderr, format=FORMAT)
def get_db(args):
if not args.dsn:
dsn = os.environ.get("DBA_DB", "")
else:
dsn = args.dsn
if not dsn:
raise CommandError("Cannot find a database to connect to: --dsn is not specified and $DBA_DB is not set")
elif dballe.DB.is_url(dsn):
return dballe.DB.connect_from_url(dsn)
else:
return dballe.DB.connect(dsn, args.user, args.password)
def get_query(args):
query = dballe.Record()
for q in args.query:
query.set_from_string(q)
return query
def get_outfd(args, text=False):
"""
Get the output file descriptor
"""
if args.outfile:
if text:
return io.open(args.outfile, "wt", encoding="utf-8")
else:
return io.open(args.outfile, "wb")
else:
if text:
return io.open(sys.stdout.fileno(), "wt", encoding="utf-8", closefd=False)
else:
return io.open(sys.stdout.fileno(), "wb", closefd=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Export data from a DB-All.e database.')
parser.add_argument("--verbose", action="store_true",
help="verbose output")
parser.add_argument("--dsn", type=str, metavar="dsn",
help="DSN, or URL-like database definition, to use for connecting to the DB-All.e database (can also be specified in the environment as DBA_DB)")
parser.add_argument("--user", type=str, metavar="name",
help="username to use for connecting to the DB-All.e database")
parser.add_argument("--pass", type=str, metavar="password", dest="password",
help="password to use for connecting to the DB-All.e database")
parser.add_argument("--outfile", "-o", type=str, metavar="file",
help="output file. Default is standard output, if supported")
subparsers = parser.add_subparsers(dest="format", help="output formats")
# Use a byte-string for subparser name to avoid seeing u'csv' in output.
# Remove in python3
parser_csv = subparsers.add_parser(b"csv", help="export data as CSV")
parser_csv.add_argument("query", nargs="*", metavar="key=val",
help="DB-All.e query to select data to export")
parser_gnur = subparsers.add_parser(b"gnur", help="export data as GNU R workspace")
parser_gnur.add_argument("query", nargs="*", metavar="key=val",
help="DB-All.e query to select data to export")
parser_bufr = subparsers.add_parser(b"bufr", help="export data as BUFR")
parser_bufr.add_argument("query", nargs="*", metavar="key=val",
help="DB-All.e query to select data to export")
parser_bufr.add_argument("--generic", action="store_true",
help="export generic BUFR messages")
parser_crex = subparsers.add_parser(b"crex", help="export data as CREX")
parser_crex.add_argument("query", nargs="*", metavar="key=val",
help="DB-All.e query to select data to export")
parser_crex.add_argument("--generic", action="store_true",
help="export generic BUFR messages")
args = parser.parse_args()
try:
setup_logging(args)
if not HAS_DBALLE:
raise CommandError("This command requires the dballe python module to run")
db = get_db(args)
query = get_query(args)
if args.format == "csv":
import dballe.dbacsv
with get_outfd(args, text=True) as fd:
dballe.dbacsv.export(db, query, fd)
elif args.format == "gnur":
if not args.outfile:
raise CommandError("cannot export R data to standard output: please use --outfile")
import dballe.rconvert
import dballe.volnd
idx = (dballe.volnd.AnaIndex(), \
dballe.volnd.DateTimeIndex(), \
dballe.volnd.LevelIndex(), \
dballe.volnd.TimeRangeIndex(), \
dballe.volnd.NetworkIndex())
res = dballe.volnd.read(db.query_data(query), idx)
dballe.rconvert.volnd_save_to_r(res, args.outfile)
elif args.format == "bufr":
if not args.outfile: raise CommandError("BUFR output requires --outfile")
db.export_to_file(query, "BUFR", args.outfile, generic=args.generic)
elif args.format == "crex":
if not args.outfile: raise CommandError("CREX output requires --outfile")
db.export_to_file(query, "CREX", args.outfile, generic=args.generic)
else:
raise CommandError("Exporter for {} is not available.".format(args.format))
except CommandError as e:
log.error("%s", e)
sys.exit(1)
|