Commit ab59046e authored by André Anjos's avatar André Anjos 💬
Browse files

[python3] Syntax fixes

parent 83d4adac
......@@ -19,7 +19,7 @@ def add_clients(session, protodir, verbose):
if not s: continue #empty line
id = int(s[0])
set = s[1]
if verbose: print "Adding client %d on '%s' set..." % (id, set)
if verbose: print("Adding client %d on '%s' set..." % (id, set))
session.add(Client(id, set))
def add_real_lists(session, protodir, verbose):
......@@ -108,7 +108,7 @@ def define_protocols(session, protodir, verbose):
attack = os.path.join(protodir, 'attack-%s-allsupports-%s.txt' % (s[1], grp))
if not os.path.exists(attack):
if verbose:
print "Not considering protocol %s as attack list '%s' was not found" % (s[1], attack)
print("Not considering protocol %s as attack list '%s' was not found" % (s[1], attack))
consider = False
# check real file
......@@ -117,7 +117,7 @@ def define_protocols(session, protodir, verbose):
alt_real = os.path.join(protodir, 'real-%s.txt' % (grp,))
if not os.path.exists(alt_real):
if verbose:
print "Not considering protocol %s as real list '%s' or '%s' were not found" % (s[1], real, alt_real)
print("Not considering protocol %s as real list '%s' or '%s' were not found" % (s[1], real, alt_real))
consider = False
else:
real = alt_real
......@@ -126,13 +126,13 @@ def define_protocols(session, protodir, verbose):
if consider: valid[s[1]] = files
for protocol, groups in valid.iteritems():
if verbose: print "Creating protocol '%s'..." % protocol
for protocol, groups in valid.items():
if verbose: print("Creating protocol '%s'..." % protocol)
# create protocol on the protocol table
obj = Protocol(name=protocol)
for grp, flist in groups.iteritems():
for grp, flist in groups.items():
counter = 0
for fname in open(flist[0], 'rt'):
......@@ -140,7 +140,7 @@ def define_protocols(session, protodir, verbose):
q = session.query(Attack).join(File).filter(File.path == s).one()
q.protocols.append(obj)
counter += 1
if verbose: print " -> %5s/%-6s: %d files" % (grp, "attack", counter)
if verbose: print(" -> %5s/%-6s: %d files" % (grp, "attack", counter))
counter = 0
for fname in open(flist[1], 'rt'):
......@@ -148,7 +148,7 @@ def define_protocols(session, protodir, verbose):
q = session.query(RealAccess).join(File).filter(File.path == s).one()
q.protocols.append(obj)
counter += 1
if verbose: print " -> %5s/%-6s: %d files" % (grp, "real", counter)
if verbose: print(" -> %5s/%-6s: %d files" % (grp, "real", counter))
session.add(obj)
......@@ -175,7 +175,7 @@ def create(args):
if args.recreate:
if args.verbose and os.path.exists(dbfile):
print('unlinking %s...' % dbfile)
print(('unlinking %s...' % dbfile))
if os.path.exists(dbfile): os.unlink(dbfile)
if not os.path.exists(os.path.dirname(dbfile)):
......
......@@ -155,13 +155,13 @@ class File(Base):
def get_realaccess(self):
"""Returns the real-access object equivalent to this file or raise"""
if len(self.realaccess) == 0:
raise RuntimeError, "%s is not a real-access" % self
raise RuntimeError("%s is not a real-access" % self)
return self.realaccess[0]
def get_attack(self):
"""Returns the attack object equivalent to this file or raise"""
if len(self.attack) == 0:
raise RuntimeError, "%s is not an attack" % self
raise RuntimeError("%s is not an attack" % self)
return self.attack[0]
def load(self, directory=None, extension='.hdf5'):
......
......@@ -49,7 +49,7 @@ class Database(object):
"""Raise a RuntimeError if the database backend is not available"""
if not self.is_valid():
raise RuntimeError, "Database '%s' cannot be found at expected location '%s'. Create it and then try re-connecting using Database.connect()" % (INFO.name(), SQLITE_FILE)
raise RuntimeError("Database '%s' cannot be found at expected location '%s'. Create it and then try re-connecting using Database.connect()" % (INFO.name(), SQLITE_FILE))
def objects(self, support=Attack.attack_support_choices,
protocol='grandtest', groups=Client.set_choices, cls=('attack', 'real'),
......@@ -101,7 +101,7 @@ class Database(object):
return check_validity((l,), obj, valid, default)
for k in l:
if k not in valid:
raise RuntimeError, 'Invalid %s "%s". Valid values are %s, or lists/tuples of those' % (obj, k, valid)
raise RuntimeError('Invalid %s "%s". Valid values are %s, or lists/tuples of those' % (obj, k, valid))
return l
# check if groups set are valid
......@@ -120,8 +120,8 @@ class Database(object):
if not protocol: protocol = 'grandtest' #default
VALID_PROTOCOLS = [k.name for k in self.protocols()]
if protocol not in VALID_PROTOCOLS:
raise RuntimeError, 'Invalid protocol "%s". Valid values are %s' % \
(protocol, VALID_PROTOCOLS)
raise RuntimeError('Invalid protocol "%s". Valid values are %s' % \
(protocol, VALID_PROTOCOLS))
# checks client identity validity
VALID_CLIENTS = [k.id for k in self.clients()]
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Tiago de Freitas Pereira <tiagofrepereira@gmail.com>
# Tue 01 Oct 2012 16:48:44 CEST
# Tue 01 Oct 2012 16:48:44 CEST
"""Replay attack database implementation as antispoofing.utils.db.Database"""
......@@ -78,7 +78,7 @@ class Database(DatabaseBase):
## remove '.. ' lines from rst
desc = '\n'.join([k for k in self.long_description().split('\n') if k.strip().find('.. ') != 0])
p = subparser.add_parser(entry_point_name,
p = subparser.add_parser(entry_point_name,
help=self.short_description(),
description=desc,
formatter_class=RawDescriptionHelpFormatter)
......@@ -100,7 +100,7 @@ class Database(DatabaseBase):
p.set_defaults(name=entry_point_name)
p.set_defaults(cls=Database)
return
create_subparser.__doc__ = DatabaseBase.create_subparser.__doc__
......@@ -113,11 +113,11 @@ class Database(DatabaseBase):
from .driver import Interface
i = Interface()
return i.version()
def short_description(self):
return "Anti-Spoofing database with 1300 videos produced at Idiap, Switzerland"
short_description.__doc__ = DatabaseBase.short_description.__doc__
def long_description(self):
return Database.__doc__
long_description.__doc__ = DatabaseBase.long_description.__doc__
......@@ -127,12 +127,12 @@ class Database(DatabaseBase):
return 'video' in propname
elif propname is None:
return True
elif isinstance(propname, (str,unicode)):
elif isinstance(propname, str):
return 'video' == propname
# does not implement the given access protocol
return False
def get_data(self, group):
"""Returns either all objects or objects for a specific group"""
real = dict(self.__kwargs)
......@@ -158,7 +158,7 @@ class Database(DatabaseBase):
def get_test_filters(self):
return ('device', 'support', 'light')
def get_filtered_test_data(self, filter):
def device_filter(obj, filter):
......@@ -189,9 +189,8 @@ class Database(DatabaseBase):
'adverse': (real, [k for k in attack if light_filter(k, 'adverse')]),
}
raise RuntimeError, \
"filter parameter should specify a valid filter among `%s'" % \
self.get_test_filters()
raise RuntimeError("filter parameter should specify a valid filter among `%s'" % \
self.get_test_filters())
def get_all_data(self):
return self.get_data(None)
......
......@@ -4,16 +4,16 @@
# Mon Aug 8 12:40:24 2011 +0200
#
# Copyright (C) 2011-2012 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/>.
......@@ -25,6 +25,12 @@ import unittest
from .query import Database
from .models import *
authenticate_str = 'authenticate'
if sys.version_info[0] < 3: authenticate_str = authenticate_str.encode('utf8')
enroll_str = 'enroll'
if sys.version_info[0] < 3: enroll_str = enroll_str.encode('utf8')
class ReplayDatabaseTest(unittest.TestCase):
"""Performs various tests on the replay attack database."""
......@@ -36,8 +42,8 @@ class ReplayDatabaseTest(unittest.TestCase):
for v in f[:10]: #only the 10 first...
self.assertTrue(isinstance(v.get_realaccess(), RealAccess))
o = v.get_realaccess()
self.assertEqual(o.purpose, u'authenticate')
self.assertEqual(o.purpose, authenticate_str)
train = db.objects(cls='real', groups='train')
self.assertEqual(len(train), 60)
......@@ -77,34 +83,34 @@ class ReplayDatabaseTest(unittest.TestCase):
def test02_queryAttacks(self):
self.queryAttackType('grandtest', 1000)
def test03_queryPrintAttacks(self):
self.queryAttackType('print', 200)
def test04_queryMobileAttacks(self):
self.queryAttackType('mobile', 400)
def test05_queryHighDefAttacks(self):
self.queryAttackType('highdef', 400)
def test06_queryPhotoAttacks(self):
self.queryAttackType('photo', 600)
def test07_queryVideoAttacks(self):
self.queryAttackType('video', 400)
def test08_queryEnrollments(self):
db = Database()
f = db.objects(cls='enroll')
self.assertEqual(len(f), 100) #50 clients, 2 conditions
for v in f:
self.assertEqual(v.get_realaccess().purpose, u'enroll')
self.assertEqual(v.get_realaccess().purpose, enroll_str)
def test09_queryClients(self):
......@@ -139,13 +145,13 @@ class ReplayDatabaseTest(unittest.TestCase):
self.assertEqual(main('replay dumplist --self-test'.split()), 0)
def test13_manage_dumplist_2(self):
from bob.db.script.dbmanage import main
self.assertEqual(main('replay dumplist --class=attack --group=devel --support=hand --protocol=highdef --self-test'.split()), 0)
def test14_manage_dumplist_client(self):
from bob.db.script.dbmanage import main
self.assertEqual(main('replay dumplist --client=117 --self-test'.split()), 0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment