[augeas-devel] Re: Towards generic modules
Raphaël Pinson
raphink at gmail.com
Wed Jul 23 10:37:08 UTC 2008
There, I'm a bit more happy with this...
I added a test file which (I believe) performs all the necessary tests. I
will have a look at the hg mail command to send the diff to you.
inifile.aug :
==================================================================
(* IniFile generic module for Augeas *)
(* Author: Raphael Pinson <raphink at gmail.com> *)
(* *)
(* TODO: Support double quotes in value *)
module IniFile =
(* Define useful shortcuts *)
let eol = Util.del_str "\n"
let del_to_eol = del /[^\n]*/ ""
let value_sep = del /[ \t]*=[ \t]*/ " = "
let value_sepwithcolon = del /[ \t]*(=|:)[ \t]*/ " = "
let value_to_eol = store /([^ \t\n][^\n]*)?/
(* Define entry function *)
(* Some implementations of INI file allow ";" as separator *)
let entry (kw:regexp) = [ key kw . value_sepwithcolon . value_to_eol .
eol ]
let entry_nocolon (kw:regexp) = [ key kw . value_sep . value_to_eol .
eol ]
(* Define comment and empty strings *)
(* Some implementations of INI file allow "#" as a comment sign *)
let comment = [ label "comment" . del /(#|;)[ \t]*/ "; " . store /([^
\t\n][^\n]*)?/ . eol ]
let comment_nosharp = [ label "comment" . del /;[ \t]*/ "; " . store
/([^ \t\n][^\n]*)?/ . eol ]
let empty = [ del /[ \t]*/ "" . eol ]
(* Define record *)
let title = Util.del_str "[" . store /[^] ]+/ . Util.del_str "]". eol
let record (label_name:string) (entry:lens) = [ label label_name . title
. (entry | comment | empty)* ]
let record_setcomment (label_name:string) (entry:lens) (comment:lens) =
[ label label_name . title . (entry | comment | empty)* ]
(* Some implementations of INI File do not allow empty lines *)
let record_noempty (label_name:string) (entry:lens) = [ label label_name
. title . (entry | comment)* ]
let record_noempty_setcomment (label_name:string) (entry:lens)
(comment:lens) = [ label label_name . title . (entry | comment)* ]
(* Generic INI file lens *)
let lns (record:lens) = ( comment | empty )* . record*
(* Let the user choose the type of comment they want *)
let lns_setcomment (record:lens) (comment:lens) = ( comment | empty )* .
record*
(* Some implementations of INI File do not allow empty lines *)
let lns_noempty (record:lens) = comment* . record*
(* Let the user choose the type of comment they want *)
let lns_noempty_setcomment (record:lens) (comment:lens) = comment* .
record*
==================================================================
tests/test_inifile.aug
==================================================================
module Test_IniFile =
(* ALL TESTS TO RUN *)
(* entry : (a) entry; (b) entry_nocolon *)
(* comment : (c) comment; (d) comment_nosharp *)
(* lns : (e) lns; (f) lns_noempty *)
(* TEST a/c/e *)
let entry_ace = IniFile.entry "test_ace"
let record_ace = IniFile.record "record_ace" entry_ace
let lns_ace = IniFile.lns record_ace
let conf_ace = "# comment with sharp
[section1]
test_ace = value
; comment with colon
"
test lns_ace get conf_ace =
{ "comment" = "comment with sharp" }
{}
{ "record_ace" = "section1"
{ "test_ace" = "value" }
{ "comment" = "comment with colon" }
{} }
(* TEST a/c/f *)
let entry_acf = IniFile.entry "test_acf"
let record_acf = IniFile.record_noempty "record_acf" entry_acf
let lns_acf = IniFile.lns_noempty record_acf
let conf_acf = "# comment with sharp
[section1]
test_acf = value
test_acf : value2
; comment with colon
"
test lns_acf get conf_acf =
{ "comment" = "comment with sharp" }
{ "record_acf" = "section1"
{ "test_acf" = "value" }
{ "test_acf" = "value2" }
{ "comment" = "comment with colon" } }
(* TEST a/d/e *)
let entry_ade = IniFile.entry "test_ade"
let record_ade = IniFile.record_setcomment "record_ade" entry_ade
IniFile.comment_nosharp
let lns_ade = IniFile.lns_setcomment record_ade IniFile.comment_nosharp
let conf_ade = "; a first comment with colon
[section1]
test_ade = value
test_ade : value2
; comment with colon
"
test lns_ade get conf_ade =
{ "comment" = "a first comment with colon" }
{ "record_ade" = "section1"
{ "test_ade" = "value" }
{ "test_ade" = "value2" }
{ "comment" = "comment with colon" }
{} }
(* TEST a/d/f *)
let entry_adf = IniFile.entry "test_adf"
let record_adf = IniFile.record_noempty_setcomment "record_adf" entry_adf
IniFile.comment_nosharp
let lns_adf = IniFile.lns_noempty_setcomment record_adf
IniFile.comment_nosharp
let conf_adf = "; a first comment with colon
[section1]
test_adf = value
test_adf : value2
; comment with colon
"
test lns_adf get conf_adf =
{ "comment" = "a first comment with colon" }
{ "record_adf" = "section1"
{ "test_adf" = "value" }
{ "test_adf" = "value2" }
{ "comment" = "comment with colon" } }
(* TEST b/c/e *)
let entry_bce = IniFile.entry_nocolon "test_bce"
let record_bce = IniFile.record "record_bce" entry_bce
let lns_bce = IniFile.lns record_bce
let conf_bce = "# comment with sharp
[section1]
test_bce = value
; comment with colon
"
test lns_bce get conf_bce =
{ "comment" = "comment with sharp" }
{}
{ "record_bce" = "section1"
{ "test_bce" = "value" }
{ "comment" = "comment with colon" }
{} }
(* TEST b/c/f *)
let entry_bcf = IniFile.entry_nocolon "test_bcf"
let record_bcf = IniFile.record_noempty "record_bcf" entry_bcf
let lns_bcf = IniFile.lns_noempty record_bcf
let conf_bcf = "# comment with sharp
[section1]
test_bcf = value
; comment with colon
"
test lns_bcf get conf_bcf =
{ "comment" = "comment with sharp" }
{ "record_bcf" = "section1"
{ "test_bcf" = "value" }
{ "comment" = "comment with colon" } }
(* TEST b/d/e *)
let entry_bde = IniFile.entry_nocolon "test_bde"
let record_bde = IniFile.record_setcomment "record_bde" entry_bde
IniFile.comment_nosharp
let lns_bde = IniFile.lns_setcomment record_bde IniFile.comment_nosharp
let conf_bde = "; first comment with colon
[section1]
test_bde = value
; comment with colon
"
test lns_bde get conf_bde =
{ "comment" = "first comment with colon" }
{}
{ "record_bde" = "section1"
{ "test_bde" = "value" }
{ "comment" = "comment with colon" }
{} }
(* TEST b/d/f *)
let entry_bdf = IniFile.entry_nocolon "test_bdf"
let record_bdf = IniFile.record_noempty_setcomment "record_bdf" entry_bdf
IniFile.comment_nosharp
let lns_bdf = IniFile.lns_noempty_setcomment record_bdf
IniFile.comment_nosharp
let conf_bdf = "; first comment with colon
[section1]
test_bdf = value
; comment with colon
"
test lns_bdf get conf_bdf =
{ "comment" = "first comment with colon" }
{ "record_bdf" = "section1"
{ "test_bdf" = "value" }
{ "comment" = "comment with colon" } }
==================================================================
On Wed, Jul 23, 2008 at 10:49 AM, Raphaël Pinson <raphink at gmail.com> wrote:
>
>
> On Tue, Jul 22, 2008 at 8:53 PM, David Lutterkort <dlutter at redhat.com>
> wrote:
>
>> On Tue, 2008-07-22 at 10:58 +0200, Raphaël Pinson wrote:
>> > Actually, here is an even more compact version :
>>
>> Very nice. I like that a lot. Do you want me to commit that ?
>>
>
> I'm happy you like it.
>
> I don't think it's ready for production yet though. I made a wiki page for
> it where I'm posting the code and notes as it goes (
> http://augeas.net/page/Generic_modules/IniFile ). I understand that it's
> not as useful as a VCS, but it's a bit easier for me for now and it allows
> to put the documentation altogether.
>
>
>> One small request: it would make my life easier if you could send these
>> things as patches (if you commit your changes locally, you can send a
>> patch with 'hg email')
>
>
>
> I will certainly email you the patch once I have implemented the full
> format (or at least a reasonable amount of it).
>
>
>
> Raphaël
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20080723/f9fe6d2b/attachment.htm>
More information about the augeas-devel
mailing list