* luci/build: add uvl2i18n helper script
[project/luci.git] / build / cbi2uvl.lua
1 #!/usr/bin/lua
2 local cbi = require "luci.cbi"
3 local i18n = require "luci.i18n"
4 local util = require "luci.util"
5
6 if not arg[1] then
7         util.perror("Usage %s path/to/cbi/model.lua [i18nfilename]" % arg[0])
8         os.exit(1)
9 end
10
11 i18n.load("default", "en")
12 i18n.load("admin-core", "en")
13 i18n.load("wifi", "en")
14
15 if arg[2] then
16         i18n.load(arg[2], "en")
17 end
18
19 local map = cbi.load(arg[1])[1]
20 assert(map)
21
22 print ("package "..map.config)
23 print ("\nconfig package")
24
25 if #map.title > 0 then
26         print ("        option title '%s'" % util.striptags(map.title))
27 end
28
29 if #map.description > 0 then
30         print ("        option description '%s'" % util.striptags(map.description))
31 end
32
33 for i, sec in pairs(map.children) do if util.instanceof(sec, cbi.TypedSection) then
34         print ("\nconfig section")
35         print ("        option name '%s'" % sec.sectiontype)
36         print ("        option package '%s'" % map.config)
37         
38         if #sec.title > 0 then
39                 print ("        option title '%s'" % util.striptags(sec.title))
40         end
41         
42         if #sec.description > 0 then
43                 print ("        option description '%s'" % util.striptags(sec.description))
44         end
45         
46         if not sec.addremove then
47                 print ("        option unique true")
48                 print ("        option required true")
49         end
50         
51         if not sec.anonymous then
52                 print ("        option named true")
53         end
54         
55         if sec.dynamic then
56                 print ("        option dynamic true")
57         end
58         
59         for j, opt in ipairs(sec.children) do
60         if opt.option:sub(1,1) ~= "_" or util.instanceof(opt, cbi.Value) then 
61                 print ("\nconfig variable")
62                 print ("        option name '%s'" % opt.option)
63                 print ("        option section '%s.%s'" % {map.config, sec.sectiontype})
64                 if #opt.title > 0 then
65                         print ("        option title '%s'" % util.striptags(opt.title))
66                 end
67                 
68                 if #opt.description > 0 then
69                         print ("        option description '%s'" % util.striptags(opt.description))
70                 end
71                 
72                 if not opt.rmempty and not opt.optional then
73                         print ("        option required true")
74                 end
75                 
76                 if util.instanceof(opt, cbi.Flag) then
77                         print ("        option datatype boolean")
78                 elseif util.instanceof(opt, cbi.DynamicList) then
79                         print ("        option type list")
80                 elseif util.instanceof(opt, cbi.ListValue) then
81                         print ("        option type enum")
82                         util.perror("*** Warning: Please verify '%s.%s.%s' ***" %
83                                 {map.config, sec.sectiontype, opt.option} )
84                 end
85                 
86                 for i, dep in ipairs(opt.deps) do
87                         if not dep.add or dep.add == "" then
88                                 local depstring
89                                 for k, v in pairs(dep.deps) do
90                                         depstring = (depstring and depstring .. "," or "") .. "%s=%s" % {k, v}
91                                 end 
92                                 print ("        list depends '%s'" % depstring)
93                         else
94                                 util.perror("*** Warning: Unable to decode dependency '%s' in '%s.%s.%s[%s]' ***" %
95                                         {util.serialize_data(dep.deps), map.config, sec.sectiontype, opt.option, dep.add})
96                         end
97                 end
98                 
99                 if util.instanceof(opt, cbi.ListValue) then
100                         for k, key in ipairs(opt.keylist) do
101                                 print ("\nconfig enum")
102                                 print ("        option variable '%s.%s.%s'" % {map.config, sec.sectiontype, opt.option})
103                                 print ("        option value '%s'" % key)
104                                 if opt.vallist[k] and opt.vallist[k] ~= opt.keylist[k] then
105                                         print ("        option title '%s'" % util.striptags(opt.vallist[k]))
106                                 end
107                         end
108                 end
109         end
110         end
111 end end