ConfigFile-1.1.4: Configuration file reading & writing

Safe HaskellNone
LanguageHaskell98

Data.ConfigFile.Monadic

Contents

Synopsis

Overview

This module reexports a slightly different version of the standard API which makes it more convenient for chaining monadically. Everywhere a ConfigParser was the first argument in a function in the standard API, it is now the last. This lets you rewrite

do let cp = emptyCP
   cp <- add_section cp "sect1"
   cp <- set cp "sect1" "opt1" "foo"
   cp <- set cp "sect1" "opt2" "bar"
   options cp "sect1"

as

return emptyCP >>=
 add_section "sect1" >>=
 set "sect1" "opt1" "foo" >>=
 set "sect1" "opt2" "bar" >>=
 options "sect1"

which may be more elegant in some cases. A future development might be to chain the ConfigParser implicitly with a state monad, which would be yet more elegant.

data ConfigParser Source #

This is the main record that is used by ConfigFile.

Constructors

ConfigParser 

Fields

type CPError = (CPErrorData, String) Source #

Indicates an error occurred. The String is an explanation of the location of the error.

type OptionSpec = String Source #

Names of options

type SectionSpec = String Source #

Names of sections

class Get_C a where Source #

The class representing the data types that can be returned by "get".

Minimal complete definition

get

Methods

get :: MonadError CPError m => ConfigParser -> SectionSpec -> OptionSpec -> m a Source #

Retrieves a string from the configuration file.

When used in a context where a String is expected, returns that string verbatim.

When used in a context where a Bool is expected, parses the string to a Boolean value (see logic below).

When used in a context where anything that is an instance of Read is expected, calls read to parse the item.

An error will be returned of no such option could be found or if it could not be parsed as a boolean (when returning a Bool).

When parsing to a Bool, strings are case-insentively converted as follows:

The following will produce a True value:

  • 1
  • yes
  • on
  • enabled
  • true

The following will produce a False value:

  • 0
  • no
  • off
  • disabled
  • false
Instances
Get_C Bool Source # 
Instance details

Defined in Data.ConfigFile

Read t => Get_C t Source # 
Instance details

Defined in Data.ConfigFile

Get_C String Source # 
Instance details

Defined in Data.ConfigFile

emptyCP :: ConfigParser Source #

The default empty ConfigFile object.

The content contains only an empty mandatory DEFAULT section.

optionxform is set to map toLower.

usedefault is set to True.

accessfunc is set to simpleAccess.

merge :: ConfigParser -> ConfigParser -> ConfigParser Source #

Combines two ConfigParsers into one.

Any duplicate options are resolved to contain the value specified in the second parser.

The ConfigParser options in the resulting object will be set as they are in the second one passed to this function.

sections :: ConfigParser -> [SectionSpec] Source #

Returns a list of sections in your configuration file. Never includes the always-present section DEFAULT.

to_string :: ConfigParser -> String Source #

Converts the ConfigParser to a string representation that could be later re-parsed by this module or modified by a human.

Note that this does not necessarily re-create a file that was originally loaded. Things may occur in a different order, comments will be removed, etc. The conversion makes an effort to make the result human-editable, but it does not make an effort to make the result identical to the original input.

The result is, however, guaranteed to parse the same as the original input.