7 local config = { ["*"] = { core = {} } };
9 local global_config = config["*"];
11 -- When host not found, use global
12 setmetatable(config, { __index = function () return global_config; end});
13 local host_mt = { __index = global_config };
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
24 function get(host, section, key)
25 local sec = config[host][section];
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];
38 if not rawget(hostconfig, section) then
39 hostconfig[section] = setmetatable({}, section_mt(section));
41 hostconfig[section][key] = value;
47 function load(filename, format)
48 if parsers[format] and parsers[format].load then
49 local f = io.open(filename);
51 local ok, err = parsers[format](f:read("*a"));
56 return false, "no parser";
59 function save(filename, format)
62 function addparser(format, parser)
63 if format and parser then
64 parsers[format] = parser;
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;
80 function env.Host(name)
81 env.__currenthost = name;
84 local chunk, err = loadstring(data);
92 local ok, err = pcall(chunk);