<commit_before>module(..., package.seeall)

-- global
XSPS = class(function(x)
	x.actions = { install = "InstallAction" }
end)

function XSPS:action(action, template)
	if self.actions[action] then
		local c = self.actions[action]
		-- TODO: load template here and pass to new()
		local a = new(c, get_class("Action"), template)
		return a
	else
		log:err("unknown action `"..action.."'. aborting.")
		os.exit(1)
	end
end

function XSPS:format(str, tbl)
	str = string.gsub(str, "%${(%w+)}", os.getenv)
	str = string.gsub(str, "%$(%w+)", os.getenv)
	str = string.gsub(str, "%${(%w+)}", tbl)
	str = string.gsub(str, "%$(%w+)", tbl)
	return str
end

function XSPS:set_config(file)
	local res, err, conf
	log:debug("using configuration file `"..file.."'")
	if not posix.access(file, "r") then
		log:err("cannot open configuration file `"..file.."'")
		os.exit(1)
	else
		res, err = loadfile(file)
		if err then
			log:err("an error occurred while reading `"..file.."'")
			os.exit(1)
		end
	end
	conf = res()
	self.config = {}
	table.foreach(conf, function(k,v)
		if type(v) == "string" then
			local value = self:format(v, conf)
			self.config[k] = value
		end
	end)
end
<commit_msg>XSPS: change 'x' to 'self' in constructor<commit_after>module(..., package.seeall)

-- global
XSPS = class(function(self)
	self.actions = { install = "InstallAction" }
end)

function XSPS:action(action, template)
	if self.actions[action] then
		local c = self.actions[action]
		-- TODO: load template here and pass to new()
		local a = new(c, get_class("Action"), template)
		return a
	else
		log:err("unknown action `"..action.."'. aborting.")
		os.exit(1)
	end
end

function XSPS:format(str, tbl)
	str = string.gsub(str, "%${(%w+)}", os.getenv)
	str = string.gsub(str, "%$(%w+)", os.getenv)
	str = string.gsub(str, "%${(%w+)}", tbl)
	str = string.gsub(str, "%$(%w+)", tbl)
	return str
end

function XSPS:set_config(file)
	local res, err, conf
	log:debug("using configuration file `"..file.."'")
	if not posix.access(file, "r") then
		log:err("cannot open configuration file `"..file.."'")
		os.exit(1)
	else
		res, err = loadfile(file)
		if err then
			log:err("an error occurred while reading `"..file.."'")
			os.exit(1)
		end
	end
	conf = res()
	self.config = {}
	table.foreach(conf, function(k,v)
		if type(v) == "string" then
			local value = self:format(v, conf)
			self.config[k] = value
		end
	end)
end

{'fraction_non_alphanumeric': 0.18770903010033446, 'fraction_numerical': 0.002508361204013378, 'mean_word_length': 3.913757700205339, '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': '5168753', 'n_tokens_mistral': 976, 'n_tokens_neox': 914, 'n_words': 242}