Annotate

util/datamanager.lua @ 87:059ef1c30844

Merge from Matthew. datamanager.simplesave fix
author Waqas Hussain <waqas20@gmail.com>
date Thu, 09 Oct 2008 03:01:47 +0500
parent 84:d0a0bac6815e
parent 82:cbf387f29d56
child 88:023320150c65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local setmetatable, type = setmetatable, type;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local pairs = pairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local char = string.char;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local log = log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local io_open = io.open;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
8
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9 module "datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
11
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12 ---- utils -----
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13 local encode, decode;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15 local log = function (type, msg) return log(type, "datamanager", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
16
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
17 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
18 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20 decode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24 encode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29 local function basicSerialize (o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 if type(o) == "number" or type(o) == "boolean" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 return tostring(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 else -- assume it is a string
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33 return string.format("%q", tostring(o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38 local function simplesave (f, o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 if type(o) == "number" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
40 f:write(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
41 elseif type(o) == "string" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42 f:write(format("%q", o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43 elseif type(o) == "table" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44 f:write("{\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 for k,v in pairs(o) do
82
cbf387f29d56 Fix for saving tables with non-string keys
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
46 f:write(" [", basicSerialize(k), "] = ")
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47 simplesave(f, v)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 f:write(",\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
50 f:write("}\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 error("cannot serialize a " .. type(o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
53 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56 ------- API -------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
57
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58 function getpath(username, host, datastore)
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
59 if username then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
60 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
61 elseif host then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
62 return format("data/%s/%s.dat", encode(host), datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
63 else
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
64 return format("data/%s.dat", datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
65 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
66 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68 function load(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
69 local data, ret = loadfile(getpath(username, host, datastore));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70 if not data then log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
71 setfenv(data, {});
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
72 local success, ret = pcall(data);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
73 if not success then log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
74 return ret;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
75 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
76
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
77 function store(username, host, datastore, data)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
78 local f, msg = io_open(getpath(username, host, datastore), "w+");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
79 if not f then log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
80 f:write("return ");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
81 simplesave(f, data);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
82 f:close();
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
83 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
84 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
85