Generic module to create INI files lenses
Author: Raphael Pinson rap@gmai l.com hink
IniFile | Generic module to create INI files lenses |
License | This file is licensed under the LGPL v2+, like the rest of Augeas. |
TODO | Things to add in the future |
Lens usage | This lens is made to provide generic primitives to construct INI File lenses. |
Examples | The Test_IniFile file contains various examples and tests. |
USEFUL PRIMITIVES | |
Internal primitives | |
eol | End of line, inherited from Util.eol |
empty | Empty line, an eol subnode |
Separators | |
sep | Generic separator |
sep_noindent | Generic separator, no indentation |
sep_re | The default regexp for a separator |
sep_default | The default separator value |
Stores | |
sto_to_eol | Store until end of line |
to_comment_re | Regex until comment |
sto_to_comment | Store until comment |
sto_multiline | Store multiline values |
sto_multiline_nocomment | Store multiline values without an end-of-line comment |
Define comment and defaults | |
comment | Map comments into “#comment” nodes |
comment_re | Default regexp for comment pattern |
comment_default | Default value for comment pattern |
ENTRY | |
entry includes comments | |
entry | Generic INI File entry |
entry_multiline | Generic multiline INI File entry |
entry_multiline_nocomment | Generic multiline INI File entry without an end-of-line comment |
indented_entry | Generic INI File entry that might be indented with an arbitrary amount of whitespace |
entry_list | Generic INI File list entry |
entry_list_nocomment | Generic INI File list entry without an end-of-line comment |
entry_re | Default regexp for entry keyword |
RECORD | |
Title definition | |
title | Title for record. |
indented_title | Title for record. |
title_label | Title for record. |
indented_title_label | Title for record. |
record_re | Default regexp for title keyword pattern |
record_label_re | Default regexp for title_label keyword pattern |
Record definition | |
record_noempty | INI File Record with no empty lines allowed. |
record | Generic INI File record |
LENS | |
Lens definition | |
lns_noempty | Generic INI File lens with no empty lines |
lns | Generic INI File lens |
The Test_IniFile file contains various examples and tests.
let eol = Util.eol
End of line, inherited from Util.eol
let empty = [ eol ]
Empty line, an eol subnode
let comment (pat:regexp) (default:string) = [ label "#comment" . sep pat default . sto_to_eol? . eol ]
Map comments into “#comment” nodes
pat:regexp | pattern to delete before commented data |
default:string | default pattern before commented data |
let comment = IniFile.comment "#" "#" let comment = IniFile.comment IniFile.comment_re IniFile.comment_default
let comment_re = /[;#]/
Default regexp for comment pattern
let comment_default = ";"
Default value for comment pattern
let entry (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic INI File entry
kw:regexp | keyword regexp for the label |
sep:lens | lens to use as key/value separator |
comment:lens | lens to use as comment |
let entry = IniFile.entry setting sep comment
let entry_multiline_nocomment (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . sto_multiline_nocomment? . eol ] | comment
Generic multiline INI File entry without an end-of-line comment
kw:regexp | keyword regexp for the label |
sep:lens | lens to use as key/value separator |
comment:lens | lens to use as comment |
let indented_entry (kw:regexp) (sep:lens) (comment:lens) = [ Util.del_opt_ws "" . key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic INI File entry that might be indented with an arbitrary amount of whitespace
kw:regexp | keyword regexp for the label |
sep:lens | lens to use as key/value separator |
comment:lens | lens to use as comment |
let entry = IniFile.indented_entry setting sep comment
let entry_list (kw:regexp) (sep:lens) (sto:regexp) (list_sep:lens) (comment:lens) = let list = counter "elem" . Build.opt_list [ seq "elem" . store sto ] list_sep in Build.key_value_line_comment kw sep (Sep.opt_space . list) comment
Generic INI File list entry
kw:regexp | keyword regexp for the label |
sep:lens | lens to use as key/value separator |
sto:regexp | store regexp for the values |
list_sep:lens | lens to use as list separator |
comment:lens | lens to use as comment |
let entry_list_nocomment (kw:regexp) (sep:lens) (sto:regexp) (list_sep:lens) = let list = counter "elem" . Build.opt_list [ seq "elem" . store sto ] list_sep in Build.key_value_line kw sep (Sep.opt_space . list)
Generic INI File list entry without an end-of-line comment
kw:regexp | keyword regexp for the label |
sep:lens | lens to use as key/value separator |
sto:regexp | store regexp for the values |
list_sep:lens | lens to use as list separator |
let entry_re = ( /[A-Za-z][A-Za-z0-9._-]+/ )
Default regexp for entry keyword
let title (kw:regexp) = Util.del_str "[" . key kw . Util.del_str "]". eol
Title for record. This maps the title of a record as a node in the abstract tree.
kw:regexp | keyword regexp for the label |
let title = IniFile.title IniFile.record_re
let indented_title (kw:regexp) = Util.indent . title kw
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
kw:regexp | keyword regexp for the label |
let title = IniFile.title IniFile.record_re
let title_label (name:string) (kw:regexp) = label name . Util.del_str "[" . store kw . Util.del_str "]". eol
Title for record. This maps the title of a record as a value in the abstract tree.
name:string | name for the title label |
kw:regexp | keyword regexp for the label |
let title = IniFile.title_label "target" IniFile.record_label_re
let indented_title_label (name:string) (kw:regexp) = Util.indent . title_label name kw
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
name:string | name for the title label |
kw:regexp | keyword regexp for the label |
let title = IniFile.title_label "target" IniFile.record_label_re
let record_re = ( /[^]\n\/]+/ - /#comment/ )
Default regexp for title keyword pattern
let record_label_re = /[^]\n]+/
Default regexp for title_label keyword pattern
let record_noempty (title:lens) (entry:lens) = [ title . entry* ]
INI File Record with no empty lines allowed.
title:lens | lens to use for title. Use either title or title_label. |
entry:lens | lens to use for entries in the record. See entry. |
let record (title:lens) (entry:lens) = record_noempty title ( entry | empty )
Generic INI File record
title:lens | lens to use for title. Use either title or title_label. |
entry:lens | lens to use for entries in the record. See entry. |
let record = IniFile.record title entry
let lns_noempty (record:lens) (comment:lens) = comment* . record*
Generic INI File lens with no empty lines
record:lens | record lens to use. See record_noempty. |
comment:lens | comment lens to use. See comment. |
let lns = IniFile.lns_noempty record comment
End of line, inherited from Util.eol
let eol = Util.eol
Delete end of line, including optional trailing whitespace
let eol = del /[ \t]*\n/ "\n"
Empty line, an eol subnode
let empty = [ eol ]
Generic separator
let sep (pat:regexp) (default:string) = Sep.opt_space . del pat default
Generic separator, no indentation
let sep_noindent (pat:regexp) (default:string) = del pat default
The default regexp for a separator
let sep_re = /[=:]/
The default separator value
let sep_default = "="
Store until end of line
let sto_to_eol = Sep.opt_space . store Rx.space_in
Regex until comment
let to_comment_re = /[^";# \t\n][^";#\n]*[^";# \t\n]|[^";# \t\n]/ | /"[^\n"]*"/
Store until comment
let sto_to_comment = Sep.opt_space . store to_comment_re
Store multiline values
let sto_multiline = Sep.opt_space . store (to_comment_re . (/[ \t]*\n/ . Rx.space . to_comment_re)*)
Store multiline values without an end-of-line comment
let sto_multiline_nocomment = Sep.opt_space . store (Rx.space_in . (/[ \t]*\n/ . Rx.space . Rx.space_in)*)
Map comments into “#comment” nodes
let comment (pat:regexp) (default:string) = [ label "#comment" . sep pat default . sto_to_eol? . eol ]
Default regexp for comment pattern
let comment_re = /[;#]/
Default value for comment pattern
let comment_default = ";"
Generic INI File entry
let entry (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic multiline INI File entry
let entry_multiline (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . sto_multiline? . (comment|eol) ] | comment
Generic multiline INI File entry without an end-of-line comment
let entry_multiline_nocomment (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . sto_multiline_nocomment? . eol ] | comment
Generic INI File entry that might be indented with an arbitrary amount of whitespace
let indented_entry (kw:regexp) (sep:lens) (comment:lens) = [ Util.del_opt_ws "" . key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic INI File list entry
let entry_list (kw:regexp) (sep:lens) (sto:regexp) (list_sep:lens) (comment:lens) = let list = counter "elem" . Build.opt_list [ seq "elem" . store sto ] list_sep in Build.key_value_line_comment kw sep (Sep.opt_space . list) comment
Generic INI File list entry without an end-of-line comment
let entry_list_nocomment (kw:regexp) (sep:lens) (sto:regexp) (list_sep:lens) = let list = counter "elem" . Build.opt_list [ seq "elem" . store sto ] list_sep in Build.key_value_line kw sep (Sep.opt_space . list)
Default regexp for entry keyword
let entry_re = ( /[A-Za-z][A-Za-z0-9._-]+/ )
Title for record.
let title (kw:regexp) = Util.del_str "[" . key kw . Util.del_str "]". eol
Generic INI File record
let record (title:lens) (entry:lens) = record_noempty title ( entry | empty )
Title for record.
let indented_title (kw:regexp) = Util.indent . title kw
Title for record.
let title_label (name:string) (kw:regexp) = label name . Util.del_str "[" . store kw . Util.del_str "]". eol
Title for record.
let indented_title_label (name:string) (kw:regexp) = Util.indent . title_label name kw
Default regexp for title keyword pattern
let record_re = ( /[^]\n\/]+/ - /#comment/ )
Default regexp for title_label keyword pattern
let record_label_re = /[^]\n]+/
INI File Record with no empty lines allowed.
let record_noempty (title:lens) (entry:lens) = [ title . entry* ]
Generic INI File lens with no empty lines
let lns_noempty (record:lens) (comment:lens) = comment* . record*
Generic INI File lens
let lns (record:lens) (comment:lens) = lns_noempty record (comment|empty)