@@ -4,47 +4,38 @@ local lfs = require('lfs')
44
55local utils = {}
66
7- function utils .starts_with_dot (str )
8- return str :sub (1 , 1 ) == ' .'
9- end
7+ function utils .starts_with_dot (str ) return str :sub (1 , 1 ) == ' .' end
108
119function utils .split (input , sep )
12- if input == ' ' or input == nil then
13- return {}
14- end
15- if sep == nil then
16- sep = ' %s'
17- end
18- local t = {}; local i = 1
19- for str in string.gmatch (input , ' ([^' .. sep .. ' ]+)' ) do
20- t [i ] = str
21- i = i + 1
22- end
23- return t
10+ if input == ' ' or input == nil then return {} end
11+ if sep == nil then sep = ' %s' end
12+ local t = {};
13+ local i = 1
14+ for str in string.gmatch (input , ' ([^' .. sep .. ' ]+)' ) do
15+ t [i ] = str
16+ i = i + 1
17+ end
18+ return t
2419end
2520
2621function utils .basename (path )
27- local parts = utils .split (path , ' /' )
28- return parts [table .getn (parts )]
22+ local parts = utils .split (path , ' /' )
23+ return parts [table .getn (parts )]
2924end
3025
3126function utils .dirname (path )
32- local parts = utils .split (path , ' /' )
33- local returnPath = ' /'
34- local length = table .getn (parts )
35- for i , part in ipairs (parts ) do
36- if i < length then
37- returnPath = returnPath .. part .. ' /'
38- end
39- end
40- return returnPath
27+ local parts = utils .split (path , ' /' )
28+ local returnPath = ' /'
29+ local length = table .getn (parts )
30+ for i , part in ipairs (parts ) do
31+ if i < length then returnPath = returnPath .. part .. ' /' end
32+ end
33+ return returnPath
4134end
4235
4336function utils .add_values_to_set (set , values )
44- for _ , el in pairs (values ) do
45- set [el ] = true
46- end
47- return set
37+ for _ , el in pairs (values ) do set [el ] = true end
38+ return set
4839end
4940
5041-- writes uci section, eg:
@@ -64,148 +55,129 @@ end
6455-- option ifname 'eth0.2'
6556--
6657function utils .write_uci_section (cursor , config , section , merge_list )
67- local name
68- -- add named section
69- if not section [' .anonymous' ] then
70- name = section [' .name' ]
71- cursor :set (config , name , section [' .type' ])
58+ local name
59+ -- add named section
60+ if not section [' .anonymous' ] then
61+ name = section [' .name' ]
62+ cursor :set (config , name , section [' .type' ])
7263 -- add anonymous section
73- else
74- name = cursor :add (config , section [' .type' ])
75- end
76- -- write options for section
77- for key , value in utils .sorted_pairs (section ) do
78- utils .write_uci_option (cursor , config , name , key , value , merge_list )
79- end
64+ else
65+ name = cursor :add (config , section [' .type' ])
66+ end
67+ -- write options for section
68+ for key , value in utils .sorted_pairs (section ) do
69+ utils .write_uci_option (cursor , config , name , key , value , merge_list )
70+ end
8071end
8172
8273-- abstraction for "uci set" which handles corner cases
8374function utils .write_uci_option (cursor , config , name , key , value , merge_list )
84- -- ignore properties starting with .
85- if utils .starts_with_dot (key ) then
86- return
87- end
88- -- avoid duplicate list settings
89- if type (value ) == ' table' and merge_list then
90- -- create set with unique values
91- local set = {}
92- -- read existing value
93- local current = cursor :get (config , name , key )
94- if type (current ) == ' table' then
95- set = utils .add_values_to_set (set , current )
96- end
97- set = utils .add_values_to_set (set , value )
98- -- reset value var with set contents
99- value = {}
100- for item_value , present in pairs (set ) do
101- table.insert (value , item_value )
102- end
103- end
104- cursor :set (config , name , key , value )
75+ -- ignore properties starting with .
76+ if utils .starts_with_dot (key ) then return end
77+ -- avoid duplicate list settings
78+ if type (value ) == ' table' and merge_list then
79+ -- create set with unique values
80+ local set = {}
81+ -- read existing value
82+ local current = cursor :get (config , name , key )
83+ if type (current ) == ' table' then set = utils .add_values_to_set (set , current ) end
84+ set = utils .add_values_to_set (set , value )
85+ -- reset value var with set contents
86+ value = {}
87+ for item_value , present in pairs (set ) do table.insert (value , item_value ) end
88+ end
89+ cursor :set (config , name , key , value )
10590end
10691
10792-- returns true if uci section is empty
10893function utils .is_uci_empty (table )
109- for key , value in pairs (table ) do
110- if not utils .starts_with_dot (key ) then return false end
111- end
112- return true
94+ for key , value in pairs (table ) do
95+ if not utils .starts_with_dot (key ) then return false end
96+ end
97+ return true
11398end
11499
115100-- removes uci options
116101-- and removes section if empty
117102-- this is the inverse operation of `write_uci_section`
118103function utils .remove_uci_options (cursor , config , section )
119- local name = section [' .name' ]
120- -- loop over keys in section and
121- -- remove each one from cursor
122- for key , value in pairs (section ) do
123- if not utils .starts_with_dot (key ) then
124- cursor :delete (config , name , key )
125- end
126- end
127- -- remove entire section if empty
128- local uci = cursor :get_all (config , name )
129- if uci and utils .is_uci_empty (uci ) then
130- cursor :delete (config , name )
131- end
104+ local name = section [' .name' ]
105+ -- loop over keys in section and
106+ -- remove each one from cursor
107+ for key , value in pairs (section ) do
108+ if not utils .starts_with_dot (key ) then cursor :delete (config , name , key ) end
109+ end
110+ -- remove entire section if empty
111+ local uci = cursor :get_all (config , name )
112+ if uci and utils .is_uci_empty (uci ) then cursor :delete (config , name ) end
132113end
133114
134115-- returns true if a table is empty
135116function utils .is_table_empty (t )
136- if next (t ) == nil then
137- return true
138- else
139- return false
140- end
117+ if next (t ) == nil then
118+ return true
119+ else
120+ return false
121+ end
141122end
142123
143124-- Code by David Kastrup
144125-- http://lua-users.org/wiki/DirTreeIterator
145126function utils .dirtree (dir_param )
146- assert (dir_param and dir_param ~= ' ' , ' directory parameter is missing or empty' )
147- if string.sub (dir_param , - 1 ) == ' /' then
148- dir_param = string.sub (dir_param , 1 , - 2 )
149- end
150- local function yieldtree (dir )
151- for entry in lfs .dir (dir ) do
152- if entry ~= ' .' and entry ~= ' ..' then
153- entry = dir .. ' /' .. entry
154- local attr = lfs .attributes (entry )
155- coroutine.yield (entry ,attr )
156- if attr .mode == ' directory' then
157- yieldtree (entry )
158- end
159- end
160- end
127+ assert (dir_param and dir_param ~= ' ' , ' directory parameter is missing or empty' )
128+ if string.sub (dir_param , - 1 ) == ' /' then dir_param = string.sub (dir_param , 1 , - 2 ) end
129+ local function yieldtree (dir )
130+ for entry in lfs .dir (dir ) do
131+ if entry ~= ' .' and entry ~= ' ..' then
132+ entry = dir .. ' /' .. entry
133+ local attr = lfs .attributes (entry )
134+ coroutine.yield (entry , attr )
135+ if attr .mode == ' directory' then yieldtree (entry ) end
136+ end
161137 end
162- return coroutine.wrap (function () yieldtree (dir_param ) end )
138+ end
139+ return coroutine.wrap (function () yieldtree (dir_param ) end )
163140end
164141
165142function utils .file_exists (path )
166- local f = io.open (path , ' r' )
167- if f ~= nil then io.close (f ) return true end
168- return false
143+ local f = io.open (path , ' r' )
144+ if f ~= nil then
145+ io.close (f )
146+ return true
147+ end
148+ return false
169149end
170150
171151function utils .file_to_set (path )
172- local f = io.open (path , ' r' )
173- local set = {}
174- for line in f :lines () do
175- set [line ] = true
176- end
177- return set
152+ local f = io.open (path , ' r' )
153+ local set = {}
154+ for line in f :lines () do set [line ] = true end
155+ return set
178156end
179157
180158function utils .set_to_file (set , path )
181- local f = io.open (path , ' w' )
182- for file , bool in pairs (set ) do
183- f :write (file , ' \n ' )
184- end
185- io.close (f )
186- return true
159+ local f = io.open (path , ' w' )
160+ for file , bool in pairs (set ) do f :write (file , ' \n ' ) end
161+ io.close (f )
162+ return true
187163end
188164
189- function utils .starts_with (str , start )
190- return str :sub (1 , # start ) == start
191- end
165+ function utils .starts_with (str , start ) return str :sub (1 , # start ) == start end
192166
193167-- iterates over a table in alphabeticaly order
194168function utils .sorted_pairs (t )
195- -- collect keys
196- local keys = {}
197- for key in pairs (t ) do keys [# keys + 1 ] = key end
198-
199- table.sort (keys )
200-
201- -- return the iterator function
202- local i = 0
203- return function ()
204- i = i + 1
205- if keys [i ] then
206- return keys [i ], t [keys [i ]]
207- end
208- end
169+ -- collect keys
170+ local keys = {}
171+ for key in pairs (t ) do keys [# keys + 1 ] = key end
172+
173+ table.sort (keys )
174+
175+ -- return the iterator function
176+ local i = 0
177+ return function ()
178+ i = i + 1
179+ if keys [i ] then return keys [i ], t [keys [i ]] end
180+ end
209181end
210182
211183return utils
0 commit comments