luat-sta.lua /size: 5608 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['luat-sta'] = {
2    version   = 1.001,
3    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
4    copyright = "PRAGMA ADE / ConTeXt Development Team",
5    license   = "see context related readme files"
6}
7
8-- this code is used in the updater
9
10local gmatch, match = string.gmatch, string.match
11local type = type
12
13states          = states or { }
14local states    = states
15
16states.data     = states.data or { }
17local data      = states.data
18
19states.hash     = states.hash or { }
20local hash      = states.hash
21
22states.tag      = states.tag      or ""
23states.filename = states.filename or ""
24
25function states.save(filename,tag)
26    tag = tag or states.tag
27    filename = file.addsuffix(filename or states.filename,'lus')
28    io.savedata(filename,
29        "-- generator : luat-sta.lua\n" ..
30        "-- state tag : " .. tag .. "\n\n" ..
31        table.serialize(data[tag or states.tag] or {},true)
32    )
33end
34
35function states.load(filename,tag)
36    states.filename = filename
37    states.tag = tag or "whatever"
38    states.filename = file.addsuffix(states.filename,'lus')
39    data[states.tag], hash[states.tag] = (io.exists(filename) and dofile(filename)) or { }, { }
40end
41
42local function set_by_tag(tag,key,value,default,persistent)
43    local d, h = data[tag], hash[tag]
44    if d then
45        if type(d) == "table" then
46            local dkey, hkey = key, key
47            local pre, post = match(key,"(.+)%.([^%.]+)$")
48            if pre and post then
49                for k in gmatch(pre,"[^%.]+") do
50                    local dk = d[k]
51                    if not dk then
52                        dk = { }
53                        d[k] = dk
54                    elseif type(dk) == "string" then
55                        -- invalid table, unable to upgrade structure
56                        -- hope for the best or delete the state file
57                        break
58                    end
59                    d = dk
60                end
61                dkey, hkey = post, key
62            end
63            if value == nil then
64                value = default
65            elseif value == false then
66                -- special case
67            elseif persistent then
68                value = value or d[dkey] or default
69            else
70                value = value or default
71            end
72            d[dkey], h[hkey] = value, value
73        elseif type(d) == "string" then
74            -- weird
75            data[tag], hash[tag] = value, value
76        end
77    end
78end
79
80local function get_by_tag(tag,key,default)
81    local h = hash[tag]
82    if h and h[key] then
83        return h[key]
84    else
85        local d = data[tag]
86        if d then
87            for k in gmatch(key,"[^%.]+") do
88                local dk = d[k]
89                if dk ~= nil then
90                    d = dk
91                else
92                    return default
93                end
94            end
95            if d == false then
96                return false
97            else
98                return d or default
99            end
100        end
101    end
102end
103
104states.set_by_tag = set_by_tag
105states.get_by_tag = get_by_tag
106
107function states.set(key,value,default,persistent)
108    set_by_tag(states.tag,key,value,default,persistent)
109end
110
111function states.get(key,default)
112    return get_by_tag(states.tag,key,default)
113end
114
115-- data.update = {
116-- 	["version"] = {
117-- 		["major"] = 0,
118-- 		["minor"] = 1,
119-- 	},
120-- 	["rsync"] = {
121-- 		["server"]     = "contextgarden.net",
122-- 		["module"]     = "minimals",
123-- 		["repository"] = "current",
124-- 		["flags"]      = "-rpztlv --stats",
125-- 	},
126-- 	["tasks"] = {
127-- 		["update"] = true,
128-- 		["make"]   = true,
129--         ["delete"] = false,
130-- 	},
131-- 	["platform"] = {
132-- 		["host"]  = true,
133-- 		["other"] = {
134-- 			["mswin"]     = false,
135-- 			["linux"]     = false,
136-- 			["linux-64"]  = false,
137-- 			["osx-intel"] = false,
138-- 			["osx-ppc"]   = false,
139-- 			["sun"]       = false,
140-- 		},
141-- 	},
142-- 	["context"] = {
143-- 		["available"] = {"current", "beta", "alpha", "experimental"},
144-- 		["selected"]  = "current",
145-- 	},
146-- 	["formats"] = {
147-- 		["cont-en"] = true,
148-- 		["cont-nl"] = true,
149-- 		["cont-de"] = false,
150-- 		["cont-cz"] = false,
151-- 		["cont-fr"] = false,
152-- 		["cont-ro"] = false,
153-- 	},
154-- 	["engine"] = {
155-- 		["pdftex"] = {
156-- 			["install"] = true,
157-- 			["formats"] = {
158-- 				["pdftex"] = true,
159-- 			},
160-- 		},
161-- 		["luatex"] = {
162-- 			["install"] = true,
163-- 			["formats"] = {
164-- 			},
165-- 		},
166-- 		["xetex"] = {
167-- 			["install"] = true,
168-- 			["formats"] = {
169-- 				["xetex"] = false,
170-- 			},
171-- 		},
172-- 		["metapost"] = {
173-- 			["install"] = true,
174-- 			["formats"] = {
175-- 				["mpost"] = true,
176-- 				["metafun"] = true,
177-- 			},
178-- 		},
179-- 	},
180-- 	["fonts"] = {
181-- 	},
182-- 	["doc"] = {
183-- 	},
184-- 	["modules"] = {
185-- 		["f-urwgaramond"] = false,
186-- 		["f-urwgothic"] = false,
187-- 		["t-bnf"] = false,
188-- 		["t-chromato"] = false,
189-- 		["t-cmscbf"] = false,
190-- 		["t-cmttbf"] = false,
191-- 		["t-construction-plan"] = false,
192-- 		["t-degrade"] = false,
193-- 		["t-french"] = false,
194-- 		["t-lettrine"] = false,
195-- 		["t-lilypond"] = false,
196-- 		["t-mathsets"] = false,
197-- 		["t-tikz"] = false,
198-- 		["t-typearea"] = false,
199-- 		["t-vim"] = false,
200-- 	},
201-- }
202
203-- states.save("teststate", "update")
204-- states.load("teststate", "update")
205
206-- print(states.get_by_tag("update","rsync.server","unknown"))
207-- states.set_by_tag("update","rsync.server","oeps")
208-- print(states.get_by_tag("update","rsync.server","unknown"))
209-- states.save("teststate", "update")
210-- states.load("teststate", "update")
211-- print(states.get_by_tag("update","rsync.server","unknown"))
212