Data.ConfigFile.Monadic

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 #

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) #

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

data CPErrorData #

Possible ConfigParser errors.

Instances

Eq CPErrorData # |
Instance details Defined in Data.ConfigFile.Types Methods (==) :: CPErrorData -> CPErrorData -> Bool # (/=) :: CPErrorData -> CPErrorData -> Bool #
Ord CPErrorData # |
Instance details Defined in Data.ConfigFile.Types Methods compare :: CPErrorData -> CPErrorData -> Ordering # (<) :: CPErrorData -> CPErrorData -> Bool # (<=) :: CPErrorData -> CPErrorData -> Bool # (>) :: CPErrorData -> CPErrorData -> Bool # (>=) :: CPErrorData -> CPErrorData -> Bool # max :: CPErrorData -> CPErrorData -> CPErrorData # min :: CPErrorData -> CPErrorData -> CPErrorData #
Show CPErrorData # |
Instance details Defined in Data.ConfigFile.Types Methods showsPrec :: Int -> CPErrorData -> ShowS # show :: CPErrorData -> String # showList :: [CPErrorData] -> ShowS #
Error CPError # |
Instance details Defined in Data.ConfigFile.Types Methods noMsg :: CPError # strMsg :: String -> CPError #

type OptionSpec = String #

Names of options

type SectionSpec = String #

Names of sections

class Get_C a where #

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

Methods

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

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

emptyCP :: ConfigParser #

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 #

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] #

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

to_string :: ConfigParser -> String #

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 #

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 #