2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type =
13 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type;
15 local eventmanager = require "core.eventmanager";
17 module "configmanager"
21 local config = { ["*"] = { core = {} } };
23 local global_config = config["*"];
25 -- When host not found, use global
26 setmetatable(config, { __index = function () return global_config; end});
27 local host_mt = { __index = global_config };
29 -- When key not found in section, check key in global's section
30 function section_mt(section_name)
31 return { __index = function (t, k)
32 local section = rawget(global_config, section_name);
33 if not section then return nil; end
42 function get(host, section, key)
43 local sec = config[host][section];
50 function set(host, section, key, value)
51 if host and section and key then
52 local hostconfig = rawget(config, host);
53 if not hostconfig then
54 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
56 if not rawget(hostconfig, section) then
57 hostconfig[section] = setmetatable({}, section_mt(section));
59 hostconfig[section][key] = value;
65 function load(filename, format)
66 format = format or filename:match("%w+$");
68 if parsers[format] and parsers[format].load then
69 local f, err = io.open(filename);
71 local ok, err = parsers[format].load(f:read("*a"), filename);
74 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
76 return ok, "parser", err;
78 return f, "file", err;
82 return nil, "file", "no parser specified";
84 return nil, "file", "no parser for "..(format);
88 function save(filename, format)
91 function addparser(format, parser)
92 if format and parser then
93 parsers[format] = parser;
97 -- Built-in Lua parser
99 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
100 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
102 function parsers.lua.load(data, filename)
104 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
105 env = setmetatable({ Host = true; host = true; Component = true, component = true,
106 Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
107 return rawget(_G, k) or
108 function (settings_table)
109 config[__currenthost or "*"][k] = settings_table;
112 __newindex = function (t, k, v)
113 set(env.__currenthost or "*", "core", k, v);
116 rawset(env, "__currenthost", "*") -- Default is global
117 function env.Host(name)
118 rawset(env, "__currenthost", name);
119 -- Needs at least one setting to logically exist :)
120 set(name or "*", "core", "defined", true);
124 function env.Component(name)
125 set(name, "core", "component_module", "component");
126 -- Don't load the global modules by default
127 set(name, "core", "load_global_modules", false);
128 rawset(env, "__currenthost", name);
130 return function (module)
131 if type(module) == "string" then
132 set(name, "core", "component_module", module);
136 env.component = env.Component;
138 function env.Include(file)
139 local f, err = io.open(file);
141 local data = f:read("*a");
142 local ok, err = parsers.lua.load(data, file);
143 if not ok then error(err:gsub("%[string.-%]", file), 0); end
145 if not f then error("Error loading included "..file..": "..err, 0); end
148 env.include = env.Include;
150 local chunk, err = loadstring(data, "@"..filename);
158 local ok, err = pcall(chunk);