2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 local setmetatable, loadfile, pcall, rawget, rawset, io =
24 setmetatable, loadfile, pcall, rawget, rawset, io;
26 module "configmanager"
30 local config = { ["*"] = { core = {} } };
32 local global_config = config["*"];
34 -- When host not found, use global
35 setmetatable(config, { __index = function () return global_config; end});
36 local host_mt = { __index = global_config };
38 -- When key not found in section, check key in global's section
39 function section_mt(section_name)
40 return { __index = function (t, k)
41 local section = rawget(global_config, section_name);
42 if not section then return nil; end
51 function get(host, section, key)
52 local sec = config[host][section];
59 function set(host, section, key, value)
60 if host and section and key then
61 local hostconfig = rawget(config, host);
62 if not hostconfig then
63 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
65 if not rawget(hostconfig, section) then
66 hostconfig[section] = setmetatable({}, section_mt(section));
68 hostconfig[section][key] = value;
74 function load(filename, format)
75 format = format or filename:match("%w+$");
77 if parsers[format] and parsers[format].load then
78 local f, err = io.open(filename);
80 local ok, err = parsers[format].load(f:read("*a"));
88 return nil, "no parser specified";
90 return nil, "no parser for "..(format);
94 function save(filename, format)
97 function addparser(format, parser)
98 if format and parser then
99 parsers[format] = parser;
103 -- Built-in Lua parser
105 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
106 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
108 function parsers.lua.load(data)
110 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
111 env = setmetatable({ Host = true; host = true; Component = true, component = true }, { __index = function (t, k)
112 return rawget(_G, k) or
113 function (settings_table)
114 config[__currenthost or "*"][k] = settings_table;
117 __newindex = function (t, k, v)
118 set(env.__currenthost or "*", "core", k, v);
121 function env.Host(name)
122 rawset(env, "__currenthost", name);
123 -- Needs at least one setting to logically exist :)
124 set(name or "*", "core", "defined", true);
128 function env.Component(name)
129 return function (module)
130 set(name, "core", "component_module", module);
131 -- Don't load the global modules by default
132 set(name, "core", "modules_enable", false);
133 rawset(env, "__currenthost", name);
136 env.component = env.Component;
138 local chunk, err = loadstring(data);
146 local ok, err = pcall(chunk);