build: add script to extract i18n tags from sources and generate *.po snippets
[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("default", "en")
27 i18n.load("admin-core", "en")
28 i18n.load("wifi", "en")
29
30 if arg[2] then
31         i18n.load(arg[2], "en")
32 end
33
34 if arg[3] then
35         pcall(function()
36                 require "uci"
37                 require "luci.model.uci".cursor = function(config, save)
38                         return uci.cursor(config or arg[3] .. "/etc/config", save or arg[3] .. "/tmp/.uci")
39                 end
40         end)
41 end
42
43 local map = cbi.load(arg[1])[1]
44 assert(map)
45
46 print ("package "..map.config)
47 print ("\nconfig package")
48
49 if #map.title > 0 then
50         print ("        option title '%s'" % util.striptags(map.title))
51 end
52
53 if #map.description > 0 then
54         print ("        option description '%s'" % util.striptags(map.description))
55 end
56
57 for i, sec in pairs(map.children) do if util.instanceof(sec, cbi.AbstractSection) then
58         print ("\nconfig section")
59         print ("        option name '%s'" % sec.sectiontype)
60         print ("        option package '%s'" % map.config)
61
62         if #sec.title > 0 then
63                 print ("        option title '%s'" % util.striptags(sec.title))
64         end
65
66         if #sec.description > 0 then
67                 print ("        option description '%s'" % util.striptags(sec.description))
68         end
69
70         if not sec.addremove then
71                 print ("        option unique true")
72                 print ("        option required true")
73         end
74
75         if not sec.anonymous then
76                 print ("        option named true")
77         end
78
79         if sec.dynamic then
80                 print ("        option dynamic true")
81         end
82
83         for j, opt in ipairs(sec.children) do
84         if opt.option:sub(1,1) ~= "_" or util.instanceof(opt, cbi.Value) then
85                 print ("\nconfig variable")
86                 print ("        option name '%s'" % opt.option)
87                 print ("        option section '%s.%s'" % {map.config, sec.sectiontype})
88                 if #opt.title > 0 then
89                         print ("        option title '%s'" % util.striptags(opt.title))
90                 end
91
92                 if #opt.description > 0 then
93                         print ("        option description '%s'" % util.striptags(opt.description))
94                 end
95
96                 if not opt.rmempty and not opt.optional then
97                         print ("        option required true")
98                 end
99
100                 if util.instanceof(opt, cbi.Flag) then
101                         print ("        option datatype boolean")
102                 elseif util.instanceof(opt, cbi.DynamicList) then
103                         print ("        option type list")
104                 elseif util.instanceof(opt, cbi.ListValue) then
105                         print ("        option type enum")
106                         util.perror("*** Warning: Please verify '%s.%s.%s' ***" %
107                                 {map.config, sec.sectiontype, opt.option} )
108                 end
109
110                 for i, dep in ipairs(opt.deps) do
111                         if not dep.add or dep.add == "" then
112                                 local depstring
113                                 for k, v in pairs(dep.deps) do
114                                         depstring = (depstring and depstring .. "," or "") .. "%s=%s" % {k, v}
115                                 end
116                                 print ("        list depends '%s'" % depstring)
117                         else
118                                 util.perror("*** Warning: Unable to decode dependency '%s' in '%s.%s.%s[%s]' ***" %
119                                         {util.serialize_data(dep.deps), map.config, sec.sectiontype, opt.option, dep.add})
120                         end
121                 end
122
123                 if util.instanceof(opt, cbi.ListValue) then
124                         for k, key in ipairs(opt.keylist) do
125                                 print ("\nconfig enum")
126                                 print ("        option variable '%s.%s.%s'" % {map.config, sec.sectiontype, opt.option})
127                                 print ("        option value '%s'" % key)
128                                 if opt.vallist[k] and opt.vallist[k] ~= opt.keylist[k] then
129                                         print ("        option title '%s'" % util.striptags(opt.vallist[k]))
130                                 end
131                         end
132                 end
133         end
134         end
135 end end