Generic modules IniFile

raphink edited this page Jan 4, 2013 · 1 revision

This generic module helps you to create lenses for INI files.

Table of Contents

Format reference

http://en.wikipedia.org/wiki/INI_file

Example usage

     let settings = IniFile.entry "allow_non-us_software"
                  | IniFile.entry "allow_unsigned_uploads"
                  | IniFile.entry "check_version"
                  | IniFile.entry "default_host_main"
                  | IniFile.entry "default_host_non-us"
                  | IniFile.entry "fqdn"
                  | IniFile.entry "hash"
                  | IniFile.entry "incoming"
                  | IniFile.entry "login"
                  | IniFile.entry "method"
                  | IniFile.entry "passive_ftp"
                  | IniFile.entry "post_upload_command"
                  | IniFile.entry "pre_upload_command"
                  | IniFile.entry "run_dinstall"
                  | IniFile.entry "run_lintian"
                  | IniFile.entry "scp_compress"
 
 
     let record = IniFile.record "target" settings
 
 
     let lns = IniFile.lns record
 
     let filter = (incl "/etc/dput.cf")
         . (incl "~/.dput.cf")
         . Util.stdexcl
 
     let xfm = transform lns filter

Types

This class provides several useful types for INI files.

All these ypes can be referred to by using:

 IniFile.$type $type_argument*

comment

General usage:

 let mycomment = IniFile.comment

The module currently provides 3 types of comments.

comment_generic

Definition:

 let comment_generic (pattern:regexp) = [ label "comment" . del pattern "; " . value_to_eol . eol ]

This is the most generic type of comment, which takes a pattern as an argument. Use this if you need to refine the pattern for comments.

comment

Definition:

 let comment = comment_generic /[ \t]*(#|;)[ \t]*/

This is the default kind of comment. In can be indented and the comment character might be either "#" or ";".

comment_nosharp

Definition:

 let comment_nosharp = comment_generic /[ \t]*;[ \t]*/ 

Some implementations of INI files do not support "#" as a valid comment character. If it is your case, you can use this definition to support only ";".

entry

General usage:

 let myentry = IniFile.entry

The module currently supports 5 types of entries.

entry_generic

Definition:

 let entry_generic (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . value_to_comment? . (comment|eol) ]

This is the most generic definition of an entry. It takes 3 arguments: the keyword (regexp), the separator (lens) and the type of comment (lens).

You can use this definition if none of the other definitions fit your needs.

entry

Definition:

 let entry (kw:regexp) = entry_generic kw value_sepwithcolon comment

This is the default definition of an entry. It takes a keyword (regexp) as argument. It supports both "=" and ":" as separators and trailing comments using both ";" and "#".

entry_setcomment

Definition:

 let entry_setcomment (kw:regexp) (comment:lens) = entry_generic kw value_sepwithcolon comment

This definition allows you to choose the type of comment to use. You might want to use this if you are using comment_nosharp or if your format does not support trailing comments.

entry_nocolon

Definition:

 let entry_nocolon (kw:regexp) = entry_generic kw value_sep comment

This is the same as the default entry definition, but it does not allow ":" as a separator.

entry_nocolon_setcomment

Definition:

 let entry_nocolon_setcomment (kw:regexp) (comment:lens) = entry_generic kw value_sep comment 

Same as entry_nocolon, but lets you set the type of comment to use.

record

IniFile.record represents an Ini file record, e.g. "[record]\nfield = value\n"

General usage:

 let myrecord = IniFile.record "record_label" myentry

There are currently 4 types of it.

record

Definition:

 let record (label_name:string) (entry:lens) = [ label label_name . title . (entry | comment | empty)* ]

This is a default record. It takes two arguments: the label name (string) and the entry lens (lens).

record_setcomment

Definition:

 let record_setcomment (label_name:string) (entry:lens) (comment:lens) = [ label label_name . title . (entry | comment | empty)* ]

Same as record, but allows you to choose the type of comment to use. This is useful if you wish to use comment_nosharp or to make up your own kind of comment lens.

record_noempty

Definition:

 let record_noempty (label_name:string) (entry:lens) = [ label label_name . title . (entry | comment)* ]

Some implementations of INI File do not allow empty lines. This is the default record type implementing this behavior.

record_noempty_setcomment

Definition:

 let record_noempty_setcomment (label_name:string) (entry:lens) (comment:lens) = [ label label_name . title . (entry | comment)* ] 

Same as record_noempty, but lets you set the type of comment to use.

lns

General usage:

 let lns = IniFile.lns myrecord

IniFile.lns represents an Ini file lens.

In effect, it implements '( comment | empty | record )*', using the IniFile definition of empty and comment.

Note: It might be interesting to make other optional lns types, such as lns_nocomment which would remove comments from the tree.

There are currently 4 types of lns types.

lns

Definition:

 let lns (record:lens) = ( comment | empty )* . record*

This is the default type of lns. It takes one argument: the record lens.

lns_setcomment

Definition:

 let lns_setcomment (record:lens) (comment:lens) = ( comment | empty )* . record*

Same as lns, but lets you choose the type of comment to use.

lns_noempty

Definition:

 let lns_noempty (record:lens) = comment* . record*

Same as lns, but does not support empty lines.

lns_noempty_setcomment

Definition:

 let lns_noempty_setcomment (record:lens) (comment:lens) = comment* . record* 

Same as lns_noempty, but lets you choose the type of comment to use.

TODO

  • Support double quotes in values (e.g. 'field1 = "value2" ). This is allowed by some implementations of INI files.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.