• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / Penlight / 478

12 Feb 2024 09:01AM UTC coverage: 89.675% (+0.7%) from 88.938%
478

push

appveyor

Tieske
fix(docs): proper escape back-slash

5489 of 6121 relevant lines covered (89.67%)

357.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/lua/pl/config.lua
1
--- Reads configuration files into a Lua table.
2
--  Understands INI files, classic Unix config files, and simple
3
-- delimited columns of values. See @{06-data.md.Reading_Configuration_Files|the Guide}
4
--
5
--    # test.config
6
--    # Read timeout in seconds
7
--    read.timeout=10
8
--    # Write timeout in seconds
9
--    write.timeout=5
10
--    #acceptable ports
11
--    ports = 1002,1003,1004
12
--
13
--    -- readconfig.lua
14
--    local config = require 'config'
15
--    local t = config.read 'test.config'
16
--    print(pretty.write(t))
17
--
18
--    ### output #####
19
--    {
20
--      ports = {
21
--        1002,
22
--        1003,
23
--        1004
24
--      },
25
--      write_timeout = 5,
26
--      read_timeout = 10
27
--    }
28
--
29
-- @module pl.config
30

31
local type,tonumber,ipairs,io, table = _G.type,_G.tonumber,_G.ipairs,_G.io,_G.table
8✔
32

33
local function split(s,re)
34
    local res = {}
96✔
35
    local t_insert = table.insert
96✔
36
    re = '[^'..re..']+'
96✔
37
    for k in s:gmatch(re) do t_insert(res,k) end
600✔
38
    return res
96✔
39
end
40

41
local function strip(s)
42
    return s:gsub('^%s+',''):gsub('%s+$','')
472✔
43
end
44

45
local function strip_quotes (s)
46
    return s:gsub("['\"](.*)['\"]",'%1')
40✔
47
end
48

49
local config = {}
8✔
50

51
--- like `io.lines`, but allows for lines to be continued with '`\\`'.
52
-- @param file a file-like object (anything where read() returns the next line) or a filename.
53
-- Defaults to standard input.
54
-- @return an iterator over the lines, or nil
55
-- @return error 'not a file-like object' or 'file is nil'
56
function config.lines(file)
8✔
57
    local f,openf,err
58
    local line = ''
128✔
59
    if type(file) == 'string' then
128✔
60
        f,err = io.open(file,'r')
×
61
        if not f then return nil,err end
×
62
        openf = true
×
63
    else
64
        f = file or io.stdin
128✔
65
        if not file.read then return nil, 'not a file-like object' end
128✔
66
    end
67
    if not f then return nil, 'file is nil' end
128✔
68
    return function()
69
        local l = f:read()
520✔
70
        while l do
704✔
71
            -- only for non-blank lines that don't begin with either ';' or '#'
72
            if l:match '%S' and not l:match '^%s*[;#]' then
576✔
73
                -- does the line end with '\'?
74
                local i = l:find '\\%s*$'
400✔
75
                if i then -- if so,
400✔
76
                    line = line..l:sub(1,i-1)
12✔
77
                elseif line == '' then
392✔
78
                    return l
384✔
79
                else
80
                    l = line..l
8✔
81
                    line = ''
8✔
82
                    return l
8✔
83
                end
84
            end
85
            l = f:read()
276✔
86
        end
87
        if openf then f:close() end
128✔
88
    end
89
end
90

