|
@@ -6,6 +6,13 @@ local term = require "volksdata.term"
|
|
|
local nsm = require "volksdata.namespace"
|
|
|
|
|
|
|
|
|
+-- Read only module properties.
|
|
|
+local PROTECTED = {
|
|
|
+ store = true,
|
|
|
+ reset_store = true,
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
local fpath = debug.getinfo(1, "S").source:sub(2)
|
|
|
local root_path = path.dirname(path.dirname(fpath))
|
|
|
local config_path = os.getenv("PKAR_CONFIG_DIR") or
|
|
@@ -14,9 +21,16 @@ local config_path = os.getenv("PKAR_CONFIG_DIR") or
|
|
|
local config = dofile(path.join(config_path, "app.lua"))
|
|
|
local store_id = "file://" .. (os.getenv("PKAR_BASE") or config.fs.dres_path)
|
|
|
|
|
|
+-- Fill namespace map from config.
|
|
|
for pfx, ns in pairs(config.namespace) do nsm.add(pfx, ns) end
|
|
|
|
|
|
|
|
|
+-- Initialize the store. `clear` wipes all data. YOU HAVE BEEN WARNED!
|
|
|
+local store_init = function(clear)
|
|
|
+ return store.new(store.MDB, store_id, clear)
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
local M = {
|
|
|
-- Project root path.
|
|
|
root = root_path,
|
|
@@ -55,14 +69,18 @@ local M = {
|
|
|
-- Common namespaces
|
|
|
PAR_NS = nsm.get_ns("par"),
|
|
|
PAS_NS = nsm.get_ns("pas"),
|
|
|
-}
|
|
|
|
|
|
+ -- Methods.
|
|
|
|
|
|
--- Initialize random ID generator.
|
|
|
-math.randomseed(M.config.id.seed[1], M.config.id.seed[2])
|
|
|
+ -- Escape strings for use in gsub patterns.
|
|
|
+ escape_ptn = function(src)
|
|
|
+ return src:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%0")
|
|
|
+ end,
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
--[[
|
|
|
- Gnerate pairtree directory and file path from an ID string and prefix.
|
|
|
+ Generate pairtree directory and file path from an ID string and prefix.
|
|
|
|
|
|
The directory is created if not existing.
|
|
|
|
|
@@ -94,18 +112,31 @@ M.gen_pairtree = function (pfx, id_str, ext, no_create)
|
|
|
end
|
|
|
|
|
|
|
|
|
--- Initialize the store. `clear` wipes all data. YOU HAVE BEEN WARNED!
|
|
|
-M.init = function(clear)
|
|
|
- M.store = store.new(store.MDB, store_id, clear)
|
|
|
-end
|
|
|
+-- No more assignments to the module afte here.
|
|
|
|
|
|
|
|
|
---[[
|
|
|
- Escape strings for use in gsub patterns.
|
|
|
---]]
|
|
|
-M.escape_ptn = function(src)
|
|
|
- return src:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%0")
|
|
|
-end
|
|
|
+local mt = {
|
|
|
+ __index = function(t, k)
|
|
|
+ if (k == "store") then
|
|
|
+ if not rawget(t, "_store") then
|
|
|
+ rawset(t, "_store", store.new(store.MDB, store_id)) end
|
|
|
+ return rawget(t, "_store")
|
|
|
+
|
|
|
+ -- WIPE ALL DATA from the store and returns the new empty store handle.
|
|
|
+ elseif (k == "reset_store") then
|
|
|
+ rawset(t, "_store", store.new(store.MDB, store_id, true))
|
|
|
+ return rawget(t, "_store")
|
|
|
+ end
|
|
|
+ end,
|
|
|
+
|
|
|
+ __newindex = function () return nil, "Module is read only." end
|
|
|
+}
|
|
|
+
|
|
|
+setmetatable (M, mt)
|
|
|
+
|
|
|
+
|
|
|
+-- Initialize random ID generator.
|
|
|
+math.randomseed(M.config.id.seed[1], M.config.id.seed[2])
|
|
|
|
|
|
|
|
|
return M
|