1module Data.ConfigFile.Monadic ( 2 -- * Overview 3 -- $overview 4 5 module Reexporting, 6 simpleAccess, 7 interpolatingAccess, 8 readfile, readhandle, readstring, 9 has_section, options, has_option, items, 10 set, setshow, remove_option, add_section, remove_section 11) where 12 13import Control.Monad.Error 14import System.IO(Handle) 15import Data.ConfigFile as Reexporting (SectionSpec, OptionSpec, ConfigParser(..), 16 CPErrorData, CPError, emptyCP, Get_C(..), sections, merge, to_string) 17import qualified Data.ConfigFile as C 18 19{- $overview 20This 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 21 22> do let cp = emptyCP 23> cp <- add_section cp "sect1" 24> cp <- set cp "sect1" "opt1" "foo" 25> cp <- set cp "sect1" "opt2" "bar" 26> options cp "sect1" 27 28as 29 30> return emptyCP >>= 31> add_section "sect1" >>= 32> set "sect1" "opt1" "foo" >>= 33> set "sect1" "opt2" "bar" >>= 34> options "sect1" 35 36which 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. 37 38-} 39 40simpleAccess :: MonadError CPError m => 41 SectionSpec -> OptionSpec -> ConfigParser -> m String 42simpleAccess s o cp = C.simpleAccess cp s o 43 44interpolatingAccess :: MonadError CPError m => 45 Int -> 46 SectionSpec -> OptionSpec -> ConfigParser 47 -> m String 48interpolatingAccess maxdepth s o cp = C.interpolatingAccess maxdepth cp s o 49 50readfile :: MonadError CPError m => FilePath -> ConfigParser -> IO (m ConfigParser) 51readfile fp cp = C.readfile cp fp 52 53readhandle :: MonadError CPError m => Handle -> ConfigParser -> IO (m ConfigParser) 54readhandle h cp = C.readhandle cp h 55 56readstring :: MonadError CPError m => 57 String -> ConfigParser -> m ConfigParser 58readstring cp s = C.readstring s cp 59 60has_section :: SectionSpec -> ConfigParser -> Bool 61has_section x cp = C.has_section cp x 62 63add_section :: MonadError CPError m => 64 SectionSpec -> ConfigParser -> m ConfigParser 65add_section s cp = C.add_section cp s 66 67options :: MonadError CPError m => 68 SectionSpec -> ConfigParser -> m [OptionSpec] 69options x cp = C.options cp x 70 71has_option :: SectionSpec -> OptionSpec -> ConfigParser -> Bool 72has_option s o cp = C.has_option cp s o 73 74items :: MonadError CPError m => 75 SectionSpec -> ConfigParser -> m [(OptionSpec, String)] 76items s cp = C.items cp s 77 78set :: MonadError CPError m => 79 SectionSpec -> OptionSpec -> String -> ConfigParser -> m ConfigParser 80set s passedo val cp = C.set cp s passedo val 81 82setshow :: (Show a, MonadError CPError m) => 83 SectionSpec -> OptionSpec -> a -> ConfigParser -> m ConfigParser 84setshow s o val cp = C.setshow cp s o val 85 86remove_option :: MonadError CPError m => 87 SectionSpec -> OptionSpec -> ConfigParser -> m ConfigParser 88remove_option s passedo cp = C.remove_option cp s passedo 89 90 91remove_section :: MonadError CPError m => 92 SectionSpec -> ConfigParser -> m ConfigParser 93remove_section s cp = C.remove_section cp s 94 95