# HG changeset patch # User Matthew Wild # Date 1679572291 0 # Node ID 433a1f36d0d32918c57721abc776c8d5e6383b9a # Parent db73c4c317cef9e47f79b9b8652807da9c9be3e8 client: Move some generic utility functions to a helpers module diff -r db73c4c317ce -r 433a1f36d0d3 scansion/helpers.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scansion/helpers.lua Thu Mar 23 11:51:31 2023 +0000 @@ -0,0 +1,30 @@ +local function filter_expression(script, s) + local expr = s:match("^%$%{(.+)%}$"); + if not expr then return s end + local name, value_name = expr:match("^(.+)'s (.+)$"); + assert(name, "Unable to parse expression: "..expr); + local key = value_name:lower():gsub(" ", "_"); + assert(script.objects[name], "Unknown object called "..name); + local value = script.objects[name][key]; + assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name); + return value; +end + +local function fill_vars(script, stanza) + for k, v in pairs(stanza.attr) do + stanza.attr[k] = filter_expression(script, v); + end + for i, child in ipairs(stanza) do + if type(child) == "string" then + stanza[i] = filter_expression(script, child); + elseif type(child) == "table" then + fill_vars(script, child); + end + end + return stanza; +end + +return { + filter_expression = filter_expression; + fill_vars = fill_vars; +}; diff -r db73c4c317ce -r 433a1f36d0d3 scansion/objects/client.lua --- a/scansion/objects/client.lua Thu Mar 23 11:43:25 2023 +0000 +++ b/scansion/objects/client.lua Thu Mar 23 11:51:31 2023 +0000 @@ -8,31 +8,7 @@ local stanzacmp = require "scansion.stanzacmp"; -local function filter_expression(script, s) - local expr = s:match("^%$%{(.+)%}$"); - if not expr then return s end - local name, value_name = expr:match("^(.+)'s (.+)$"); - assert(name, "Unable to parse expression: "..expr); - local key = value_name:lower():gsub(" ", "_"); - assert(script.objects[name], "Unknown object called "..name); - local value = script.objects[name][key]; - assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name); - return value; -end - -local function fill_vars(script, stanza) - for k, v in pairs(stanza.attr) do - stanza.attr[k] = filter_expression(script, v); - end - for i, child in ipairs(stanza) do - if type(child) == "string" then - stanza[i] = filter_expression(script, child); - elseif type(child) == "table" then - fill_vars(script, child); - end - end - return stanza; -end +local helpers = require "scansion.helpers"; return { _validate = function (client) @@ -81,7 +57,7 @@ end; sends = function (client, data) - local stanza = fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " "))))); + local stanza = helpers.fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " "))))); local wait, done = async.waiter(); local function handle_drained() client.stream:unhook("drained", handle_drained); @@ -98,7 +74,7 @@ local have_received_stanza = false; data = table.concat(data):gsub("\t", " "):gsub("^%s+", ""):gsub("%s+$", ""); if data ~= "nothing" then - expected_stanza = fill_vars(client.script, assert(parse_xml(data))); + expected_stanza = helpers.fill_vars(client.script, assert(parse_xml(data))); end local function stanza_handler(received_stanza) have_received_stanza = true; diff -r db73c4c317ce -r 433a1f36d0d3 squishy --- a/squishy Thu Mar 23 11:43:25 2023 +0000 +++ b/squishy Thu Mar 23 11:51:31 2023 +0000 @@ -6,6 +6,7 @@ Module "scansion.async" Module "scansion.error" Module "scansion.parser" +Module "scansion.helpers" Module "scansion.iterators" Module "scansion.objects.client" Module "scansion.queue"