Software /
code /
prosody
Annotate
core/objectmanager.lua @ 2425:772b2caf762e
net.dns: Make sure math.randomseed() gets passed an integer
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 07 Jan 2010 01:26:01 +0000 |
parent | 2102:b5ee3c416609 |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
1 -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
889
diff
changeset
|
8 |
2102
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
9 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
10 local new_multitable = require "util.multitable".new; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
11 local t_insert = table.insert; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
12 local t_concat = table.concat; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
13 local tostring = tostring; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
14 local unpack = unpack; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
15 local pairs = pairs; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
16 local error = error; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
17 local type = type; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
18 local _G = _G; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
19 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
20 local data = new_multitable(); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
21 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
22 module "objectmanager" |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
23 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
24 function set(...) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
25 return data:set(...); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
26 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
27 function remove(...) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
28 return data:remove(...); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
29 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
30 function get(...) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
31 return data:get(...); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
32 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
33 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
34 local function get_path(path) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
35 if type(path) == "table" then return path; end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
36 local s = {}; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
37 for part in tostring(path):gmatch("[%w_]+") do |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
38 t_insert(s, part); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
39 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
40 return s; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
41 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
42 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
43 function get_object(path) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
44 path = get_path(path) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
45 return data:get(unpack(path)), path; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
46 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
47 function set_object(path, object) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
48 path = get_path(path); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
49 data:set(unpack(path), object); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
50 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
51 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
52 data:set("ls", function(_dir) |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
53 local obj, dir = get_object(_dir); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
54 if not obj then error("object not found: " .. t_concat(dir, '/')); end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
55 local r = {}; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
56 if type(obj) == "table" then |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
57 for key, val in pairs(obj) do |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
58 r[key] = type(val); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
59 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
60 end |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
61 return r; |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
62 end); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
63 data:set("get", get_object); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
64 data:set("set", set_object); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
65 data:set("echo", function(...) return {...}; end); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
66 data:set("_G", _G); |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
67 |
b5ee3c416609
objectmanager: Convert to unix line-endings
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
68 return _M; |