Making a channel specific configuration file

This is a technique for making it easy to load and save a configuration file that is associated with a channel. I’ve done it in a relatively elaborate manner.

The config module below

  1. It looks up the GUID for the script in the configuration file (taking into account the type of Translator module).
  2. Tries to load GUID.xml and parse the XML if it can.
  3. If not it returns a default XML document as supplied by the caller.

It makes for a convenient way to associate a configuration file with a Translator instance.

Here it is in action:

And here is the code to copy:

require 'config'

local function trace(a,b,c,d) return end

Default = [[
<llp_client
 port='5123'
 host='localhost'
/>
]]

function main()
   local C, Tok = config.File(config.TO, Default)

   C.llp_client.port = 6000

   config.Save(Tok, C)
end

You can see how convenient the annotations and auto-completion make using the resulting configuration file:

Where this is all headed is providing the foundation tools to make it possible to write complete components in Lua that have their own configuration interfaces.

Here’s the source code to the config module:

require('node')

config={}

local function ReadFile(FileName)
   local F = io.open(FileName)
   local X = F:read("*all")
   F:close()
   return X
end

local function WriteFile(FileName, Content)
   local F = io.open(FileName, "w") 
   F:write(Content)
   F:close()
end

function node.getChannel(C, Name) 
   for i=1,#C.iguana_config.channel_config do
      local Chan = C.iguana_config.channel_config[i]
      if Chan.name:nodeValue() == Name then
         return Chan
      end
   end
   return nil
end

config.FROM = 1
config.TO = 2
config.FILTER = 3 
config.ACK = 4

function ScriptGuid(ScriptType, ChannelName, Config)
   if ScriptType == config.TO then return Config:getChannel(ChannelName).to_mapper.guid:nodeValue() end
   if ScriptType == config.FROM then return Config:getChannel(ChannelName).from_mapper.guid:nodeValue() end
   if ScriptType == config.FILTER then return Config:getChannel(ChannelName).message_filter.translator_guid:nodeValue() end
   if ScriptType == config.ACK then return Config:getChannel(ChannelName).llp_listener.ack_script:nodeValue() end
end

function config.ScriptGuid(ScriptType)
   if not ScriptType then ScriptType = config.TO end
   local X = ReadFile('IguanaConfiguration.xml')
   X = xml.parse{data=X}
   local Guid = ScriptGuid(ScriptType, iguana.channelName(), X)
   return Guid
end

function config.File(ScriptType, Default)
   local G = config.ScriptGuid(ScriptType)
   local F = io.open(G..'.xml', 'r')
   local D = Default
   if F then
      D = F:read("*all")
      F:close()
   end
   return xml.parse{data=D}, G
end
function config.Save(Guid, Config)
   local Content = Config:S()
   WriteFile(Guid..'.xml', Content)
end

Leave A Comment?