<commit_before>#!/usr/bin/lua

--! LibreMesh is modular but this doesn't mean parallel, modules are executed
--! sequencially, so we don't need to worry about transactionality and all other
--! stuff that affects parrallels database, at moment we don't need parallelism
--! as this is just some configuration stuff and is not performance critical.

local libuci = require("uci")

config = {}

config.uci = libuci:cursor()

--! Minimal /etc/config/lime santitizing
function config.sanitize()
	local cf = io.open("/etc/config/lime", "r")
	if (cf == nil) then
		cf = io.open("/etc/config/lime", "w")
		cf:write("")
	end
	cf:close()


	for _,sectName in pairs({"system","network","wifi"}) do
		config.set(sectName, "lime")
	end
end

function config.get(sectionname, option, default)
	local limeconf = config.uci:get("lime", sectionname, option)
	if limeconf then return limeconf end

	local defcnf = config.uci:get("lime-defaults", sectionname, option)
	if ( defcnf ~= nil ) then
		config.set(sectionname, option, defcnf)
	elseif ( default ~= nil ) then
		defcnf = default
		local cfn = sectionname.."."..option
		print("WARNING: Option "..cfn.." is not defined. Using value "..default)
	else
		local cfn = sectionname.."."..option
		print("WARNING: Attempt to access undeclared default for: "..cfn)
		print(debug.traceback())
	end
	return defcnf
end

--! Execute +callback+ for each config of type +configtype+ found in
--! +/etc/config/lime+.
--! beware this function doesn't look in +/etc/config/lime-default+ for default
--! values as it is designed for use with specific sections only
function config.foreach(configtype, callback)
	return config.uci:foreach("lime", configtype, callback)
end

function config.get_all(sectionname)
	local lime_section = config.uci:get_all("lime", sectionname)
	local lime_def_section = config.uci:get_all("lime-defaults", sectionname)

	if lime_section or lime_def_section then
		local ret = lime_section or {}

		if lime_def_section then
			for key,value in pairs(lime_def_section) do
				if (ret[key] == nil) then
					config.set(sectionname, key, value)
					ret[key] = value
				end
			end
		end

		return ret
	end

	return nil
end

function config.get_bool(sectionname, option, default)
	local val = config.get(sectionname, option, default)
	return (val and ((val == '1') or (val == 'on') or (val == 'true') or (val == 'enabled')))
end

config.batched = false

function config.init_batch()
	config.batched = true
end

function config.set(...)
	local aty = type(arg[3])
	if (aty ~= "nil" and aty ~= "string" and aty ~= "table") then
		arg[3] = tostring(arg[3])
	end
	config.uci:set("lime", unpack(arg))
	if(not config.batched) then config.uci:save("lime") end
end

function config.delete(...)
	config.uci:delete("lime", unpack(arg))
	if(not config.batched) then config.uci:save("lime") end
end

function config.end_batch()
	if(config.batched) then
		config.uci:save("lime")
		config.batched = false<commit_msg>config.lua: get_bool & defcnf hotfix<commit_after>#!/usr/bin/lua

--! LibreMesh is modular but this doesn't mean parallel, modules are executed
--! sequencially, so we don't need to worry about transactionality and all other
--! stuff that affects parrallels database, at moment we don't need parallelism
--! as this is just some configuration stuff and is not performance critical.

local libuci = require("uci")

config = {}

config.uci = libuci:cursor()

--! Minimal /etc/config/lime santitizing
function config.sanitize()
	local cf = io.open("/etc/config/lime", "r")
	if (cf == nil) then
		cf = io.open("/etc/config/lime", "w")
		cf:write("")
	end
	cf:close()


	for _,sectName in pairs({"system","network","wifi"}) do
		config.set(sectName, "lime")
	end
end

function config.get(sectionname, option, default)
	local limeconf = config.uci:get("lime", sectionname, option)
	if limeconf then return limeconf end

	local defcnf = config.uci:get("lime-defaults", sectionname, option)
	if ( defcnf ~= nil ) then
		config.set(sectionname, option, defcnf)
	elseif ( default ~= nil ) then
		defcnf = default
		local cfn = sectionname.."."..option
		print("WARNING: Option "..cfn.." is not defined. Using value "..tostring(defcnf))
	else
		local cfn = sectionname.."."..option
		print("WARNING: Attempt to access undeclared default for: "..cfn)
		print(debug.traceback())
	end
	return defcnf
end

--! Execute +callback+ for each config of type +configtype+ found in
--! +/etc/config/lime+.
--! beware this function doesn't look in +/etc/config/lime-default+ for default
--! values as it is designed for use with specific sections only
function config.foreach(configtype, callback)
	return config.uci:foreach("lime", configtype, callback)
end

function config.get_all(sectionname)
	local lime_section = config.uci:get_all("lime", sectionname)
	local lime_def_section = config.uci:get_all("lime-defaults", sectionname)

	if lime_section or lime_def_section then
		local ret = lime_section or {}

		if lime_def_section then
			for key,value in pairs(lime_def_section) do
				if (ret[key] == nil) then
					config.set(sectionname, key, value)
					ret[key] = value
				end
			end
		end

		return ret
	end

	return nil
end

function config.get_bool(sectionname, option, default)
	local val = config.get(sectionname, option, default)
	return (val and ((val == '1') or (val == 'on') or (val == 'true') or (val == 'enabled') or (val == 1) or (val == true)))
end

config.batched = false

function config.init_batch()
	config.batched = true
end

function config.set(...)
	local aty = type(arg[3])
	if (aty ~= "nil" and aty ~= "string" and aty ~= "table") then
		arg[3] = tostring(arg[3])
	end
	config.uci:set("lime", unpack(arg))
	if(not config.batched) then config.uci:save("lime") end
end

function config.delete(...)
	config.uci:delete("lime", unpack(arg))
	if(not config.batched) then config.uci:save("lime") end
end

function config.end_batch()
	if(config.batched) then
		config.uci:save("lime")
		config.batched = false
{'fraction_non_alphanumeric': 0.13647809299983213, 'fraction_numerical': 0.0015108275977841195, 'mean_word_length': 4.300711743772242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '932091', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2023, 'n_words': 610}