[augeas-devel] [PATCH] Add inifile generic module and associated test module
raphink at gmail.com
raphink at gmail.com
Wed Jul 23 10:52:24 UTC 2008
# HG changeset patch
# User rpinson at lab64.echo-net.net
# Date 1216809799 -7200
# Node ID 48bba9ca2e8b09d96f37763e8a15a7a447c4fe64
# Parent 6f2f5289c94a304d686b13c7aa6778ddcedbbe7d
Add inifile generic module and associated test module
diff -r 6f2f5289c94a -r 48bba9ca2e8b lenses/inifile.aug
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lenses/inifile.aug Wed Jul 23 12:43:19 2008 +0200
@@ -0,0 +1,47 @@
+(* 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*
diff -r 6f2f5289c94a -r 48bba9ca2e8b lenses/tests/test_inifile.aug
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lenses/tests/test_inifile.aug Wed Jul 23 12:43:19 2008 +0200
@@ -0,0 +1,158 @@
+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" } }
+
+
+
More information about the augeas-devel
mailing list