91
--- read a configuration file into a table
92
-- @param file either a file-like object or a string, which must be a filename
93
-- @tab[opt] cnfg a configuration table that may contain these fields:
94
--
95
--  * `smart`  try to deduce what kind of config file we have (default false)
96
--  * `variabilize` make names into valid Lua identifiers (default true)
97
--  * `convert_numbers` try to convert values into numbers (default true)
98
--  * `trim_space` ensure that there is no starting or trailing whitespace with values (default true)
99
--  * `trim_quotes` remove quotes from strings (default false)
100
--  * `list_delim` delimiter to use when separating columns (default ',')
101
--  * `keysep` separator between key and value pairs (default '=')
102
--
103
-- @return a table containing items, or `nil`
104
-- @return error message (same as @{config.lines}
105
function config.read(file,cnfg)
8✔
106
    local auto
107

108
    local iter,err = config.lines(file)
128✔
109
    if not iter then return nil,err end
128✔
110
    local line = iter()
128✔
111
    cnfg = cnfg or {}
128✔
112
    if cnfg.smart then
128✔
113
        auto = true
32✔
114
        if line:match '^[^=]+=' then
32✔
115
            cnfg.keysep = '='
8✔
116
        elseif line:match '^[^:]+:' then
24✔
117
            cnfg.keysep = ':'
8✔
118
            cnfg.list_delim = ':'
8✔
119
        elseif line:match '^%S+%s+' then
16✔
120
            cnfg.keysep = ' '
16✔
121
            -- more than two columns assume that it's a space-delimited list
122
            -- cf /etc/fstab with /etc/ssh/ssh_config
123
            if line:match '^%S+%s+%S+%s+%S+' then
16✔
124
                cnfg.list_delim = ' '
8✔
125
            end
126
            cnfg.variabilize = false
16✔
127
        end
128
    end
129

130

131
    local function check_cnfg (var,def)
132
        local val = cnfg[var]
1,024✔
133
        if val == nil then return def else return val end
1,024✔
134
    end
135

136
    local initial_digits = '^[%d%+%-]'
128✔
137
    local t = {}
128✔
138
    local top_t = t
128✔
139
    local variabilize = check_cnfg ('variabilize',true)
128✔
140
    local list_delim = check_cnfg('list_delim',',')
128✔
141
    local convert_numbers = check_cnfg('convert_numbers',true)
128✔
142
    local convert_boolean = check_cnfg('convert_boolean',false)
128✔
143
    local trim_space = check_cnfg('trim_space',true)
128✔
144
    local trim_quotes = check_cnfg('trim_quotes',false)
128✔
145
    local ignore_assign = check_cnfg('ignore_assign',false)
128✔
146
    local keysep = check_cnfg('keysep','=')
128✔
147
    local keypat = keysep == ' ' and '%s+' or '%s*'..keysep..'%s*'
128✔
148
    if list_delim == ' ' then list_delim = '%s+' end
128✔
149

150
    local function process_name(key)
151
        if variabilize then
328✔
152
            key = key:gsub('[^%w]','_')
256✔
153
        end
154
        return key
328✔
155
    end
156

157
    local function process_value(value)
158
        if list_delim and value:find(list_delim) then
880✔
159
            value = split(value,list_delim)
144✔
160
            for i,v in ipairs(value) do
600✔
161
                value[i] = process_value(v)
756✔
162
            end
163
        elseif convert_numbers and value:find(initial_digits) then
784✔
164
            local val = tonumber(value)
328✔
165
            if not val and value:match ' kB$' then
328✔
166
                value = value:gsub(' kB','')
32✔
167
                val = tonumber(value)
32✔
168
            end
169
            if val then value = val end
328✔
170
        elseif convert_boolean and value == 'true' then
456✔
171
           return true
8✔
172
        elseif convert_boolean and value == 'false' then
448✔
173
           return false
8✔
174
        end
175
        if type(value) == 'string' then
864✔
176
           if trim_space then value = strip(value) end
708✔
177
           if not trim_quotes and auto and value:match '^"' then
472✔
178
                trim_quotes = true
8✔
179
            end
180
           if trim_quotes then value = strip_quotes(value) end
492✔
181
        end
182
        return value
864✔
183
    end
184

185
    while line do
520✔
186
        if line:find('^%[') then -- section!
392✔
187
            local section = process_name(line:match('%[([^%]]+)%]'))
16✔
188
            t = top_t
16✔
189
            t[section] = {}
16✔
190
            t = t[section]
16✔
191
        else
192
            line = line:gsub('^%s*','')
376✔
193
            local i1,i2 = line:find(keypat)
376✔
194
            if i1 and not ignore_assign then -- key,value assignment
376✔
195
                local key = process_name(line:sub(1,i1-1))
468✔
196
                local value = process_value(line:sub(i2+1))
468✔
197
                t[key] = value
312✔
198
            else -- a plain list of values...
199
                t[#t+1] = process_value(line)
96✔
200
            end
201
        end
202
        line = iter()
588✔
203
    end
204
    return top_t
128✔
205
end
206

207
return config
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc