| 1 |
# Base mole type class definitions
|
| 2 |
# Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
|
| 3 |
# $Id: storage.py 1391 2006-08-10 14:21:00Z jeroen $
|
| 4 |
|
| 5 |
# This program is free software; you can redistribute it and/or modify
|
| 6 |
# it under the terms of the GNU General Public License as published by
|
| 7 |
# the Free Software Foundation; either version 2 of the License, or
|
| 8 |
# (at your option) any later version.
|
| 9 |
|
| 10 |
# This program is distributed in the hope that it will be useful,
|
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
# GNU General Public License for more details.
|
| 14 |
|
| 15 |
# You should have received a copy of the GNU General Public License
|
| 16 |
# along with this program; if not, write to the Free Software
|
| 17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 18 |
|
| 19 |
import sys
|
| 20 |
from Mole import storage
|
| 21 |
|
| 22 |
class Type:
|
| 23 |
"""Mole type baseclass"""
|
| 24 |
|
| 25 |
def __init__(this, id, conf):
|
| 26 |
this.id = id
|
| 27 |
this.storage = None
|
| 28 |
this.storageType = storage._storageTypes[conf["StorageType"]]
|
| 29 |
this.allowKeys = conf["allowKeys"]
|
| 30 |
|
| 31 |
def openStorage(this, mode):
|
| 32 |
if this.storage:
|
| 33 |
raise Exception("Storage already open!")
|
| 34 |
this.storage = this.storageType(this.id, mode)
|
| 35 |
def close(this):
|
| 36 |
if this.storage:
|
| 37 |
this.storage.close()
|
| 38 |
this.storage = None
|
| 39 |
|
| 40 |
def clear(this):
|
| 41 |
if not this.storage:
|
| 42 |
raise Exception("Storage not open")
|
| 43 |
this.storage.clear()
|
| 44 |
|
| 45 |
def __setitem__(this, key, value):
|
| 46 |
if this.allowKeys != '*' and not key in this.storage:
|
| 47 |
raise KeyError("%s: Invalid key '%s'" % (this.id, key))
|
| 48 |
this.storage[key] = value
|
| 49 |
def __getitem__(this, key):
|
| 50 |
value = this.storage[key]
|
| 51 |
if not value:
|
| 52 |
raise KeyError("No value stored yet")
|
| 53 |
return value
|
| 54 |
def __contains__(this, key):
|
| 55 |
return key in this.storage
|
| 56 |
def isSet(this, key):
|
| 57 |
return this.storage.isSet(key)
|
| 58 |
def __iter__(this):
|
| 59 |
return this.storage.__iter__()
|
| 60 |
def __len__(this):
|
| 61 |
return this.storage.db.__len__()
|
| 62 |
|
| 63 |
# For the maintainance scripts, to define which keys are testable
|
| 64 |
def enableKey(this, key):
|
| 65 |
if not key in this.storage:
|
| 66 |
this.storage[key] = ""
|
| 67 |
|
| 68 |
# This is how maintainance will ask to update the keys, default to no-ops
|
| 69 |
def generateNeeded(this):
|
| 70 |
pass
|
| 71 |
def packagegeneratorFilter(this, **args):
|
| 72 |
return False
|
| 73 |
|
| 74 |
# Define this if you want to maintain a todo database about this one
|
| 75 |
def syncTodo(this):
|
| 76 |
pass
|
| 77 |
|
| 78 |
def genSummary(this, outfile):
|
| 79 |
filled = 0
|
| 80 |
total = 0
|
| 81 |
for key in this.storage:
|
| 82 |
total += 1
|
| 83 |
if this.storage.isSet(key):
|
| 84 |
filled += 1
|
| 85 |
# Total is reduced by a very small number to prevent div-by-zero in
|
| 86 |
# cornercases/bugs in types
|
| 87 |
outfile.write("Out of %i keys, %i are already known (%f%%)\n" %
|
| 88 |
(total, filled, 100.0*filled/(total - 1e-9)))
|
| 89 |
|
| 90 |
class TransientType(Type):
|
| 91 |
"Allows storing 'versions': will not overwrite previous versions"
|
| 92 |
|
| 93 |
def __init__(this, id):
|
| 94 |
#Type.__init__(this, id, storageType)
|
| 95 |
pass
|
| 96 |
|
| 97 |
|
| 98 |
def __setitem__(this, key, value):
|
| 99 |
if not key in this.storage:
|
| 100 |
raise KeyError("%s: Invalid key '%s'" % (this.id, key))
|
| 101 |
now = common.now()
|
| 102 |
this.storage["%s\001%s" % (key, now)] = value
|
| 103 |
# TODO: enable storing hashes in this one, and the real values in
|
| 104 |
# another one. Maybe create new 'bulkstorage' class which doesn't
|
| 105 |
# manage hashes itself but just returns them? Not relevant when using
|
| 106 |
# pure Bdb
|
| 107 |
this.storage[key] += "%s\n" % now
|
| 108 |
def __getitem__(this, key):
|
| 109 |
entries = Type.__getitem__(this, key).split("\n")
|
| 110 |
latest = entries[-2]
|
| 111 |
return this.storage["%s\001%s" % (key, latest)]
|
| 112 |
def __iter__(this):
|
| 113 |
for key in Type.__iter__(this):
|
| 114 |
if not key.contains("\001"):
|
| 115 |
yield key
|
| 116 |
|