hledger/Utils.hs

48 lines
1.6 KiB
Haskell
Raw Normal View History

2008-10-10 14:04:26 +04:00
{-|
2008-10-12 13:17:21 +04:00
Utilities for top-level modules and/or ghci. See also "Ledger.Utils".
2008-10-10 14:04:26 +04:00
-}
module Utils
where
import qualified Data.Map as Map (lookup)
2008-11-22 23:22:59 +03:00
import Text.ParserCombinators.Parsec
2008-11-29 23:00:21 +03:00
import System.IO
2008-10-10 14:04:26 +04:00
import Options
import Ledger
2008-11-29 23:00:21 +03:00
-- | Convert a RawLedger to a canonicalised, cached and filtered Ledger
-- based on the command-line options/arguments and today's date.
prepareLedger :: [Opt] -> [String] -> Day -> RawLedger -> Ledger
prepareLedger opts args refdate rl =
cacheLedger apats $ filterRawLedger span dpats c r $ canonicaliseAmounts cb rl
where
(apats,dpats) = parseAccountDescriptionArgs [] args
span = dateSpanFromOpts refdate opts
c = Cleared `elem` opts
r = Real `elem` opts
cb = CostBasis `elem` opts
2008-11-22 23:22:59 +03:00
-- | Get a RawLedger from the given string, or raise an error.
rawledgerfromstring :: String -> RawLedger
rawledgerfromstring = fromparse . parsewith ledgerfile
2008-12-05 11:51:14 +03:00
-- | Get a Ledger from the given string and options, or raise an error.
2008-11-29 23:00:21 +03:00
ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> Ledger
ledgerfromstringwithopts opts args refdate s =
prepareLedger opts args refdate $ rawledgerfromstring s
2008-10-10 14:04:26 +04:00
2008-12-05 11:51:14 +03:00
-- | Get a Ledger from the given file path and options, or raise an error.
ledgerfromfilewithopts :: [Opt] -> [String] -> FilePath -> IO Ledger
ledgerfromfilewithopts opts args f = do
rl <- readFile f >>= return . rawledgerfromstring
refdate <- today
return $ prepareLedger opts args refdate rl
2008-12-05 11:51:14 +03:00
-- | Get a Ledger from your default ledger file, or raise an error.
-- Assumes no options.
2008-10-10 14:04:26 +04:00
myledger :: IO Ledger
2008-12-05 11:51:14 +03:00
myledger = ledgerFilePathFromOpts [] >>= ledgerfromfilewithopts [] []