comparison bindings/python/pyadb.py @ 625:448b28a598e3

added the first pass of the native python access class, open, close and insert supported, query still to come. Also, on account of a definite memory leak (yet to be sorted out) when using the modulw in the interpreter, sometimes exit() will segfault...
author map01bf
date Tue, 22 Sep 2009 12:35:24 +0000
parents
children 356d7b319ae8
comparison
equal deleted inserted replaced
624:afa05407ce41 625:448b28a598e3
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 pyadb.py
5
6 public access and class structure for python audioDb api bindings.
7
8 Created by Benjamin Fields on 2009-09-22.
9 Copyright (c) 2009 Goldsmith University of London.
10 """
11
12 import sys
13 import os, os.path
14 import unittest
15 import _pyadb
16
17 ADB_HEADER_FLAG_L2NORM = 0x1#annoyingly I can't find a means
18 ADB_HEADER_FLAG_POWER = 0x4#around defining these flag definitions
19 ADB_HEADER_FLAG_TIMES = 0x20#as they aren't even exported to the
20 ADB_HEADER_FLAG_REFERENCES = 0x40#api, so this is the only way to get them.
21
22 class Usage(Exception):
23 def __init__(self, msg):
24 self.msg = msg
25
26 class Pyadb:
27
28 def __init__(self, path, mode='w'):
29 self.path = path
30 if not (mode=='w' or mode =='r'):
31 raise(ValueError, "if specified, mode must be either\'r\' or \'w\'.")
32 if os.path.exists(path):
33 self._db = _pyadb._pyadb_open(path, mode)
34 else:
35 self._db = _pyadb._pyadb_create(path,0,0,0)
36 self._updateDBAttributes()
37 return
38
39 def insert(self, featFile=None, powerFile=None, timesFile=None, featData=None, powerData=None, timesData=None, key=None):
40 """Insert features into database. Can be done with data provided directly or by giving a path to a binary fftExtract style feature file. If power and/or timing is engaged in the database header, it must be provided (via the same means as the feature) or a Usage exception will be raised. Power files should be of the same binary type as features. Times files should be the ascii number length of time in seconds from the begining of the file to segment start, one per line.\n---Note that direct data insertion is not yet implemented.---"""
41 #While python normally advocates leaping before looking, these check are nessecary as
42 #it is very difficult to assertain why the insertion failed once it has been called.
43 if (self.hasPower and (((featFile) and powerFile==None) or ((featData) and powerData==None))):
44 raise(Usage, "The db you are attempting an insert on (%s) expects power and you either\
45 haven't provided any or have done so in the wrong format."%self.path)
46 if (self.hasTimes and (((timesFile) and timesFile==None) or ((timesData) and timesData==None))):
47 raise(Usage, "The db you are attempting an insert on (%s) expects times and you either\
48 haven't provided any or have done so in the wrong format."%self.path)
49 args = {"db":self._db}
50 if featFile:
51 args["features"] = featFile
52 elif featData:
53 args["features"] = featData
54 else:
55 raise(Usage, "Must provide some feature data!")
56 if self.hasPower:
57 if featFile:
58 args["power"]=powerFile
59 elif featData:
60 pass
61 if self.hasTimes:
62 if featFile:
63 args["times"]=timesFile
64 elif timesData:
65 pass
66 if key:
67 args["key"]=str(key)
68 if featFile:
69 if not _pyadb._pyadb_insertFromFile(**args):
70 raise(RuntimeError, "Insertion failed for an unknown reason.")
71 else:
72 self._updateDBAttributes()
73 return
74 elif featData:
75 raise(NotImplementedError, "direct data insertion not yet implemented")
76
77
78 ###internal methods###
79 def _updateDBAttributes(self):
80 '''run _pyadb_status to fill/update the database level flags and info'''
81 rawFlags = long(0)
82 (self.numFiles,
83 self.dims,
84 self.dudCount,
85 self.nullCount,
86 rawFlags,
87 self.length,
88 self.data_region_size) = _pyadb._pyadb_status(self._db)
89 self.l2Normed = bool(rawFlags & ADB_HEADER_FLAG_L2NORM)
90 self.hasPower = bool(rawFlags & ADB_HEADER_FLAG_POWER)
91 self.hasTimes = bool(rawFlags & ADB_HEADER_FLAG_TIMES)
92 self.usesRefs = bool(rawFlags & ADB_HEADER_FLAG_REFERENCES)
93 return
94
95
96
97
98 class Result:
99 def __init__(self):
100 pass
101
102 class untitledTests(unittest.TestCase):
103 def setUp(self):
104 pass
105
106
107 if __name__ == '__main__':
108 unittest.main()