Annotate

core/moduleapi.lua @ 4613:27fbc9c69eb5

moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Mar 2012 02:52:31 +0000
parent 4605:69746db53125
child 4614:20940729c1b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2012 Matthew Wild
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2012 Waqas Hussain
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local config = require "core.configmanager";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local modulemanager = require "modulemanager";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local array = require "util.array";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local set = require "util.set";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local logger = require "util.logger";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local pluginloader = require "util.pluginloader";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local multitable_new = require "util.multitable".new;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local error, setmetatable, setfenv, type = error, setmetatable, setfenv, type;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local tonumber, tostring = tonumber, tostring;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local prosody = prosody;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local hosts = prosody.hosts;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Registry of shared module data
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local shared_data = setmetatable({}, { __mode = "v" });
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local NULL = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local api = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- Returns the name of the current module
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function api:get_name()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return self.name;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- Returns the host that the current module is serving
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function api:get_host()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return self.host;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 function api:get_host_type()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return hosts[self.host].type;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function api:set_global()
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 self.host = "*";
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 -- Update the logger
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local _log = logger.init("mod_"..self.name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 self.log = function (self, ...) return _log(...); end;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 self._log = _log;
4605
69746db53125 moduleapi: Set module.global = true when module:set_global() is called
Matthew Wild <mwild1@gmail.com>
parents: 4539
diff changeset
53 self.global = true;
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 function api:add_feature(xmlns)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 self:add_item("feature", xmlns);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 function api:add_identity(category, type, name)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 self:add_item("identity", {category = category, type = type, name = name});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function api:add_extension(data)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 self:add_item("extension", data);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function api:fire_event(...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 return (hosts[self.host] or prosody).events.fire_event(...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
70 function api:hook_object_event(object, event, handler, priority)
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
71 self.event_handlers[handler] = { name = event, priority = priority, object = object };
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
72 return object.add_handler(event, handler, priority);
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
73 end
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
74
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 function api:hook(event, handler, priority)
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
76 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 function api:hook_global(event, handler, priority)
4534
7a0a31c4f6c5 modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
Matthew Wild <mwild1@gmail.com>
parents: 4531
diff changeset
80 return self:hook_object_event(prosody.events, event, handler, priority);
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 function api:hook_stanza(xmlns, name, handler, priority)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if not handler and type(name) == "function" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 -- If only 2 options then they specified no xmlns
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 xmlns, name, handler, priority = nil, xmlns, name, handler;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 elseif not (handler and name) then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 self:log("warn", "Error: Insufficient parameters to module:hook_stanza()");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 return;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return self:hook("stanza/"..(xmlns and (xmlns..":") or "")..name, function (data) return handler(data.origin, data.stanza, data); end, priority);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 function api:require(lib)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local f, n = pluginloader.load_code(self.name, lib..".lib.lua");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if not f then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 f, n = pluginloader.load_code(lib, lib..".lib.lua");
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if not f then error("Failed to load plugin library '"..lib.."', error: "..n); end -- FIXME better error message
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 setfenv(f, self.environment);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 return f();
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
4538
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
104 function api:depends(name)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
105 if not self.dependencies then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
106 self.dependencies = {};
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
107 self:hook("module-reloaded", function (event)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
108 if self.dependencies[event.module] then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
109 self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
110 modulemanager.reload(self.host, self.name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
111 return;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
112 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
113 end);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
114 self:hook("module-unloaded", function (event)
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
115 if self.dependencies[event.module] then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
116 self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
117 modulemanager.unload(self.host, self.name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
118 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
119 end);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
120 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
121 local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
122 if not mod then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
123 local err;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
124 mod, err = modulemanager.load(self.host, name);
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
125 if not mod then
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
126 return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) ));
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
127 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
128 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
129 self.dependencies[name] = true;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
130 return mod;
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
131 end
d0a89c1c43fd moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Matthew Wild <mwild1@gmail.com>
parents: 4534
diff changeset
132
4539
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
133 -- Returns one or more shared tables at the specified virtual paths
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
134 -- Intentionally does not allow the table at a path to be _set_, it
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
135 -- is auto-created if it does not exist.
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
136 function api:shared(...)
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
137 local paths = { n = select("#", ...), ... };
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
138 local data_array = {};
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
139 local default_path_components = { self.host, self.name };
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
140 for i = 1, paths.n do
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
141 local path = paths[i];
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
142 if path:sub(1,1) ~= "/" then -- Prepend default components
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
143 local n_components = select(2, path:gsub("/", "%1"));
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
144 path = (n_components<#default_path_components and "/" or "")..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
145 end
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
146 local shared = shared_data[path];
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
147 if not shared then
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
148 shared = {};
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
149 shared_data[path] = shared;
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
150 end
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
151 t_insert(data_array, shared);
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
152 end
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
153 return unpack(data_array);
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
154 end
3cbfa768eb06 moduleapi: Add module:shared(), a way to easily share data between multiple loaded modules
Matthew Wild <mwild1@gmail.com>
parents: 4538
diff changeset
155
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 function api:get_option(name, default_value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 local value = config.get(self.host, self.name, name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 value = config.get(self.host, "core", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 value = default_value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 return value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 function api:get_option_string(name, default_value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local value = self:get_option(name, default_value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 return tostring(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 function api:get_option_number(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 local ret = tonumber(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 if value ~= nil and ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 self:log("error", "Config option '%s' not understood, expecting a number", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 return ret;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 function api:get_option_boolean(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 if type(value) == "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 if #value > 1 then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 value = value[1];
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 local ret = value == true or value == "true" or value == 1 or nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 if ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 ret = (value == false or value == "false" or value == 0);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 if ret then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 ret = false;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 else
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 ret = nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 if ret == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 self:log("error", "Config option '%s' not understood, expecting true/false", name);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 return ret;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 function api:get_option_array(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 local value = self:get_option(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 if type(value) ~= "table" then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return array{ value }; -- Assume any non-list is a single-item list
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 return array():append(value); -- Clone
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 function api:get_option_set(name, ...)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 local value = self:get_option_array(name, ...);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 if value == nil then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 return nil;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 return set.new(value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 local module_items = multitable_new();
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 function api:add_item(key, value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 self.items = self.items or {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 self.items[key] = self.items[key] or {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 t_insert(self.items[key], value);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 self:fire_event("item-added/"..key, {source = self, item = value});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 function api:remove_item(key, value)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 local t = self.items and self.items[key] or NULL;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 for i = #t,1,-1 do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 if t[i] == value then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 t_remove(self.items[key], i);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 self:fire_event("item-removed/"..key, {source = self, item = value});
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 return value;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 function api:get_host_items(key)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 local result = {};
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 for mod_name, module in pairs(modulemanager.get_modules(self.host)) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 module = module.module;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 if module.items then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 for _, item in ipairs(module.items[key] or NULL) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 t_insert(result, item);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 for mod_name, module in pairs(modulemanager.get_modules("*")) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 module = module.module;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 if module.items then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 for _, item in ipairs(module.items[key] or NULL) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 t_insert(result, item);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 return result;
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 function api:handle_items(type, added_cb, removed_cb, existing)
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 self:hook("item-added/"..type, added_cb);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 self:hook("item-removed/"..type, removed_cb);
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 if existing ~= false then
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 for _, item in ipairs(self:get_host_items(type)) do
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 added_cb({ item = item });
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 end
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294
4613
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
295 function api:provides(name, item)
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
296 if not item then item = self.environment; end
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
297 if not item.name then
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
298 local item_name = module.name;
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
299 -- Strip a provider prefix to find the item name
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
300 -- (e.g. "auth_foo" -> "foo" for an auth provider)
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
301 if item_name:find(name.."_", 1, true) == 1 then
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
302 item_name = item_name:sub(#name+2);
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
303 end
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
304 item.name = item_name;
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
305 end
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
306 self:add_item(name, item);
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
307 end
27fbc9c69eb5 moduleapi: Add module:provides(), a shortcut to add an item with the current module's name
Matthew Wild <mwild1@gmail.com>
parents: 4605
diff changeset
308
4531
c778ce7e3c78 modulemanager: Move in-module API functions to core.moduleapi (half the file size, yay)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 return api;