driver.py 6.93 KB
Newer Older
Pedro TOME's avatar
Pedro TOME committed
1
2
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
3
# Pedro Tome <Pedro.Tome@idiap.ch>
Pedro TOME's avatar
Pedro TOME committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Laurent El Shafey <laurent.el-shafey@idiap.ch>
#
# Copyright (C) 2014 Idiap Research Institute, Martigny, Switzerland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Bob Database Driver entry-point for the UTFVP
"""

import os
import sys
Pedro TOME's avatar
Pedro TOME committed
25
from bob.db.base.driver import Interface as BaseInterface
Pedro TOME's avatar
Pedro TOME committed
26
27
28
29
30
31
32
33


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

  from .query import Database
  db = Database()

34
35
36
37
  model_ids = None
  if args.models is not None:
    if isinstance(args.models, (list, tuple)): model_ids = args.models
    else: model_ids = (args.models,)
Pedro TOME's avatar
Pedro TOME committed
38
39
40
  r = db.objects(
      protocol=args.protocol,
      purposes=args.purpose,
41
      model_ids=model_ids,
Pedro TOME's avatar
Pedro TOME committed
42
43
44
45
46
47
      groups=args.group,
      classes=args.sclass
  )

  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
48
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
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
    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
77
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    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


def reverse(args):
  """Returns a list of file database identifiers given the path stems"""

  from .query import Database
  db = Database()

  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
97
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    output = null()

  r = db.reverse(args.path)
  for f in r: output.write('%d\n' % f.id)

  if not r: return 1

  return 0


def path(args):
  """Returns a list of fully formed paths or stems given some file id"""

  from .query import Database
  db = Database()

  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
116
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    output = null()

  r = db.paths(args.id, prefix=args.directory, suffix=args.extension)
  for path in r: output.write('%s\n' % path)

  if not r: return 1

  return 0


class Interface(BaseInterface):

  def name(self):
    return 'utfvp'

  def version(self):
    import pkg_resources  # part of setuptools
Pedro TOME's avatar
Pedro TOME committed
134
    return pkg_resources.require('bob.db.%s' % self.name())[0].version
Pedro TOME's avatar
Pedro TOME committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

  def files(self):

    from pkg_resources import resource_filename
    raw_files = ('db.sql3',)
    return [resource_filename(__name__, k) for k in raw_files]

  def type(self):
    return 'sqlite'

  def add_commands(self, parser):

    from . import __doc__ as docs

    subparsers = self.setup_parser(parser,
        "UTFVP database", docs)

    # 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 ())
    parser.add_argument('-m', '--models', type=str, help="if given, limits the dump to a particular model", choices=db.model_ids() if db.is_valid() else ())
    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.")
    parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=checkfiles) #action

    # adds the "reverse" command
    parser = subparsers.add_parser('reverse', help=reverse.__doc__)
    parser.add_argument('path', nargs='+', type=str, help="one or more path stems to look up. If you provide more than one, files which cannot be reversed will be omitted from the output.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=reverse) #action

    # adds the "path" command
    parser = subparsers.add_parser('path', help=path.__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('id', nargs='+', type=int, help="one or more file ids to look up. If you provide more than one, files which cannot be found will be omitted from the output. If you provide a single id to lookup, an error message will be printed if the id does not exist in the database. The exit status will be non-zero in such case.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=path) #action