3
Generic module to create INI files lenses
5
Author: Raphael Pinson <raphink@gmail.com>
8
This file is licensed under the LGPLv2+, like the rest of Augeas.
11
Things to add in the future
12
- Support double quotes in value
13
- Support multiline values (is it standard?)
16
This lens is made to provide generic primitives to construct INI File lenses.
17
See <Puppet>, <PHP>, <MySQL> or <Dput> for examples of real life lenses using it.
24
(************************************************************************
25
* Group: USEFUL PRIMITIVES
26
*************************************************************************)
28
(* Group: Internal primitives *)
32
End of line, inherited from <Util.eol>
38
Empty line, an <eol> subnode
43
(* Group: Separators *)
52
pat:regexp - the pattern to delete
53
default:string - the default string to use
55
let sep (pat:regexp) (default:string)
56
= Sep.opt_space . del pat default
60
The default regexp for a separator
67
The default separator value
77
Store until end of line
79
let sto_to_eol = Sep.opt_space . store Rx.space_in
82
Variable: sto_to_comment
85
let sto_to_comment = Sep.opt_space
86
. store /[^;# \t\n][^;#\n]*[^;# \t\n]|[^;# \t\n]/
89
(* Group: Define comment and defaults *)
93
Map comments into "#comment" nodes
96
pat:regexp - pattern to delete before commented data
97
default:string - default pattern before commented data
101
let comment = IniFile.comment "#" "#"
102
let comment = IniFile.comment IniFile.comment_re IniFile.comment_default
105
let comment (pat:regexp) (default:string)
106
= [ label "#comment" . sep pat default
107
. sto_to_eol? . eol ]
110
Default regexp for <comment> pattern
113
let comment_re = /[;#]/
116
Variable: comment_default
117
Default value for <comment> pattern
119
let comment_default = ";"
122
(************************************************************************
124
*************************************************************************)
126
(* Group: entry includes comments *)
130
Generic INI File entry
133
kw:regexp - keyword regexp for the label
134
sep:lens - lens to use as key/value separator
135
comment:lens - lens to use as comment
138
> let entry = IniFile.entry setting sep comment
140
let entry (kw:regexp) (sep:lens) (comment:lens)
141
= [ key kw . sep . sto_to_comment? . (comment|eol) ] | comment
145
Generic INI File entry that might be indented with an arbitrary
149
kw:regexp - keyword regexp for the label
150
sep:lens - lens to use as key/value separator
151
comment:lens - lens to use as comment
154
> let entry = IniFile.indented_entry setting sep comment
156
let indented_entry (kw:regexp) (sep:lens) (comment:lens)
157
= [ Util.del_opt_ws "" .
158
key kw . sep . sto_to_comment? .
165
Default regexp for <entry> keyword
167
let entry_re = ( /[A-Za-z][A-Za-z0-9\._-]+/ )
170
(************************************************************************
172
*************************************************************************)
174
(* Group: Title definition *)
178
Title for <record>. This maps the title of a record as a node in the abstract tree.
181
kw:regexp - keyword regexp for the label
184
> let title = IniFile.title IniFile.record_re
186
let title (kw:regexp)
187
= Util.del_str "[" . key kw
188
. Util.del_str "]". eol
192
Title for <record>. This maps the title of a record as a node in the abstract tree. The title may be indented with arbitrary amounts of whitespace
195
kw:regexp - keyword regexp for the label
198
> let title = IniFile.title IniFile.record_re
200
let indented_title (kw:regexp)
201
= Util.indent . title kw
205
Title for <record>. This maps the title of a record as a value in the abstract tree.
208
name:string - name for the title label
209
kw:regexp - keyword regexp for the label
212
> let title = IniFile.title_label "target" IniFile.record_label_re
214
let title_label (name:string) (kw:regexp)
216
. Util.del_str "[" . store kw
217
. Util.del_str "]". eol
220
View: indented_title_label
221
Title for <record>. This maps the title of a record as a value in the abstract tree. The title may be indented with arbitrary amounts of whitespace
224
name:string - name for the title label
225
kw:regexp - keyword regexp for the label
228
> let title = IniFile.title_label "target" IniFile.record_label_re
230
let indented_title_label (name:string) (kw:regexp)
231
= Util.indent . title_label name kw
236
Default regexp for <title> keyword pattern
238
let record_re = ( /[^]\n\/]+/ - /#comment/ )
241
Variable: record_label_re
242
Default regexp for <title_label> keyword pattern
244
let record_label_re = /[^]\n]+/
247
(* Group: Record definition *)
251
INI File Record with no empty lines allowed.
254
title:lens - lens to use for title. Use either <title> or <title_label>.
255
entry:lens - lens to use for entries in the record. See <entry>.
257
let record_noempty (title:lens) (entry:lens)
263
Generic INI File record
266
title:lens - lens to use for title. Use either <title> or <title_label>.
267
entry:lens - lens to use for entries in the record. See <entry>.
270
> let record = IniFile.record title entry
272
let record (title:lens) (entry:lens)
273
= record_noempty title ( entry | empty )
276
(************************************************************************
278
*************************************************************************)
283
Group: Lens definition
286
Generic INI File lens with no empty lines
289
record:lens - record lens to use. See <record_noempty>.
290
comment:lens - comment lens to use. See <comment>.
293
> let lns = IniFile.lns_noempty record comment
295
let lns_noempty (record:lens) (comment:lens)
300
Generic INI File lens
303
record:lens - record lens to use. See <record>.
304
comment:lens - comment lens to use. See <comment>.
307
> let lns = IniFile.lns record comment
309
let lns (record:lens) (comment:lens)
310
= lns_noempty record (comment|empty)