applications/luci-splash: Fix typo
[project/luci.git] / build / cbi2uvl.lua
1 #!/usr/bin/lua
2 --[[
3 LuCI - Lua Configuration Interface
4
5 Copyright 2008 Steven Barth <steven@midlink.org>
6 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12         http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id: index.lua 3548 2008-10-09 20:28:07Z Cyrus $
15 ]]--
16
17 local cbi = require "luci.cbi"
18 local i18n = require "luci.i18n"
19 local util = require "luci.util"
20
21 if not arg[1] then
22         util.perror("Usage %s path/to/cbi/model.lua [i18nfilename]" % arg[0])
23         os.exit(1)
24 end
25
26 i18n.load("base", "en")
27
28 if arg[2] then
29         i18n.load(arg[2], "en")
30 end
31
32 if arg[3] then
33         pcall(function()
34                 require "uci"
35                 require "luci.model.uci".cursor = function(config, save)
36                         return uci.cursor(config or arg[3] .. "/etc/config", save or arg[3] .. "/tmp/.uci")
37                 end
38         end)
39 end
40
41 local map = cbi.load(arg[1])[1]
42 assert(map)
43
44 print ("package "..map.config)
45 print ("\nconfig package")
46
47 if #map.title > 0 then
48         print ("        option title '%s'" % util.striptags(map.title))
49 end
50
51 if #map.description > 0 then
52         print ("        option description '%s'" % util.striptags(map.description))
53 end
54
55 for i, sec in pairs(map.children) do if util.instanceof(sec, cbi.AbstractSection) then
56         print ("\nconfig section")
57         print ("        option name '%s'" % sec.sectiontype)
58         print ("        option package '%s'" % map.config)
59
60         if #sec.title > 0 then
61                 print ("        option title '%s'" % util.striptags(sec.title))
62         end
63
64         if #sec.description > 0 then
65                 print ("        option description '%s'" % util.striptags(sec.description))
66         end
67
68         if not sec.addremove then
69                 print ("        option unique true")
70                 print ("        option required true")
71         end
72
73         if not sec.anonymous then
74                 print ("        option named true")
75         end
76
77         if sec.dynamic then
78                 print ("        option dynamic true")
79         end
80
81         for j, opt in ipairs(sec.children) do
82         if opt.option:sub(1,1) ~= "_" or util.instanceof(opt, cbi.Value) then
83                 print ("\nconfig variable")
84                 print ("        option name '%s'" % opt.option)
85                 print ("        option section '%s.%s'" % {map.config, sec.sectiontype})
86                 if #opt.title > 0 then
87                         print ("        option title '%s'" % util.striptags(opt.title))
88                 end
89
90                 if #opt.description > 0 then
91                         print ("        option description '%s'" % util.striptags(opt.description))
92                 end
93
94                 if not opt.rmempty and not opt.optional then
95                         print ("        option required true")
96                 end
97
98                 if util.instanceof(opt, cbi.Flag) then
99                         print ("        option datatype boolean")
100                 elseif util.instanceof(opt, cbi.DynamicList) then
101                         print ("        option type list")
102                 elseif util.instanceof(opt, cbi.ListValue) then
103                         print ("        option type enum")
104                         util.perror("*** Warning: Please verify '%s.%s.%s' ***" %
105                                 {map.config, sec.sectiontype, opt.option} )
106                 end
107
108                 for i, dep in ipairs(opt.deps) do
109                         if not dep.add or dep.add == "" then
110                                 local depstring
111                                 for k, v in pairs(dep.deps) do
112                                         depstring = (depstring and depstring .. "," or "") .. "%s=%s" % {k, v}
113                                 end
114                                 print ("        list depends '%s'" % depstring)
115                         else
116                                 util.perror("*** Warning: Unable to decode dependency '%s' in '%s.%s.%s[%s]' ***" %
117                                         {util.serialize_data(dep.deps), map.config, sec.sectiontype, opt.option, dep.add})
118                         end
119                 end
120
121                 if util.instanceof(opt, cbi.ListValue) then
122                         for k, key in ipairs(opt.keylist) do
123                                 print ("\nconfig enum")
124                                 print ("        option variable '%s.%s.%s'" % {map.config, sec.sectiontype, opt.option})
125                                 print ("        option value '%s'" % key)
126                                 if opt.vallist[k] and opt.vallist[k] ~= opt.keylist[k] then
127                                         print ("        option title '%s'" % util.striptags(opt.vallist[k]))
128                                 end
129                         end
130                 end
131         end
132         end
133 end end