Comparison

util/datamanager.lua @ 2583:cb1f9266130b

util.datamanager: Added support for hooks to override behavior.
author Waqas Hussain <waqas20@gmail.com>
date Thu, 11 Feb 2010 05:21:03 +0500
parent 2444:267d6482bac6
child 2626:39a2e8ff0b0f
comparison
equal deleted inserted replaced
2582:d6afb6d919df 2583:cb1f9266130b
58 local data_path = "data"; 58 local data_path = "data";
59 local callbacks = {}; 59 local callbacks = {};
60 60
61 ------- API ------------- 61 ------- API -------------
62 62
63 local _set_data_path;
63 function set_data_path(path) 64 function set_data_path(path)
65 if _set_data_path then return _set_data_path(path); end
64 log("debug", "Setting data path to: %s", path); 66 log("debug", "Setting data path to: %s", path);
65 data_path = path; 67 data_path = path;
66 end 68 end
67 69
68 local function callback(username, host, datastore, data) 70 local function callback(username, host, datastore, data)
71 if username == false then break; end 73 if username == false then break; end
72 end 74 end
73 75
74 return username, host, datastore, data; 76 return username, host, datastore, data;
75 end 77 end
78 local _add_callback;
76 function add_callback(func) 79 function add_callback(func)
80 if _add_callback then return _add_callback(func); end
77 if not callbacks[func] then -- Would you really want to set the same callback more than once? 81 if not callbacks[func] then -- Would you really want to set the same callback more than once?
78 callbacks[func] = true; 82 callbacks[func] = true;
79 callbacks[#callbacks+1] = func; 83 callbacks[#callbacks+1] = func;
80 return true; 84 return true;
81 end 85 end
82 end 86 end
87 local _remove_callback;
83 function remove_callback(func) 88 function remove_callback(func)
89 if _remove_callback then return _remove_callback(func); end
84 if callbacks[func] then 90 if callbacks[func] then
85 for i, f in ipairs(callbacks) do 91 for i, f in ipairs(callbacks) do
86 if f == func then 92 if f == func then
87 callbacks[i] = nil; 93 callbacks[i] = nil;
88 callbacks[f] = nil; 94 callbacks[f] = nil;
90 end 96 end
91 end 97 end
92 end 98 end
93 end 99 end
94 100
101 local _getpath;
95 function getpath(username, host, datastore, ext, create) 102 function getpath(username, host, datastore, ext, create)
103 if _getpath then return _getpath(username, host, datastore, ext, create); end
96 ext = ext or "dat"; 104 ext = ext or "dat";
97 host = (host and encode(host)) or "_global"; 105 host = (host and encode(host)) or "_global";
98 username = username and encode(username); 106 username = username and encode(username);
99 if username then 107 if username then
100 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end 108 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
106 if create then mkdir(data_path); end 114 if create then mkdir(data_path); end
107 return format("%s/%s.%s", data_path, datastore, ext); 115 return format("%s/%s.%s", data_path, datastore, ext);
108 end 116 end
109 end 117 end
110 118
119 local _load;
111 function load(username, host, datastore) 120 function load(username, host, datastore)
121 if _load then return _load(username, host, datastore); end
112 local data, ret = loadfile(getpath(username, host, datastore)); 122 local data, ret = loadfile(getpath(username, host, datastore));
113 if not data then 123 if not data then
114 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 124 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
115 return nil; 125 return nil;
116 end 126 end
121 return nil; 131 return nil;
122 end 132 end
123 return ret; 133 return ret;
124 end 134 end
125 135
136 local _store;
126 function store(username, host, datastore, data) 137 function store(username, host, datastore, data)
138 if _store then return _store(username, host, datastore, data); end
127 if not data then 139 if not data then
128 data = {}; 140 data = {};
129 end 141 end
130 142
131 username, host, datastore, data = callback(username, host, datastore, data); 143 username, host, datastore, data = callback(username, host, datastore, data);
149 -- we write data even when we are deleting because lua doesn't have a 161 -- we write data even when we are deleting because lua doesn't have a
150 -- platform independent way of checking for non-exisitng files 162 -- platform independent way of checking for non-exisitng files
151 return true; 163 return true;
152 end 164 end
153 165
166 local _list_append;
154 function list_append(username, host, datastore, data) 167 function list_append(username, host, datastore, data)
168 if _list_append then return _list_append(username, host, datastore, data); end
155 if not data then return; end 169 if not data then return; end
156 if callback(username, host, datastore) == false then return true; end 170 if callback(username, host, datastore) == false then return true; end
157 -- save the datastore 171 -- save the datastore
158 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); 172 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
159 if not f then 173 if not f then
165 f:write(");\n"); 179 f:write(");\n");
166 f:close(); 180 f:close();
167 return true; 181 return true;
168 end 182 end
169 183
184 local _list_store;
170 function list_store(username, host, datastore, data) 185 function list_store(username, host, datastore, data)
186 if _list_store then return _list_store(username, host, datastore, data); end
171 if not data then 187 if not data then
172 data = {}; 188 data = {};
173 end 189 end
174 if callback(username, host, datastore) == false then return true; end 190 if callback(username, host, datastore) == false then return true; end
175 -- save the datastore 191 -- save the datastore
191 -- we write data even when we are deleting because lua doesn't have a 207 -- we write data even when we are deleting because lua doesn't have a
192 -- platform independent way of checking for non-exisitng files 208 -- platform independent way of checking for non-exisitng files
193 return true; 209 return true;
194 end 210 end
195 211
212 local _list_load;
196 function list_load(username, host, datastore) 213 function list_load(username, host, datastore)
214 if _list_load then return _list_load(username, host, datastore); end
197 local data, ret = loadfile(getpath(username, host, datastore, "list")); 215 local data, ret = loadfile(getpath(username, host, datastore, "list"));
198 if not data then 216 if not data then
199 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); 217 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
200 return nil; 218 return nil;
201 end 219 end
207 return nil; 225 return nil;
208 end 226 end
209 return items; 227 return items;
210 end 228 end
211 229
230 function set(t)
231 _set_data_path = t.set_data_path;
232 _add_callback = t.add_callback;
233 _remove_callback = t.remove_callback;
234 _getpath = t.getpath;
235 _load = t.load;
236 _store = t.store;
237 _list_append = t.list_append;
238 _list_store = t.list_store;
239 _list_load = t.list_load;
240 end
241 function get()
242 return {
243 set_data_path = _set_data_path;
244 add_callback = _add_callback;
245 remove_callback = _remove_callback;
246 getpath = _getpath;
247 load = _load;
248 store = _store;
249 list_append = _list_append;
250 list_store = _list_store;
251 list_load = _list_load;
252 };
253 end
254
212 return _M; 255 return _M;