mirror of https://github.com/koreader/koreader
Add LuaData and Dictionary Lookup History (#3161)
* Add dictionary history Fixes #2033, fixes #2998. * Add LuaData * table handling in base settings * Add LuaData specpull/3318/head
parent
ddf700043d
commit
d23d01643e
@ -0,0 +1,165 @@
|
||||
--[[--
|
||||
Handles append-mostly data such as KOReader's bookmarks and dictionary search history.
|
||||
]]
|
||||
|
||||
local LuaSettings = require("luasettings")
|
||||
local dbg = require("dbg")
|
||||
local dump = require("dump")
|
||||
local logger = require("logger")
|
||||
local util = require("util")
|
||||
|
||||
local LuaData = LuaSettings:new{
|
||||
name = "",
|
||||
max_backups = 9,
|
||||
}
|
||||
|
||||
--- Creates a new LuaData instance.
|
||||
function LuaData:open(file_path, o) -- luacheck: ignore 312
|
||||
if o and type(o) ~= "table" then
|
||||
if dbg.is_on then
|
||||
error("LuaData: got "..type(o)..", table expected")
|
||||
else
|
||||
o = {}
|
||||
end
|
||||
end
|
||||
-- always initiate a new instance
|
||||
-- careful, `o` is already a table so we use parentheses
|
||||
self = LuaData:new(o)
|
||||
|
||||
local new = {file=file_path, data={}}
|
||||
|
||||
-- some magic to allow for self-describing function names
|
||||
local _local = {}
|
||||
_local.__index = _local
|
||||
setmetatable(_G, _local)
|
||||
_local[self.name.."Entry"] = function(table)
|
||||
if table.index then
|
||||
-- we've got a deleted setting, overwrite with nil
|
||||
if not table.data then new.data[table.index] = nil end
|
||||
new.data[table.index] = new.data[table.index] or {}
|
||||
local size = util.tableSize(table.data)
|
||||
if size == 1 then
|
||||
for key, value in pairs(table.data) do
|
||||
new.data[table.index][key] = value
|
||||
end
|
||||
else
|
||||
new.data[table.index] = table.data
|
||||
end
|
||||
-- we've got it all at once
|
||||
else
|
||||
new.data = table
|
||||
end
|
||||
end
|
||||
|
||||
local ok = pcall(dofile, new.file)
|
||||
|
||||
if ok then
|
||||
logger.dbg("data is read from ", new.file)
|
||||
else
|
||||
logger.dbg(new.file, " is invalid, remove.")
|
||||
os.remove(new.file)
|
||||
for i=1, self.max_backups, 1 do
|
||||
local backup_file = new.file..".old."..i
|
||||
if pcall(dofile, backup_file) then
|
||||
logger.dbg("data is read from ", backup_file)
|
||||
break
|
||||
else
|
||||
logger.dbg(backup_file, " is invalid, remove.")
|
||||
os.remove(backup_file)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return setmetatable(new, {__index = self})
|
||||
end
|
||||
|
||||
--- Saves a setting.
|
||||
function LuaData:saveSetting(key, value)
|
||||
self.data[key] = value
|
||||
self:append{
|
||||
index = key,
|
||||
data = value,
|
||||
}
|
||||
return self
|
||||
end
|
||||
|
||||
--- Deletes a setting.
|
||||
function LuaData:delSetting(key)
|
||||
self.data[key] = nil
|
||||
self:append{
|
||||
index = key,
|
||||
}
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds item to table.
|
||||
function LuaData:addTableItem(table_name, value)
|
||||
local settings_table = self:has(table_name) and self:readSetting(table_name) or {}
|
||||
table.insert(settings_table, value)
|
||||
self.data[table_name] = settings_table
|
||||
self:append{
|
||||
index = table_name,
|
||||
data = {[#settings_table] = value},
|
||||
}
|
||||
end
|
||||
|
||||
local _orig_removeTableItem = LuaSettings.removeTableItem
|
||||
--- Removes index from table.
|
||||
function LuaData:removeTableItem(key, index)
|
||||
_orig_removeTableItem(self, key, index)
|
||||
self:flush()
|
||||
return self
|
||||
end
|
||||
|
||||
--- Appends settings to disk.
|
||||
function LuaData:append(data)
|
||||
if not self.file then return end
|
||||
local f_out = io.open(self.file, "a")
|
||||
if f_out ~= nil then
|
||||
os.setlocale('C', 'numeric')
|
||||
f_out:write(self.name.."Entry")
|
||||
f_out:write(dump(data))
|
||||
f_out:write("\n")
|
||||
f_out:close()
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Replaces existing settings with table.
|
||||
function LuaData:reset(table)
|
||||
self.data = table
|
||||
self:flush()
|
||||
return self
|
||||
end
|
||||
|
||||
--- Writes all settings to disk (does not append).
|
||||
function LuaData:flush()
|
||||
if not self.file then return end
|
||||
|
||||
if lfs.attributes(self.file, "mode") == "file" then
|
||||
for i=1, self.max_backups, 1 do
|
||||
if lfs.attributes(self.file..".old."..i, "mode") == "file" then
|
||||
logger.dbg("LuaData: Rename ", self.file .. ".old." .. i, " to ", self.file .. ".old." .. i+1)
|
||||
os.rename(self.file, self.file .. ".old." .. i+1)
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
logger.dbg("LuaData: Rename ", self.file, " to ", self.file .. ".old.1")
|
||||
os.rename(self.file, self.file .. ".old.1")
|
||||
end
|
||||
|
||||
logger.dbg("LuaData: Write to ", self.file)
|
||||
local f_out = io.open(self.file, "w")
|
||||
if f_out ~= nil then
|
||||
os.setlocale('C', 'numeric')
|
||||
f_out:write("-- we can read Lua syntax here!\n")
|
||||
f_out:write(self.name.."Entry")
|
||||
f_out:write(dump(self.data))
|
||||
f_out:write("\n")
|
||||
f_out:close()
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
return LuaData
|
@ -0,0 +1,145 @@
|
||||
describe("luadata module", function()
|
||||
local Settings
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
Settings = require("frontend/luadata"):open("this-is-not-a-valid-file")
|
||||
end)
|
||||
|
||||
it("should handle undefined keys", function()
|
||||
Settings:delSetting("abc")
|
||||
|
||||
assert.True(Settings:hasNot("abc"))
|
||||
assert.True(Settings:nilOrTrue("abc"))
|
||||
assert.False(Settings:isTrue("abc"))
|
||||
Settings:saveSetting("abc", true)
|
||||
assert.True(Settings:has("abc"))
|
||||
assert.True(Settings:nilOrTrue("abc"))
|
||||
assert.True(Settings:isTrue("abc"))
|
||||
end)
|
||||
|
||||
it("should flip bool values", function()
|
||||
Settings:delSetting("abc")
|
||||
|
||||
assert.True(Settings:hasNot("abc"))
|
||||
Settings:flipNilOrTrue("abc")
|
||||
assert.False(Settings:nilOrTrue("abc"))
|
||||
assert.True(Settings:has("abc"))
|
||||
assert.False(Settings:isTrue("abc"))
|
||||
Settings:flipNilOrTrue("abc")
|
||||
assert.True(Settings:nilOrTrue("abc"))
|
||||
assert.True(Settings:hasNot("abc"))
|
||||
assert.False(Settings:isTrue("abc"))
|
||||
|
||||
Settings:flipTrue("abc")
|
||||
assert.True(Settings:has("abc"))
|
||||
assert.True(Settings:isTrue("abc"))
|
||||
assert.True(Settings:nilOrTrue("abc"))
|
||||
Settings:flipTrue("abc")
|
||||
assert.False(Settings:has("abc"))
|
||||
assert.False(Settings:isTrue("abc"))
|
||||
assert.True(Settings:nilOrTrue("abc"))
|
||||
end)
|
||||
|
||||
it("should create child settings", function()
|
||||
Settings:delSetting("key")
|
||||
|
||||
Settings:saveSetting("key", {
|
||||
a = "b",
|
||||
c = "true",
|
||||
d = false,
|
||||
})
|
||||
|
||||
local child = Settings:child("key")
|
||||
|
||||
assert.is_not_nil(child)
|
||||
assert.True(child:has("a"))
|
||||
assert.are.equal(child:readSetting("a"), "b")
|
||||
assert.True(child:has("c"))
|
||||
assert.True(child:isTrue("c"))
|
||||
assert.True(child:has("d"))
|
||||
assert.True(child:isFalse("d"))
|
||||
assert.False(child:isTrue("e"))
|
||||
child:flipTrue("e")
|
||||
child:close()
|
||||
|
||||
child = Settings:child("key")
|
||||
assert.True(child:isTrue("e"))
|
||||
end)
|
||||
|
||||
describe("table wrapper", function()
|
||||
Settings:delSetting("key")
|
||||
|
||||
it("should add item to table", function()
|
||||
Settings:addTableItem("key", 1)
|
||||
Settings:addTableItem("key", 2)
|
||||
Settings:addTableItem("key", 3)
|
||||
|
||||
assert.are.equal(1, Settings:readSetting("key")[1])
|
||||
assert.are.equal(2, Settings:readSetting("key")[2])
|
||||
assert.are.equal(3, Settings:readSetting("key")[3])
|
||||
end)
|
||||
|
||||
it("should remove item from table", function()
|
||||
Settings:removeTableItem("key", 1)
|
||||
|
||||
assert.are.equal(2, Settings:readSetting("key")[1])
|
||||
assert.are.equal(3, Settings:readSetting("key")[2])
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("backup data file", function()
|
||||
local file = "dummy-test-file"
|
||||
local d = Settings:open(file)
|
||||
it("should generate data file", function()
|
||||
d:saveSetting("a", "a")
|
||||
assert.Equals("file", lfs.attributes(d.file, "mode"))
|
||||
end)
|
||||
it("should generate backup data file on flush", function()
|
||||
d:flush()
|
||||
-- file and file.old.1 should be generated.
|
||||
assert.Equals("file", lfs.attributes(d.file, "mode"))
|
||||
assert.Equals("file", lfs.attributes(d.file .. ".old.1", "mode"))
|
||||
d:close()
|
||||
end)
|
||||
it("should remove garbage data file", function()
|
||||
-- write some garbage to sidecar-file.
|
||||
local f_out = io.open(d.file, "w")
|
||||
f_out:write("bla bla bla")
|
||||
f_out:close()
|
||||
|
||||
d = Settings:open(file)
|
||||
-- file should be removed.
|
||||
assert.are.not_equal("file", lfs.attributes(d.file, "mode"))
|
||||
assert.Equals("file", lfs.attributes(d.file .. ".old.2", "mode"))
|
||||
assert.Equals("a", d:readSetting("a"))
|
||||
d:saveSetting("a", "b")
|
||||
d:close()
|
||||
-- backup should be generated.
|
||||
assert.Equals("file", lfs.attributes(d.file, "mode"))
|
||||
assert.Equals("file", lfs.attributes(d.file .. ".old.1", "mode"))
|
||||
-- The contents in file and file.old.1 are different.
|
||||
-- a:b v.s. a:a
|
||||
end)
|
||||
it("should open backup data file after garbage removal", function()
|
||||
d = Settings:open(file)
|
||||
-- We should get the right result.
|
||||
assert.Equals("b", d:readSetting("a"))
|
||||
-- write some garbage to file.
|
||||
local f_out = io.open(d.file, "w")
|
||||
f_out:write("bla bla bla")
|
||||
f_out:close()
|
||||
|
||||
-- do not flush the result, open docsettings again.
|
||||
d = Settings:open(file)
|
||||
-- data file should be removed.
|
||||
assert.are.not_equal("file", lfs.attributes(d.file, "mode"))
|
||||
assert.Equals("file", lfs.attributes(d.file .. ".old.2", "mode"))
|
||||
-- The content should come from file.old.2.
|
||||
assert.Equals("a", d:readSetting("a"))
|
||||
d:close()
|
||||
-- data file should be generated and last good backup should not change name.
|
||||
assert.Equals("file", lfs.attributes(d.file, "mode"))
|
||||
assert.Equals("file", lfs.attributes(d.file .. ".old.2", "mode"))
|
||||
end)
|
||||
end)
|
||||
end)
|
Loading…
Reference in New Issue