Comparison

core/configmanager.lua @ 6779:6236668da30a

core.*: Remove use of module() function
author Kim Alvefur <zash@zash.se>
date Sat, 21 Feb 2015 10:42:19 +0100
parent 6718:be98ebe87eef
child 7947:24170d74b00b
comparison
equal deleted inserted replaced
6778:4009ae66e0f0 6779:6236668da30a
17 local deps = require"util.dependencies"; 17 local deps = require"util.dependencies";
18 local resolve_relative_path = require"util.paths".resolve_relative_path; 18 local resolve_relative_path = require"util.paths".resolve_relative_path;
19 local glob_to_pattern = require"util.paths".glob_to_pattern; 19 local glob_to_pattern = require"util.paths".glob_to_pattern;
20 local path_sep = package.config:sub(1,1); 20 local path_sep = package.config:sub(1,1);
21 21
22 local have_encodings, encodings = pcall(require, "util.encodings"); 22 local encodings = deps.softreq"util.encodings";
23 local nameprep = have_encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end 23 local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
24 24
25 module "configmanager" 25 local _M = {};
26 local _ENV = nil;
26 27
27 _M.resolve_relative_path = resolve_relative_path; -- COMPAT 28 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
28 29
29 local parsers = {}; 30 local parsers = {};
30 31
32 local config = setmetatable({ ["*"] = { } }, config_mt); 33 local config = setmetatable({ ["*"] = { } }, config_mt);
33 34
34 -- When host not found, use global 35 -- When host not found, use global
35 local host_mt = { __index = function(_, k) return config["*"][k] end } 36 local host_mt = { __index = function(_, k) return config["*"][k] end }
36 37
37 function getconfig() 38 function _M.getconfig()
38 return config; 39 return config;
39 end 40 end
40 41
41 function get(host, key, _oldkey) 42 function _M.get(host, key, _oldkey)
42 if key == "core" then 43 if key == "core" then
43 key = _oldkey; -- COMPAT with code that still uses "core" 44 key = _oldkey; -- COMPAT with code that still uses "core"
44 end 45 end
45 return config[host][key]; 46 return config[host][key];
46 end 47 end
71 key, value = value, _oldvalue; --COMPAT with code that still uses "core" 72 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
72 end 73 end
73 return set(config, host, key, value); 74 return set(config, host, key, value);
74 end 75 end
75 76
76 function load(filename, config_format) 77 function _M.load(filename, config_format)
77 config_format = config_format or filename:match("%w+$"); 78 config_format = config_format or filename:match("%w+$");
78 79
79 if parsers[config_format] and parsers[config_format].load then 80 if parsers[config_format] and parsers[config_format].load then
80 local f, err = io.open(filename); 81 local f, err = io.open(filename);
81 if f then 82 if f then
100 else 101 else
101 return nil, "file", "no parser for "..(config_format); 102 return nil, "file", "no parser for "..(config_format);
102 end 103 end
103 end 104 end
104 105
105 function addparser(config_format, parser) 106 function _M.addparser(config_format, parser)
106 if config_format and parser then 107 if config_format and parser then
107 parsers[config_format] = parser; 108 parsers[config_format] = parser;
108 end 109 end
109 end 110 end
110 111