ConfigFile-1.1.4: Configuration file reading & writing

Safe Haskell | None
Language | Haskell98

Data.ConfigFile.Monadic

Contents

- Overview

Synopsis

- data ConfigParser = ConfigParser {
  - content :: CPData
  - optionxform :: OptionSpec -> OptionSpec
  - defaulthandler :: ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String
  - usedefault :: Bool
  - accessfunc :: ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String
  }
- type CPError = (CPErrorData, String)
- data CPErrorData
- type OptionSpec = String
- type SectionSpec = String
- class Get_C a where
  - get :: MonadError CPError m => ConfigParser -> SectionSpec -> OptionSpec -> m a
- emptyCP :: ConfigParser
- merge :: ConfigParser -> ConfigParser -> ConfigParser
- sections :: ConfigParser -> [SectionSpec]
- to_string :: ConfigParser -> String
- simpleAccess :: MonadError CPError m => SectionSpec -> OptionSpec -> ConfigParser -> m String
- interpolatingAccess :: MonadError CPError m => Int -> SectionSpec -> OptionSpec -> ConfigParser -> m String
- readfile :: MonadError CPError m => FilePath -> ConfigParser -> IO (m ConfigParser)
- readhandle :: MonadError CPError m => Handle -> ConfigParser -> IO (m ConfigParser)
- readstring :: MonadError CPError m => String -> ConfigParser -> m ConfigParser
- has_section :: SectionSpec -> ConfigParser -> Bool
- options :: MonadError CPError m => SectionSpec -> ConfigParser -> m [OptionSpec]
- has_option :: SectionSpec -> OptionSpec -> ConfigParser -> Bool
- items :: MonadError CPError m => SectionSpec -> ConfigParser -> m [(OptionSpec, String)]
- set :: MonadError CPError m => SectionSpec -> OptionSpec -> String -> ConfigParser -> m ConfigParser
- setshow :: (Show a, MonadError CPError m) => SectionSpec -> OptionSpec -> a -> ConfigParser -> m ConfigParser
- remove_option :: MonadError CPError m => SectionSpec -> OptionSpec -> ConfigParser -> m ConfigParser
- add_section :: MonadError CPError m => SectionSpec -> ConfigParser -> m ConfigParser
- remove_section :: MonadError CPError m => SectionSpec -> ConfigParser -> m ConfigParser

# 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

- content :: CPData

  The data itself

- optionxform :: OptionSpec -> OptionSpec

  How to transform an option into a standard representation

- defaulthandler :: ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String

  Function to look up an option, considering a default value if `usedefault` is True; or ignoring a default value otherwise. The option specification is assumed to be already transformed.

- usedefault :: Bool

  Whether or not to seek out a default action when no match is found.

- accessfunc :: ConfigParser -> SectionSpec -> OptionSpec -> Either CPError String

  Function that is used to perform lookups, do optional interpolation, etc. It is assumed that accessfunc will internally call defaulthandler to do the underlying lookup. The option value is not assumed to be transformed.

type CPError = (CPErrorData, String) Source #

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

data CPErrorData Source #

Possible ConfigParser errors.

Instances

Eq CPErrorData Source # |
Instance details Defined in Data.ConfigFile.Types Methods (==) :: CPErrorData -> CPErrorData -> Bool Source # (/=) :: CPErrorData -> CPErrorData -> Bool Source #
Ord CPErrorData Source # |

Instance details

Defined in Data.ConfigFile.Types

Methods

compare :: CPErrorData -> CPErrorData -> Ordering Source #

(<) :: CPErrorData -> CPErrorData -> Bool Source #

(<=) :: CPErrorData -> CPErrorData -> Bool Source #

(>) :: CPErrorData -> CPErrorData -> Bool Source #

(>=) :: CPErrorData -> CPErrorData -> Bool Source #

max :: CPErrorData -> CPErrorData -> CPErrorData Source #

min :: CPErrorData -> CPErrorData -> CPErrorData Source #

Show CPErrorData Source # |
Instance details Defined in Data.ConfigFile.Types Methods showsPrec :: Int -> CPErrorData -> ShowS Source # show :: CPErrorData -> String Source # showList :: [CPErrorData] -> ShowS Source #
Error CPError Source # |
Instance details Defined in Data.ConfigFile.Types Methods noMsg :: CPError Source # strMsg :: String -> CPError Source #

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".

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 Methods get :: MonadError CPError m => ConfigParser -> SectionSpec -> OptionSpec -> m Bool Source #
Read t => Get_C t Source # |
Instance details Defined in Data.ConfigFile Methods get :: MonadError CPError m => ConfigParser -> SectionSpec -> OptionSpec -> m t Source #
Get_C String Source # |
Instance details Defined in Data.ConfigFile Methods get :: MonadError CPError m => ConfigParser -> SectionSpec -> OptionSpec -> m String Source #

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 `ConfigParser`s 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.

simpleAccess :: MonadError CPError m => SectionSpec -> OptionSpec -> ConfigParser -> m String Source #

interpolatingAccess :: MonadError CPError m => Int -> SectionSpec -> OptionSpec -> ConfigParser -> m String Source #

readfile :: MonadError CPError m => FilePath -> ConfigParser -> IO (m ConfigParser) Source #

readhandle :: MonadError CPError m => Handle -> ConfigParser -> IO (m ConfigParser) Source #

readstring :: MonadError CPError m => String -> ConfigParser -> m ConfigParser Source #

has_section :: SectionSpec -> ConfigParser -> Bool Source #

options :: MonadError CPError m => SectionSpec -> ConfigParser -> m [OptionSpec] Source #

has_option :: SectionSpec -> OptionSpec -> ConfigParser -> Bool Source #

items :: MonadError CPError m => SectionSpec -> ConfigParser -> m [(OptionSpec, String)] Source #

set :: MonadError CPError m => SectionSpec -> OptionSpec -> String -> ConfigParser -> m ConfigParser Source #

setshow :: (Show a, MonadError CPError m) => SectionSpec -> OptionSpec -> a -> ConfigParser -> m ConfigParser Source #

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

add_section :: MonadError CPError m => SectionSpec -> ConfigParser -> m ConfigParser Source #

remove_section :: MonadError CPError m => SectionSpec -> ConfigParser -> m ConfigParser Source #