[luci] what is .write and .cfgvalue?

Seamus Tuohy s2e at opentechinstitute.org
Mon Feb 10 15:55:14 CET 2014


Hey Dat,

There really is no documentation other than the code. I wrote a short
overview of the core functionality of these object below. But, both of
those really depend upon which cbi object you are interacting with. On
the device you can look in /usr/lib/lua/luci/cbi.lua or at
luci-0.11/libs/web/luasrc/cbi.lua in the repo to find the code for the
object you are interacting with.

---- CFGVALUE ----

cfgvalue is function that returns the uci value of the chosen object
from its config file. On a section it will run a uci cursor() get_all on
that section and return it. On a value, after some error checking and
the like it will return the results of a uci cursor() get. On a value
this can return a string or a table depending upon the value returned
from the uci file.

It can be called in either of the following ways

val = object:cfgvalue(section)

val object.cfgvalue(self, section)

This is run at the start of the parse function to populate and evaluate
your cbi page. As such, the section value here is usually pre-populated
by the parse function.

If you wish to modify what value a specific object receives when it
checks its config file (for instance if you were to want to write a uci
page that writes to a different config file type) you would do it as such.

--just setting up a basic cbi file.
m = map("network", "network")
s = m:section(NamedSection, "wired")
v = s:option(Value, "dhcp", "DHCP")

--And here we subvert how it grabs the value from the uci file.

function v.cfgvalue(self, section)
	configVal = mySpecialConfigParser.get(section, self.option)
	if configVal then
		return configVal
	else
		return nil
	end
end


---- WRITE ----

write is the value level option for writing the changes received from
the page into the uci file. (Section uses the function .create for this,
but it behaves in a totally different way.) Write is ususally abstracted
from the AbstractValue implementation for all other classes. As such on
MOST occations write simple runs a uci cursor() set command using the
value and section it is given and the option of the object it is writing
from.

It can be called in either of the following ways. ( I apologize if I am
simplifying this too much by highlighting the different ways to pass
self to a function, just trying to be thorough)

result = object:write(section, value)

result = object.write(self, section, value)


Write will return a boolean result of whether the operation succeeded or
not.

If you want to extend write (for instance to have it now write to the
custom config file type above) you could do it as such.

--following from above
function v.write(self, section, value)
	if mySpecialConfigParser.set(section, self.option, value) then
		return True
	else
		return nil
	end
end

The one value that implements its own write function is the DynamicList
value. If you are going to hijack that function make sure you go in the
code and read what it is doing so that you don't cause yourself more
headaches in the process.


In both of these examples I have chosen to simply extend the existing
value so that they will be called from the appropriate place in their
parse function. You can do crazier things, but if you do, you are
probably working too hard.


Hope that helps,

s2e

On 02/09/2014 11:41 PM, Le Tran Dat wrote:
> Hi list,
> 
> where is the document for .write and .cfgvalue? I am trying to do an
> immediate action if users check or uncheck an checkbox.
> 
> I think to implement this I need to deal with .write and .cfgvalue but
> I couldn't find any information of those functions.
> 
> Thanks,
> Dat
> _______________________________________________
> luci mailing list
> luci at lists.subsignal.org
> https://lists.subsignal.org/mailman/listinfo/luci
> 


More information about the luci mailing list