Module vtab_media
[hide private]
[frames] | no frames]

Source Code for Module vtab_media

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2007 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: vtab_media.py 4155 2007-03-17 04:07:42Z djpham $ 
  9   
 10  """ 
 11  An apsw compatible module that implements a virtual table that stores media 
 12  data as native system files. 
 13  """ 
 14  # System modules 
 15  import glob 
 16  import os 
 17  import os.path 
 18   
 19  # BitPim moduless 
 20  import database 
 21   
22 -class Media(database.ModuleBase):
23 """ 24 Module to implements a virtual table that stores media data as native system 25 files. 26 The table has 2 fiels: __rowid__ and mediadata 27 This class maintains its own rowid, cursor, and constraints parameters. 28 The data of each row is stored in a file named Fxxxxxxx, where 29 xxxxxxx is the rowid starting with 1 ie (F0000001, F0000002, etc) 30 """ 31
32 - def __init__(self, pathname):
33 """ 34 @params pathname: full path in which to stores data files 35 """ 36 super(Media, self).__init__(('data',)) 37 # make sure the path exists 38 if not os.path.exists(pathname): 39 os.makedirs(pathname) 40 self.pathname=os.path.abspath(pathname) 41 self.rowidfilename=os.path.join(self.pathname, 'rowid') 42 self.getnextrowid() 43 self.filenames=[] 44 self.cursor=None
45
46 - def getnextrowid(self):
47 """ 48 Return the next row ID from a file called rowid 49 """ 50 try: 51 id=file(self.rowidfilename, 'rt').read() 52 self.rowid=int(id) 53 except IOError: 54 # file does not exist 55 self.rowid=1 56 self.saverowid(1)
57 - def incrementrowid(self):
58 # increment the next row ID by 1 59 self.rowid+=1 60 self.saverowid(self.rowid)
61 - def saverowid(self, id):
62 file(self.rowidfilename, 'wt').write(str(id))
63 - def filenamefromid(self, id):
64 # return a full path name based on the row ID 65 return os.path.join(self.pathname, 'F%07d'%id)
66 - def idfromfilename(self, filename):
67 return int(os.path.basename(filename)[1:])
68
69 - def BestIndex(self, constraints, orderby):
70 """ 71 Provide information on how to best access this table. 72 Must be overriden by subclass. 73 @params constraints: a tuple of (column #, op) defining a constraints 74 @params orderby: a tuple of (column #, desc) defining the order by 75 @returns a tuple of up to 5 values: 76 0: aConstraingUsage: a tuple of the same size as constraints. 77 Each item is either None, argv index(int), or (argv index, omit(Bool)). 78 1: idxNum(int) 79 2: idxStr(string) 80 3: orderByConsumed(Bool) 81 4: estimatedCost(float) 82 83 Todo: store the constraints for subsequent evaluation, and tell 84 sqlite to pass the constraints parameters to Filter. 85 """ 86 pass
87 - def Filter(self, idxNum, idxStr, argv):
88 """ 89 Begin a search of a virtual table. 90 @params idxNum: int value passed by BestIndex 91 @params idxStr: string valued passed by BestIndex 92 @params argv: constraint parameters requested by BestIndex 93 @returns: None 94 95 Todo: evaluate actual constraints, will let sqlite do the orderby. 96 """ 97 self.filenames=glob.glob(os.path.join(self.pathname, 98 'F[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')) 99 if self.filenames: 100 self.cursor=0 101 else: 102 self.cursor=None
103 - def Eof(self):
104 """ 105 Determines if the current cursor points to a valid row. 106 @returns: False if valid row, True otherwise 107 """ 108 return self.cursor is None or self.cursor>=len(self.filenames)
109 - def Column(self, N):
110 """ 111 Find the value for the N-th column of the current row. 112 @params N: the N-th column 113 @returns: value of the N-th column 114 We only have 2 columns: __rowid__ and mediadata 115 """ 116 filename=self.filenames[self.cursor] 117 if N==0: 118 # __rowid__ 119 return self.idfromfilename(filename) 120 else: 121 # mediadata 122 return buffer(file(filename, 'rb').read())
123 - def Next(self):
124 """ 125 Move the cursor to the next row. 126 @returns: None 127 """ 128 self.cursor+=1
129 - def Rowid(self):
130 """ 131 Return the rowid of the current row. 132 @returns: the rowid(int) of the current row. 133 """ 134 return self.Column(0)
135 - def UpdateDeleteRow(self, rowid):
136 """ 137 Delete row rowid 138 @params rowid: rowid to delete 139 @returns: None 140 """ 141 # delete the file 142 os.remove(self.filenamefromid(rowid))
143 - def UpdateInsertRow(self, rowid, fields):
144 """ 145 Insert a new row of data into the table 146 @params rowid: if not None, use this rowid. If None, create a new rowid 147 @params fields: a tuple of the field values in the order declared in 148 Create/Connet 149 @returns: rowid of the new row. 150 """ 151 if rowid is None: 152 rowid=self.rowid 153 self.incrementrowid() 154 file(self.filenamefromid(rowid), 'wb').write(fields[1]) 155 return rowid
156 - def UpdateChangeRow(self, rowid, newrowid, fields):
157 """ 158 Change the row of the current rowid with the new rowid and new values 159 @params rowid: rowid of the current row 160 @params newrowid: new rowid 161 @params fields: a tuple of the field values in the order declared in 162 Create/Connect 163 @returns: rowid of the new row 164 """ 165 os.remove(self.filenamefromid(rowid)) 166 file(self.filenamefromid(newrowid), 'wb').write(fields[1]) 167 return newrowid
168