2008-10-10 14:04:26 +04:00
|
|
|
{-|
|
|
|
|
|
2009-04-04 12:50:36 +04:00
|
|
|
Utilities for top-level modules and ghci. See also "Ledger.IO" and
|
|
|
|
"Ledger.Utils".
|
2008-10-10 14:04:26 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utils
|
|
|
|
where
|
2008-12-08 04:49:31 +03:00
|
|
|
import Control.Monad.Error
|
2008-10-10 14:04:26 +04:00
|
|
|
import Ledger
|
2009-07-10 00:25:50 +04:00
|
|
|
import Options (Opt,ledgerFilePathFromOpts,optsToFilterSpec)
|
2009-05-17 00:45:05 +04:00
|
|
|
import System.Directory (doesFileExist)
|
2009-04-04 12:50:36 +04:00
|
|
|
import System.IO
|
2008-10-10 14:04:26 +04:00
|
|
|
|
|
|
|
|
2009-05-17 00:45:05 +04:00
|
|
|
-- | Parse the user's specified ledger file and run a hledger command on
|
|
|
|
-- it, or report a parse error. This function makes the whole thing go.
|
|
|
|
withLedgerDo :: [Opt] -> [String] -> String -> ([Opt] -> [String] -> Ledger -> IO ()) -> IO ()
|
|
|
|
withLedgerDo opts args cmdname cmd = do
|
|
|
|
-- We kludgily read the file before parsing to grab the full text, unless
|
|
|
|
-- it's stdin, or it doesn't exist and we are adding. We read it strictly
|
|
|
|
-- to let the add command work.
|
2009-04-04 12:50:36 +04:00
|
|
|
f <- ledgerFilePathFromOpts opts
|
2009-05-17 00:45:05 +04:00
|
|
|
let f' = if f == "-" then "/dev/null" else f
|
|
|
|
fileexists <- doesFileExist f
|
|
|
|
let creating = not fileexists && cmdname == "add"
|
|
|
|
rawtext <- if creating then return "" else strictReadFile f'
|
2009-01-25 10:06:59 +03:00
|
|
|
t <- getCurrentLocalTime
|
2009-05-17 00:45:05 +04:00
|
|
|
let go = cmd opts args . filterAndCacheLedgerWithOpts opts args t rawtext . (\rl -> rl{filepath=f})
|
|
|
|
case creating of
|
|
|
|
True -> return rawLedgerEmpty >>= go
|
|
|
|
False -> return f >>= runErrorT . parseLedgerFile t >>= either (hPutStrLn stderr) go
|
2008-11-22 23:22:59 +03:00
|
|
|
|
2008-12-05 11:51:14 +03:00
|
|
|
-- | Get a Ledger from the given string and options, or raise an error.
|
2009-04-04 12:50:36 +04:00
|
|
|
ledgerFromStringWithOpts :: [Opt] -> [String] -> LocalTime -> String -> IO Ledger
|
|
|
|
ledgerFromStringWithOpts opts args reftime s =
|
|
|
|
liftM (filterAndCacheLedgerWithOpts opts args reftime s) $ rawLedgerFromString s
|
|
|
|
|
|
|
|
-- | Read a Ledger from the given file, filtering according to the
|
|
|
|
-- options, or give an error.
|
|
|
|
readLedgerWithOpts :: [Opt] -> [String] -> FilePath -> IO Ledger
|
|
|
|
readLedgerWithOpts opts args f = do
|
|
|
|
t <- getCurrentLocalTime
|
2009-07-10 00:25:50 +04:00
|
|
|
readLedgerWithFilterSpec (optsToFilterSpec opts args t) f
|
2008-11-22 08:51:48 +03:00
|
|
|
|
2009-04-04 12:50:36 +04:00
|
|
|
-- | Convert a RawLedger to a canonicalised, cached and filtered Ledger
|
|
|
|
-- based on the command-line options/arguments and a reference time.
|
|
|
|
filterAndCacheLedgerWithOpts :: [Opt] -> [String] -> LocalTime -> String -> RawLedger -> Ledger
|
2009-09-22 19:56:59 +04:00
|
|
|
filterAndCacheLedgerWithOpts opts args = filterAndCacheLedger . optsToFilterSpec opts args
|
2008-12-08 04:49:31 +03:00
|
|
|
|