util/datamanager.lua

changeset 475
fe861de7a4fc
parent 456
27cb85d4059e
child 519
cccd610a0ef9
equal deleted inserted replaced
474:666e22848890 475:fe861de7a4fc
1 local format = string.format; 1 local format = string.format;
2 local setmetatable, type = setmetatable, type; 2 local setmetatable, type = setmetatable, type;
3 local pairs, ipairs = pairs, ipairs; 3 local pairs, ipairs = pairs, ipairs;
4 local char = string.char; 4 local char = string.char;
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; 5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
6 local log = log; 6 local log = require "util.logger".init("datamanager");
7 local io_open = io.open; 7 local io_open = io.open;
8 local os_remove = os.remove; 8 local os_remove = os.remove;
9 local tostring, tonumber = tostring, tonumber; 9 local tostring, tonumber = tostring, tonumber;
10 local error = error; 10 local error = error;
11 local next = next; 11 local next = next;
15 for n = 1, i do 15 for n = 1, i do
16 f:write("\t"); 16 f:write("\t");
17 end 17 end
18 end 18 end
19 19
20 local data_path = "data";
21
20 module "datamanager" 22 module "datamanager"
21 23
22 24
23 ---- utils ----- 25 ---- utils -----
24 local encode, decode; 26 local encode, decode;
25
26 local log = function (type, msg) return log(type, "datamanager", msg); end
27 27
28 do 28 do
29 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); 29 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
30 30
31 decode = function (s) 31 decode = function (s)
68 end 68 end
69 end 69 end
70 70
71 ------- API ------------- 71 ------- API -------------
72 72
73 function set_data_path(path)
74 data_path = path;
75 end
76
73 function getpath(username, host, datastore, ext) 77 function getpath(username, host, datastore, ext)
74 ext = ext or "dat"; 78 ext = ext or "dat";
75 if username then 79 if username then
76 return format("data/%s/%s/%s.%s", encode(host), datastore, encode(username), ext); 80 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
77 elseif host then 81 elseif host then
78 return format("data/%s/%s.%s", encode(host), datastore, ext); 82 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
79 else 83 else
80 return format("data/%s.%s", datastore, ext); 84 return format("%s/%s.%s", data_path, datastore, ext);
81 end 85 end
82 end 86 end
83 87
84 function load(username, host, datastore) 88 function load(username, host, datastore)
85 local data, ret = loadfile(getpath(username, host, datastore)); 89 local data, ret = loadfile(getpath(username, host, datastore));

mercurial