Mercurial > prosody-hg
comparison core/configmanager.lua @ 371:0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 21 Nov 2008 05:47:27 +0000 |
| parents | |
| children | 6d87944df37c |
comparison
equal
deleted
inserted
replaced
| 370:9ade55e059ea | 371:0dc5819660e8 |
|---|---|
| 1 | |
| 2 local _G = _G; | |
| 3 module "configmanager" | |
| 4 | |
| 5 local parsers = {}; | |
| 6 | |
| 7 local config = { ["*"] = { core = {} } }; | |
| 8 | |
| 9 local global_config = config["*"]; | |
| 10 | |
| 11 -- When host not found, use global | |
| 12 setmetatable(config, { __index = function () return global_config; end}); | |
| 13 local host_mt = { __index = global_config }; | |
| 14 | |
| 15 -- When key not found in section, check key in global's section | |
| 16 function section_mt(section_name) | |
| 17 return { __index = function (t, k) | |
| 18 local section = rawget(global_config, section_name); | |
| 19 if not section then return nil; end | |
| 20 return section[k]; | |
| 21 end }; | |
| 22 end | |
| 23 | |
| 24 function get(host, section, key) | |
| 25 local sec = config[host][section]; | |
| 26 if sec then | |
| 27 return sec[key]; | |
| 28 end | |
| 29 return nil; | |
| 30 end | |
| 31 | |
| 32 function set(host, section, key, value) | |
| 33 if host and section and key then | |
| 34 local hostconfig = rawget(config, host); | |
| 35 if not hostconfig then | |
| 36 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host]; | |
| 37 end | |
| 38 if not rawget(hostconfig, section) then | |
| 39 hostconfig[section] = setmetatable({}, section_mt(section)); | |
| 40 end | |
| 41 hostconfig[section][key] = value; | |
| 42 return true; | |
| 43 end | |
| 44 return false; | |
| 45 end | |
| 46 | |
| 47 function load(filename, format) | |
| 48 if parsers[format] and parsers[format].load then | |
| 49 local f = io.open(filename); | |
| 50 if f then | |
| 51 local ok, err = parsers[format](f:read("*a")); | |
| 52 f:close(); | |
| 53 return ok, err; | |
| 54 end | |
| 55 end | |
| 56 return false, "no parser"; | |
| 57 end | |
| 58 | |
| 59 function save(filename, format) | |
| 60 end | |
| 61 | |
| 62 function addparser(format, parser) | |
| 63 if format and parser then | |
| 64 parsers[format] = parser; | |
| 65 end | |
| 66 end | |
| 67 | |
| 68 do | |
| 69 parsers.lua = {}; | |
| 70 function parsers.lua.load(data) | |
| 71 local env = setmetatable({}, { __index = function (t, k) | |
| 72 if k:match("^mod_") then | |
| 73 return function (settings_table) | |
| 74 config[__currenthost or "*"][k] = settings_table; | |
| 75 end; | |
| 76 end | |
| 77 return rawget(_G, k); | |
| 78 end}); | |
| 79 | |
| 80 function env.Host(name) | |
| 81 env.__currenthost = name; | |
| 82 end | |
| 83 | |
| 84 local chunk, err = loadstring(data); | |
| 85 | |
| 86 if not chunk then | |
| 87 return nil, err; | |
| 88 end | |
| 89 | |
| 90 setfenv(chunk, env); | |
| 91 | |
| 92 local ok, err = pcall(chunk); | |
| 93 | |
| 94 if not ok then | |
| 95 return nil, err; | |
| 96 end | |
| 97 | |
| 98 | |
| 99 | |
| 100 return true; | |
| 101 end | |
| 102 | |
| 103 end | |
| 104 | |
| 105 return _M; |
