Comparison

core/configmanager.lua @ 8153:c22d5680ca68

configmanager: Remove support for multiple parsers, fixes #852.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 27 May 2017 15:32:28 +0100
parent 7947:24170d74b00b
child 8158:b4cbd65ee4c5
comparison
equal deleted inserted replaced
8150:4720f5ec4171 8153:c22d5680ca68
25 local _M = {}; 25 local _M = {};
26 local _ENV = nil; 26 local _ENV = nil;
27 27
28 _M.resolve_relative_path = resolve_relative_path; -- COMPAT 28 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
29 29
30 local parsers = {}; 30 local parser = nil;
31 31
32 local config_mt = { __index = function (t, _) return rawget(t, "*"); end}; 32 local config_mt = { __index = function (t, _) return rawget(t, "*"); end};
33 local config = setmetatable({ ["*"] = { } }, config_mt); 33 local config = setmetatable({ ["*"] = { } }, config_mt);
34 34
35 -- When host not found, use global 35 -- When host not found, use global
75 end 75 end
76 76
77 function _M.load(filename, config_format) 77 function _M.load(filename, config_format)
78 config_format = config_format or filename:match("%w+$"); 78 config_format = config_format or filename:match("%w+$");
79 79
80 if parsers[config_format] and parsers[config_format].load then 80 if config_format == "lua" then
81 local f, err = io.open(filename); 81 local f, err = io.open(filename);
82 if f then 82 if f then
83 local new_config = setmetatable({ ["*"] = { } }, config_mt); 83 local new_config = setmetatable({ ["*"] = { } }, config_mt);
84 local ok, err = parsers[config_format].load(f:read("*a"), filename, new_config); 84 local ok, err = parser.load(f:read("*a"), filename, new_config);
85 f:close(); 85 f:close();
86 if ok then 86 if ok then
87 config = new_config; 87 config = new_config;
88 fire_event("config-reloaded", { 88 fire_event("config-reloaded", {
89 filename = filename, 89 filename = filename,
101 else 101 else
102 return nil, "file", "no parser for "..(config_format); 102 return nil, "file", "no parser for "..(config_format);
103 end 103 end
104 end 104 end
105 105
106 function _M.addparser(config_format, parser)
107 if config_format and parser then
108 parsers[config_format] = parser;
109 end
110 end
111
112 -- _M needed to avoid name clash with local 'parsers'
113 function _M.parsers()
114 local p = {};
115 for config_format in pairs(parsers) do
116 table.insert(p, config_format);
117 end
118 return p;
119 end
120
121 -- Built-in Lua parser 106 -- Built-in Lua parser
122 do 107 do
123 local pcall = _G.pcall; 108 local pcall = _G.pcall;
124 parsers.lua = {}; 109 parser = {};
125 function parsers.lua.load(data, config_file, config_table) 110 function parser.load(data, config_file, config_table)
126 local env; 111 local env;
127 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below 112 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
128 env = setmetatable({ 113 env = setmetatable({
129 Host = true, host = true, VirtualHost = true, 114 Host = true, host = true, VirtualHost = true,
130 Component = true, component = true, 115 Component = true, component = true,
209 end 194 end
210 -- Not a wildcard, so resolve (potentially) relative path and run through config parser 195 -- Not a wildcard, so resolve (potentially) relative path and run through config parser
211 file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file); 196 file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
212 local f, err = io.open(file); 197 local f, err = io.open(file);
213 if f then 198 if f then
214 local ret, err = parsers.lua.load(f:read("*a"), file, config_table); 199 local ret, err = parser.load(f:read("*a"), file, config_table);
215 if not ret then error(err:gsub("%[string.-%]", file), 0); end 200 if not ret then error(err:gsub("%[string.-%]", file), 0); end
216 end 201 end
217 if not f then error("Error loading included "..file..": "..err, 0); end 202 if not f then error("Error loading included "..file..": "..err, 0); end
218 return f, err; 203 return f, err;
219 end 204 end