[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:19 UTC 2010


The following commit has been merged in the master branch:
commit 9782755d9d197a5cad437e6fa8c450d7a510ce4a
Author: John Goerzen <jgoerzen at complete.org>
Date:   Tue Nov 23 04:35:07 2004 +0100

    Checkpointing
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--0.5--patch-106)

diff --git a/ChangeLog b/ChangeLog
index ede2980..02ba71e 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 21:35:07 GMT	John Goerzen <jgoerzen at complete.org>	patch-106
+
+    Summary:
+      Checkpointing
+    Revision:
+      missingh--head--0.5--patch-106
+
+
+    modified files:
+     ChangeLog TODO libsrc/MissingH/ConfigParser.hs
+     libsrc/MissingH/ConfigParser/Types.hs
+
+
 2004-11-19 22:10:35 GMT	John Goerzen <jgoerzen at complete.org>	patch-105
 
     Summary:
diff --git a/TODO b/TODO
index 2582131..3a90697 100644
--- a/TODO
+++ b/TODO
@@ -3,4 +3,6 @@ arch-tag: TODO file
 Printf:
  make %H work 
 
+Test configparser according to spec
+
 tests for new Parsec stuff
diff --git a/libsrc/MissingH/ConfigParser.hs b/libsrc/MissingH/ConfigParser.hs
index 3cc3e90..1d4ccc8 100644
--- a/libsrc/MissingH/ConfigParser.hs
+++ b/libsrc/MissingH/ConfigParser.hs
@@ -42,19 +42,28 @@ module MissingH.ConfigParser
      -- $reading
      readfile, readhandle, readstring,
 
+     -- * Accessing Data
+     get, getbool, getnum,
+
+     -- * Setting Data
+     set, setshow,
+
      -- * Meta-queries
      sections, has_section,
      options, has_option,
+     items,
 
-     -- * Miscellaneous anipulation
-     merge
+     -- * Miscellaneous Manipulation
+     add_section, merge
 ) where
 import MissingH.ConfigParser.Types
 import MissingH.ConfigParser.Parser
 import MissingH.FiniteMap
+import MissingH.Str
 import Data.FiniteMap
 import Data.List
 import System.IO(Handle)
+import Data.Char
 
 {- | Combines two 'ConfigParser's into one.
 
@@ -128,6 +137,15 @@ No special @DEFAULT@ processing is done. -}
 has_section :: ConfigParser -> SectionSpec -> Bool
 has_section cp x = elemFM x (content cp)
 
+{- | Adds the specified section name.  Raises an exception if the
+section was already present.  Otherwise, returns the new 
+'ConfigParser' object.-}
+add_section :: ConfigParser -> SectionSpec -> ConfigParser
+add_section cp s =
+    if has_section cp s
+       then error ("add_section: section " ++ s ++ " already exists")
+       else cp {content = addToFM (content cp) s emptyFM}
+
 {- | Returns a list of the names of all the options present in the
 given section.
 
@@ -147,6 +165,60 @@ has_option cp s o =
                 elemFM (optionxform cp $ o)
                        (forceLookupFM "ConfigParser.has_option" c s) 
                            
+{- | Retrieves a string from the configuration file.  Raises an exception if
+no such option could be found. -}
+get :: ConfigParser -> SectionSpec -> OptionSpec -> String
+get cp s o = 
+    case (accessfunc 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
+
+{- | 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
+         "1" -> True
+         "yes" -> True
+         "on" -> True
+         "enabled" -> True
+         "0" -> False
+         "no" -> False
+         "off" -> False
+         "disabled" -> False
+         _ -> error ("getbool: couldn't parse " ++ get cp s o ++ " from " ++
+                     s ++ "/" ++ o)
+
+{- | Returns a list of @(optionname, value)@ pairs representing the content
+of the given section.  Raises an error if the section is invalid. -}
+items :: ConfigParser -> SectionSpec -> [(OptionSpec, String)]
+items cp s = fmToList (forceLookupFM "ConfigParser.items" (content cp) s)
+
+{- | Sets the option to a new value, replacing an existing one if it exists.
+Raises an error if the section does not exist. -}
+set :: ConfigParser -> SectionSpec -> OptionSpec -> String -> ConfigParser
+set cp s passedo val = 
+    cp { content = newmap}
+    where newmap = addToFM (content cp) s newsect
+          newsect = addToFM sectmap o val
+          sectmap = forceLookupFM "ConfigParser.set" (content cp) s
+          o = (optionxform cp) 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.
+This can be used with bool values, as well as numeric ones.  Raises
+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)
+
+
 
 ----------------------------------------------------------------------
 -- Docs
diff --git a/libsrc/MissingH/ConfigParser/Types.hs b/libsrc/MissingH/ConfigParser/Types.hs
index 4cee4fd..c0856c8 100644
--- a/libsrc/MissingH/ConfigParser/Types.hs
+++ b/libsrc/MissingH/ConfigParser/Types.hs
@@ -61,10 +61,15 @@ data ConfigParser = ConfigParser
       content :: CPData,
       -- | How to transform an option into a standard representation
       optionxform :: (OptionSpec -> OptionSpec),
-      --defaulthandler :: (ConfigParser -> String -> String),
+      -- | Function to look up an option, considering a default value
+      -- | if 'usedefault' is True; or ignoring a default value otherwise.
+      defaulthandler :: (ConfigParser -> SectionSpec -> OptionSpec -> Maybe String),
       -- | Whether or not to seek out a default action when no match
       -- is found.
-      usedefault :: Bool
+      usedefault :: Bool,
+      -- | Function that is used to perform lookups, do optional
+      -- interpolation, etc.
+      accessfunc :: (ConfigParser -> SectionSpec -> OptionSpec -> Maybe String)
     }
 
 {- | The default empty 'MissingH.ConfigParser' object.

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list