core/configmanager.lua

Sat, 29 Nov 2008 03:26:46 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 29 Nov 2008 03:26:46 +0000
changeset 466
0ecfd89c2cc0
parent 376
6d87944df37c
child 519
cccd610a0ef9
permissions
-rw-r--r--

Fix for configmanager when config file can't be found

371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local _G = _G;
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
3 local setmetatable, loadfile, pcall, rawget, rawset, io =
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
4 setmetatable, loadfile, pcall, rawget, rawset, io;
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
5
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 module "configmanager"
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local parsers = {};
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local config = { ["*"] = { core = {} } };
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local global_config = config["*"];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 -- When host not found, use global
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 setmetatable(config, { __index = function () return global_config; end});
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local host_mt = { __index = global_config };
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- When key not found in section, check key in global's section
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 function section_mt(section_name)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return { __index = function (t, k)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local section = rawget(global_config, section_name);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if not section then return nil; end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return section[k];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end };
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
27 function getconfig()
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
28 return config;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
29 end
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
30
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function get(host, section, key)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local sec = config[host][section];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if sec then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 return sec[key];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return nil;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function set(host, section, key, value)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if host and section and key then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local hostconfig = rawget(config, host);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not hostconfig then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if not rawget(hostconfig, section) then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 hostconfig[section] = setmetatable({}, section_mt(section));
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 hostconfig[section][key] = value;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return true;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 return false;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function load(filename, format)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
55 format = format or filename:match("%w+$");
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
56
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if parsers[format] and parsers[format].load then
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
58 local f, err = io.open(filename);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if f then
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
60 local ok, err = parsers[format].load(f:read("*a"));
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 f:close();
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return ok, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
64 return f, err;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
66
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
67 if not format then
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
68 return nil, "no parser specified";
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
69 else
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
70 return nil, "no parser for "..(format);
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
71 end
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 function save(filename, format)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 function addparser(format, parser)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if format and parser then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 parsers[format] = parser;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
83 -- Built-in Lua parser
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 do
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
85 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
86 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 parsers.lua = {};
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 function parsers.lua.load(data)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
89 local env;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
90 env = setmetatable({ Host = true; host = true; }, { __index = function (t, k)
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
91 return rawget(_G, k) or
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
92 function (settings_table)
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 config[__currenthost or "*"][k] = settings_table;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end;
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
95 end,
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
96 __newindex = function (t, k, v)
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
97 set(env.__currenthost or "*", "core", k, v);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end});
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 function env.Host(name)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
101 rawset(env, "__currenthost", name);
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
102 set(name or "*", "core", "defined", true);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
104 env.host = env.Host;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local chunk, err = loadstring(data);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 if not chunk then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 setfenv(chunk, env);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local ok, err = pcall(chunk);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if not ok then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return true;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
125 return _M;

mercurial