KVS.remove() test now passes
1 parent 8d6b9e5 commit 12941d9633493d31c63e14c095efc3d62b3f96a5
@David Whale David Whale authored on 27 May 2016
Showing 2 changed files
View
40
src/energenie/KVS.py
v = values[k]
f.write("%s=%s\n" % (k, v))
f.write("\n")
 
@unimplemented
@untested
def remove(self, key):
"""Remove reference to this key in the file, and remove from in memory store"""
if self.filename != None:
pass #TODO
"""Remove reference to this key in the file"""
if self.filename == None:
return # No file, nothing to do
 
####HERE####
 
# open file for read write
# search line at a time, process each command
# when we find the command 'ADD key'
# reseek to start of line
# write overwrite ADD with IGN
# keep going in case of duplicates
# close file
with open(self.filename, 'r+') as f: # read and write mode
while True:
start = f.tell() # start of current line
line = f.readline()
end = f.tell() # position just beyond end of line
if line == "": break # EOF
# don't worry about cmd/data, space in search string disambiguates
if line.startswith("ADD "): # space after is critical to simplify parser
line = line.strip() # remove nl
cmd, this_key = line.split(" ", 1)
if this_key == key:
print("found: %s %s" % (cmd, this_key))
f.seek(start) # back to start of line
f.write('IGN') # patch it to be an ignore record but leave record intact
f.seek(end) # back to end of line, to process next lines
print("Patched to IGN rec")
 
def write(self, filename=None):
"""Rewrite the whole in memory cache over the top of the external file"""
#or create a new file if one does not exist
View
4
src/energenie/KVS_test.py
remove_file(self.KVS_FILENAME)
kvs = KVS(self.KVS_FILENAME)
 
kvs["tv1"] = TV(1)
kvs["tv2"] = TV(2)
kvs["tv3"] = TV(3)
kvs["tv4"] = TV(4)
 
show_file(self.KVS_FILENAME)
 
del kvs["tv1"] ####FAIL remove() needs implementing