a cached ledger now includes the full ledger text, also.

This reads the file twice, since I couldn't figure out how to do it just
once with the new error monad.. fixes welcome.
This commit is contained in:
Simon Michael 2008-12-10 07:29:08 +00:00
parent ee4a2a1c1e
commit a51596899f
4 changed files with 17 additions and 11 deletions

View File

@ -2,7 +2,8 @@
A 'Ledger' stores, for efficiency, a 'RawLedger' plus its tree of account A 'Ledger' stores, for efficiency, a 'RawLedger' plus its tree of account
names, and a map from account names to 'Account's. It may also have had names, and a map from account names to 'Account's. It may also have had
uninteresting 'Entry's and 'Transaction's filtered out. uninteresting 'Entry's and 'Transaction's filtered out. It also stores
the complete ledger file text for the ui command.
-} -}
@ -30,7 +31,7 @@ instance Show Ledger where
-- | Convert a raw ledger to a more efficient cached type, described above. -- | Convert a raw ledger to a more efficient cached type, described above.
cacheLedger :: [String] -> RawLedger -> Ledger cacheLedger :: [String] -> RawLedger -> Ledger
cacheLedger apats l = Ledger l ant acctmap cacheLedger apats l = Ledger{rawledgertext="",rawledger=l,accountnametree=ant,accountmap=acctmap}
where where
ts = filtertxns apats $ rawLedgerTransactions l ts = filtertxns apats $ rawLedgerTransactions l
ant = rawLedgerAccountNameTree l ant = rawLedgerAccountNameTree l

View File

@ -106,6 +106,7 @@ data Account = Account {
} }
data Ledger = Ledger { data Ledger = Ledger {
rawledgertext :: String,
rawledger :: RawLedger, rawledger :: RawLedger,
accountnametree :: Tree AccountName, accountnametree :: Tree AccountName,
accountmap :: Map.Map AccountName Account accountmap :: Map.Map AccountName Account

View File

@ -16,10 +16,10 @@ import Ledger
-- | Convert a RawLedger to a canonicalised, cached and filtered Ledger -- | Convert a RawLedger to a canonicalised, cached and filtered Ledger
-- based on the command-line options/arguments and today's date. -- based on the command-line options/arguments and today's date.
prepareLedger :: [Opt] -> [String] -> Day -> RawLedger -> Ledger prepareLedger :: [Opt] -> [String] -> Day -> String -> RawLedger -> Ledger
prepareLedger opts args refdate rl = prepareLedger opts args refdate rawtext rl = l{rawledgertext=rawtext}
cacheLedger apats $ filterRawLedger span dpats c r $ canonicaliseAmounts cb rl
where where
l = cacheLedger apats $ filterRawLedger span dpats c r $ canonicaliseAmounts cb rl
(apats,dpats) = parseAccountDescriptionArgs [] args (apats,dpats) = parseAccountDescriptionArgs [] args
span = dateSpanFromOpts refdate opts span = dateSpanFromOpts refdate opts
c = Cleared `elem` opts c = Cleared `elem` opts
@ -33,14 +33,15 @@ rawledgerfromstring = liftM (either error id) . runErrorT . parseLedger "(string
-- | Get a Ledger from the given string and options, or raise an error. -- | Get a Ledger from the given string and options, or raise an error.
ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> IO Ledger ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> IO Ledger
ledgerfromstringwithopts opts args refdate s = ledgerfromstringwithopts opts args refdate s =
liftM (prepareLedger opts args refdate) $ rawledgerfromstring s liftM (prepareLedger opts args refdate s) $ rawledgerfromstring s
-- | Get a Ledger from the given file path and options, or raise an error. -- | Get a Ledger from the given file path and options, or raise an error.
ledgerfromfilewithopts :: [Opt] -> [String] -> FilePath -> IO Ledger ledgerfromfilewithopts :: [Opt] -> [String] -> FilePath -> IO Ledger
ledgerfromfilewithopts opts args f = do ledgerfromfilewithopts opts args f = do
rl <- readFile f >>= rawledgerfromstring refdate <- today
refdate <- today s <- readFile f
return $ prepareLedger opts args refdate rl rl <- rawledgerfromstring s
return $ prepareLedger opts args refdate s rl
-- | Get a Ledger from your default ledger file, or raise an error. -- | Get a Ledger from your default ledger file, or raise an error.
-- Assumes no options. -- Assumes no options.

View File

@ -76,5 +76,8 @@ main = do
parseLedgerAndDo :: [Opt] -> [String] -> ([Opt] -> [String] -> Ledger -> IO ()) -> IO () parseLedgerAndDo :: [Opt] -> [String] -> ([Opt] -> [String] -> Ledger -> IO ()) -> IO ()
parseLedgerAndDo opts args cmd = do parseLedgerAndDo opts args cmd = do
refdate <- today refdate <- today
let runcmd = cmd opts args . prepareLedger opts args refdate -- ack, we're reading the file twice in order to save the text
ledgerFilePathFromOpts opts >>= runErrorT . parseLedgerFile >>= either (hPutStrLn stderr) runcmd f <- ledgerFilePathFromOpts opts
rawtext <- readFile f
let runcmd = cmd opts args . prepareLedger opts args refdate rawtext
return f >>= runErrorT . parseLedgerFile >>= either (hPutStrLn stderr) runcmd