driver.py 4.12 KB
Newer Older
Pedro TOME's avatar
Pedro TOME committed
1 2 3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

4
"""Bob Database Driver entry-point for the UTFVP Fingervein database
Pedro TOME's avatar
Pedro TOME committed
5 6 7 8
"""

import os
import sys
9
import pkg_resources
Pedro TOME's avatar
Pedro TOME committed
10
from bob.db.base.driver import Interface as BaseInterface
Pedro TOME's avatar
Pedro TOME committed
11 12 13 14 15 16 17 18 19 20 21


def dumplist(args):
  """Dumps lists of files based on your criteria"""

  from .query import Database
  db = Database()

  r = db.objects(
      protocol=args.protocol,
      groups=args.group,
22 23 24
      purposes=args.purpose,
      model_ids=args.models,
      )
Pedro TOME's avatar
Pedro TOME committed
25 26 27

  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
28
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
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
    output = null()

  for f in r:
    output.write('%s\n' % (f.make_path(args.directory, args.extension),))

  return 0


def checkfiles(args):
  """Checks existence of files based on your criteria"""

  from .query import Database
  db = Database()

  r = db.objects()

  # go through all files, check if they are available on the filesystem
  good = []
  bad = []
  for f in r:
    if os.path.exists(f.make_path(args.directory, args.extension)):
      good.append(f)
    else:
      bad.append(f)

  # report
  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
57
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
58 59 60 61 62 63 64 65 66 67 68 69 70
    output = null()

  if bad:
    for f in bad:
      output.write('Cannot find file "%s"\n' % (f.make_path(args.directory, args.extension),))
    output.write('%d files (out of %d) were not found at "%s"\n' % \
      (len(bad), len(r), args.directory))

  return 0


class Interface(BaseInterface):

71

Pedro TOME's avatar
Pedro TOME committed
72 73 74
  def name(self):
    return 'utfvp'

75

Pedro TOME's avatar
Pedro TOME committed
76
  def version(self):
Pedro TOME's avatar
Pedro TOME committed
77
    return pkg_resources.require('bob.db.%s' % self.name())[0].version
Pedro TOME's avatar
Pedro TOME committed
78

79

Pedro TOME's avatar
Pedro TOME committed
80
  def files(self):
81 82 83 84 85
    basedir = pkg_resources.resource_filename(__name__, '')
    filelist = os.path.join(basedir, 'files.txt')
    with open(filelist, 'rt') as f:
      return [os.path.join(basedir, k.strip()) for k in \
          f.readlines() if k.strip()]
Pedro TOME's avatar
Pedro TOME committed
86 87 88 89 90


  def type(self):
    return 'sqlite'

91

Pedro TOME's avatar
Pedro TOME committed
92 93 94 95 96
  def add_commands(self, parser):

    from . import __doc__ as docs

    subparsers = self.setup_parser(parser,
97
        "UTFVP Fingervein database", docs)
Pedro TOME's avatar
Pedro TOME committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    # example: get the "create" action from a submodule
    from .create import add_command as create_command
    create_command(subparsers)

    # example: get the "dumplist" action from a submodule
    from .query import Database
    import argparse
    db = Database()

    parser = subparsers.add_parser('dumplist', help=dumplist.__doc__)
    parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
    parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
    parser.add_argument('-p', '--protocol', help="if given, limits the dump to a particular subset of the data that corresponds to the given protocol.", choices=db.protocol_names() if db.is_valid() else ())
    parser.add_argument('-u', '--purpose', help="if given, this value will limit the output files to those designed for the given purposes.", choices=db.purposes() if db.is_valid() else ())
113
    parser.add_argument('-m', '--models', type=str, help="if given, limits the dump to a particular model")
Pedro TOME's avatar
Pedro TOME committed
114 115 116 117 118 119 120 121
    parser.add_argument('-g', '--group', help="if given, this value will limit the output files to those belonging to a particular protocolar group.", choices=db.groups() if db.is_valid() else ())
    parser.add_argument('-c', '--class', dest='sclass', help="if given, this value will limit the output files to those belonging to the given classes.", choices=('client', 'impostor'))
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=dumplist) #action

    # the "checkfiles" action
    parser = subparsers.add_parser('checkfiles', help=checkfiles.__doc__)
    parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
122
    parser.add_argument('-e', '--extension', default='.png', help="if given, this extension will be appended to every entry returned.")
Pedro TOME's avatar
Pedro TOME committed
123 124
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=checkfiles) #action