-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathHook.lua
138 lines (126 loc) · 5.86 KB
/
Hook.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
--------------------------------------------------------------------------
-- A way for client sites to register functions that are defined in
-- SitePackage.lua
-- @classmod Hook
require("strict")
--------------------------------------------------------------------------
-- Lmod License
--------------------------------------------------------------------------
--
-- Lmod is licensed under the terms of the MIT license reproduced below.
-- This means that Lmod is free software and can be used for both academic
-- and commercial purposes at absolutely no cost.
--
-- ----------------------------------------------------------------------
--
-- Copyright (C) 2008-2018 Robert McLay
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject
-- to the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
--------------------------------------------------------------------------
local M={}
local validT =
{
['load'] = {}, -- This load hook is called after a
-- modulefile is loaded.
unload = {}, -- This unload hook is called after a
-- modulefile is unloaded.
parse_updateFn = {}, -- This hook returns the time on the
-- timestamp file.
writeCache = {}, -- This hook return whether a cache
-- should be written.
SiteName = {}, -- Hook to specify Site Name
-- It is used to generate family
-- prefix: site_FAMILY_
msgHook = {}, -- Hook to print messages after:
-- avail, list, spider,
errWarnMsgHook = {}, -- Hook to print messages after LmodError
-- LmodWarning, LmodMessage
groupName = {}, -- This hook adds the arch and os name
-- to moduleT.lua to make it safe on
-- shared filesystems.
avail = {}, -- Map directory names to labels
category = {}, -- Hook to change output of category
restore = {}, -- This hook is run after restore operation
startup = {}, -- This hook is run when Lmod is called
finalize = {}, -- This hook is run just before Lmod generates its output before exiting
packagebasename = {}, -- Hook to find the patterns that spider uses for reverse map
load_spider = {}, -- This hook is run evaluating modules for spider/avail
listHook = {}, -- This hook gets the list of active modules
isVisibleHook = {}, -- Called to evalate if a module should be hidden or not
isForbiddenHook = {}, -- Called to evalate if a module should be forbidden or not
spider_decoration = {}, -- This hook adds decoration to spider level one output.
-- It can be the category or a property.
reverseMapPathFilter = {}, -- This hook returns two arrays keepA, ignoreA to keep or
-- ignore a path in the reverseMap mapping
colorize_fullName = {}, -- Allow module avail and list to colorize name and/or version
}
local s_actionT = { append = true, prepend = true, replace = true }
--------------------------------------------------------------------------
-- Checks for a valid hook name and stores it if valid.
-- @param name The name of the hook.
-- @param func The function to store with it.
-- @param action The kind of action. This is an optional argument.
function M.register(name, func, action)
if (validT[name] == nil) then
LmodWarning{msg="w_Unknown_Hook",name = tostring(name)}
return
end
-- set default for action to be backwards compatible
if (action) then
if (type(action) == "string") then
action = action:lower()
else
action = "append"
end
else
action = "replace"
end
-- Check for a valid action
if (not s_actionT[action]) then
LmodWarning{msg="w_Unknown_Hook_Action",action = tostring(action)}
end
-- Save func depending on action
if (action == "replace") then
validT[name] = {func}
elseif (action == "append") then
validT[name][#validT[name]+1] = func
elseif (action == "prepend") then
table.insert(validT[name],1,func)
end
end
--------------------------------------------------------------------------
-- If a valid hook function has been registered then apply it.
-- @param name The name of the hook.
-- @return the results of the hook if it exists.
function M.apply(name, ...)
if (next(validT[name]) ~= nil) then
local sz = #validT[name]
for i=1,sz-1 do
validT[name][i](...)
end
return validT[name][sz](...)
end
end
function M.exists(name)
return (next(validT[name]) ~= nil) and true or false
end
return M