[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:48:23 UTC 2010
The following commit has been merged in the master branch:
commit f315c2b663721dfaf53f60028817a459aea79aa7
Author: John Goerzen <jgoerzen at complete.org>
Date: Tue Nov 23 05:06:45 2004 +0100
Checkpointing initial configparser development
Keywords:
(jgoerzen at complete.org--projects/missingh--head--0.5--patch-107)
diff --git a/ChangeLog b/ChangeLog
index 02ba71e..401dc0c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
# arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--0.5
#
+2004-11-22 22:06:45 GMT John Goerzen <jgoerzen at complete.org> patch-107
+
+ Summary:
+ Checkpointing initial configparser development
+ Revision:
+ missingh--head--0.5--patch-107
+
+
+ modified files:
+ ChangeLog libsrc/MissingH/ConfigParser.hs
+ libsrc/MissingH/ConfigParser/Types.hs
+
+
2004-11-22 21:35:07 GMT John Goerzen <jgoerzen at complete.org> patch-106
Summary:
diff --git a/libsrc/MissingH/ConfigParser.hs b/libsrc/MissingH/ConfigParser.hs
index 1d4ccc8..75e895e 100644
--- a/libsrc/MissingH/ConfigParser.hs
+++ b/libsrc/MissingH/ConfigParser.hs
@@ -82,14 +82,18 @@ merge src dest =
ConfigParser { content = plusFM (mapFM convFM (content src))
(content dest),
optionxform = optionxform dest,
- usedefault = usedefault dest }
+ usedefault = usedefault dest,
+ defaulthandler = defaulthandler dest,
+ accessfunc = accessfunc dest}
{- | Utility to do a special case merge. -}
readutil :: ConfigParser -> ParseOutput -> ConfigParser
readutil old new =
let mergedest = ConfigParser {content = fromAL new,
optionxform = optionxform old,
- usedefault = usedefault old}
+ usedefault = usedefault old,
+ defaulthandler = defaulthandler old,
+ accessfunc = accessfunc old}
in
merge old mergedest
@@ -169,22 +173,22 @@ has_option cp s o =
no such option could be found. -}
get :: ConfigParser -> SectionSpec -> OptionSpec -> String
get cp s o =
- case (accessfunc cp) s o of
+ case (accessfunc cp) cp s o of
Nothing -> error $ "get: no option " ++ s ++ "/" ++ o
Just x -> x
{- | Retrieves a string from the configuration file and attempts to parse it
as a number. Raises an exception if no such option could be found or if it
could not be parsed as the destination number. -}
-getnum :: Num a => ConfigParser -> SectionSpec -> OptionSpec -> a
-getnum = read . get
+getnum :: (Read a, Num a) => ConfigParser -> SectionSpec -> OptionSpec -> a
+getnum cp s o = read $ get cp s o
{- | Retrieves a string from the configuration file and attempts to parse
it as a boolean. Raises an exception if no such option could be found or
if it could not be parsed as a boolean. -}
getbool :: ConfigParser -> SectionSpec -> OptionSpec -> Bool
getbool cp s o =
- case map toLower . strip . get $ cp s o of
+ case map toLower . strip . get cp s $ o of
"1" -> True
"yes" -> True
"on" -> True
@@ -209,7 +213,7 @@ set cp s passedo val =
where newmap = addToFM (content cp) s newsect
newsect = addToFM sectmap o val
sectmap = forceLookupFM "ConfigParser.set" (content cp) s
- o = (optionxform cp) cp passedo
+ o = (optionxform cp) passedo
{- | Sets the option to a new value, replacing an existing one if it exists.
It requires only a showable value as its parameter.
@@ -218,7 +222,17 @@ an error if the section does not exist. -}
setshow :: Show a => ConfigParser -> SectionSpec -> OptionSpec -> a -> ConfigParser
setshow cp s o val = set cp s o (show val)
-
+{- | Converts the 'ConfigParser' to a string representation that could be
+later re-parsed by this module. -}
+to_string :: ConfigParser -> String
+to_string cp =
+ let gen_option (key, value) =
+ key ++ ": " ++ (replace "\n" "\n " value) ++ "\n"
+ gen_section (sect, valfm) =
+ "[" ++ sect ++ "]\n" ++
+ (concat $ map gen_option (fmToList valfm)) ++ "\n"
+ in
+ concat $ map gen_section (fmToList (content cp))
----------------------------------------------------------------------
-- Docs
diff --git a/libsrc/MissingH/ConfigParser/Types.hs b/libsrc/MissingH/ConfigParser/Types.hs
index c0856c8..989b47d 100644
--- a/libsrc/MissingH/ConfigParser/Types.hs
+++ b/libsrc/MissingH/ConfigParser/Types.hs
@@ -61,8 +61,9 @@ data ConfigParser = ConfigParser
content :: CPData,
-- | How to transform an option into a standard representation
optionxform :: (OptionSpec -> OptionSpec),
- -- | Function to look up an option, considering a default value
- -- | if 'usedefault' is True; or ignoring a default value otherwise.
+ -- | 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.
defaulthandler :: (ConfigParser -> SectionSpec -> OptionSpec -> Maybe String),
-- | Whether or not to seek out a default action when no match
-- is found.
@@ -82,8 +83,31 @@ The content contains only an empty mandatory @DEFAULT@ section.
-}
empty :: ConfigParser
empty = ConfigParser { content = fromAL [("DEFAULT", [])],
+ defaulthandler = defdefaulthandler,
optionxform = map toLower,
- usedefault = True}
+ usedefault = True,
+ accessfunc = defaccessfunc}
+
+-- internal function: default access function
+defaccessfunc :: ConfigParser -> SectionSpec -> OptionSpec -> Maybe String
+defaccessfunc cp s o = defdefaulthandler cp s (optionxform cp $ o)
+
+-- internal function: default handler
+defdefaulthandler :: ConfigParser -> SectionSpec -> OptionSpec -> Maybe String
+defdefaulthandler cp sect opt =
+ let fm = content cp
+ lookup s o =
+ case lookupFM fm s of
+ Nothing -> Nothing
+ Just sect -> case lookupFM sect o of
+ Nothing -> Nothing
+ Just x -> Just x
+ in
+ case lookup sect opt of
+ Just r -> Just r
+ Nothing -> if (usedefault cp)
+ then lookup "DEFAULT" opt
+ else Nothing
{- | Low-level tool to convert a parsed object into a 'CPData'
representation. Performs no option conversions or special handling
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list