Software /
code /
prosody
Comparison
core/configmanager.lua @ 376:6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 23 Nov 2008 02:12:46 +0000 |
parent | 371:0dc5819660e8 |
child | 466:0ecfd89c2cc0 |
comparison
equal
deleted
inserted
replaced
375:a6a4ea3633b0 | 376:6d87944df37c |
---|---|
1 | 1 |
2 local _G = _G; | 2 local _G = _G; |
3 local setmetatable, loadfile, pcall, rawget, rawset, io = | |
4 setmetatable, loadfile, pcall, rawget, rawset, io; | |
3 module "configmanager" | 5 module "configmanager" |
4 | 6 |
5 local parsers = {}; | 7 local parsers = {}; |
6 | 8 |
7 local config = { ["*"] = { core = {} } }; | 9 local config = { ["*"] = { core = {} } }; |
17 return { __index = function (t, k) | 19 return { __index = function (t, k) |
18 local section = rawget(global_config, section_name); | 20 local section = rawget(global_config, section_name); |
19 if not section then return nil; end | 21 if not section then return nil; end |
20 return section[k]; | 22 return section[k]; |
21 end }; | 23 end }; |
24 end | |
25 | |
26 function getconfig() | |
27 return config; | |
22 end | 28 end |
23 | 29 |
24 function get(host, section, key) | 30 function get(host, section, key) |
25 local sec = config[host][section]; | 31 local sec = config[host][section]; |
26 if sec then | 32 if sec then |
43 end | 49 end |
44 return false; | 50 return false; |
45 end | 51 end |
46 | 52 |
47 function load(filename, format) | 53 function load(filename, format) |
54 format = format or filename:match("%w+$"); | |
48 if parsers[format] and parsers[format].load then | 55 if parsers[format] and parsers[format].load then |
49 local f = io.open(filename); | 56 local f = io.open(filename); |
50 if f then | 57 if f then |
51 local ok, err = parsers[format](f:read("*a")); | 58 local ok, err = parsers[format].load(f:read("*a")); |
52 f:close(); | 59 f:close(); |
53 return ok, err; | 60 return ok, err; |
54 end | 61 end |
55 end | 62 end |
56 return false, "no parser"; | 63 if not format then |
64 return nil, "no parser specified"; | |
65 else | |
66 return false, "no parser"; | |
67 end | |
57 end | 68 end |
58 | 69 |
59 function save(filename, format) | 70 function save(filename, format) |
60 end | 71 end |
61 | 72 |
63 if format and parser then | 74 if format and parser then |
64 parsers[format] = parser; | 75 parsers[format] = parser; |
65 end | 76 end |
66 end | 77 end |
67 | 78 |
79 -- Built-in Lua parser | |
68 do | 80 do |
81 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable; | |
82 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring; | |
69 parsers.lua = {}; | 83 parsers.lua = {}; |
70 function parsers.lua.load(data) | 84 function parsers.lua.load(data) |
71 local env = setmetatable({}, { __index = function (t, k) | 85 local env; |
72 if k:match("^mod_") then | 86 env = setmetatable({ Host = true; host = true; }, { __index = function (t, k) |
73 return function (settings_table) | 87 return rawget(_G, k) or |
88 function (settings_table) | |
74 config[__currenthost or "*"][k] = settings_table; | 89 config[__currenthost or "*"][k] = settings_table; |
75 end; | 90 end; |
76 end | 91 end, |
77 return rawget(_G, k); | 92 __newindex = function (t, k, v) |
93 set(env.__currenthost or "*", "core", k, v); | |
78 end}); | 94 end}); |
79 | 95 |
80 function env.Host(name) | 96 function env.Host(name) |
81 env.__currenthost = name; | 97 rawset(env, "__currenthost", name); |
98 set(name or "*", "core", "defined", true); | |
82 end | 99 end |
100 env.host = env.Host; | |
83 | 101 |
84 local chunk, err = loadstring(data); | 102 local chunk, err = loadstring(data); |
85 | 103 |
86 if not chunk then | 104 if not chunk then |
87 return nil, err; | 105 return nil, err; |
93 | 111 |
94 if not ok then | 112 if not ok then |
95 return nil, err; | 113 return nil, err; |
96 end | 114 end |
97 | 115 |
98 | |
99 | |
100 return true; | 116 return true; |
101 end | 117 end |
102 | 118 |
103 end | 119 end |
104 | 120 |