clean up ledger loading utilities

This commit is contained in:
Simon Michael 2008-12-05 08:51:14 +00:00
parent e34b8b5662
commit 1c969b5744
2 changed files with 15 additions and 47 deletions

View File

@ -29,51 +29,19 @@ prepareLedger opts args refdate rl =
rawledgerfromstring :: String -> RawLedger
rawledgerfromstring = fromparse . parsewith ledgerfile
-- | Get a RawLedger from the given file path, or raise an error.
rawledgerfromfile :: FilePath -> IO RawLedger
rawledgerfromfile f = openFile f ReadMode >>= hGetContents >>= return . rawledgerfromstring
-- | Get a RawLedger from the file your LEDGER environment variable
-- variable points to, or raise an error.
myrawledger :: IO RawLedger
myrawledger = ledgerFilePathFromOpts [] >>= rawledgerfromfile
-- | Get a filtered and cached Ledger from the given string and arguments,
-- or raise an error. Does not handle all the command-line options that
-- parseLedgerAndDo does.
ledgerfromstring :: [String] -> String -> Ledger
ledgerfromstring args s =
cacheLedger apats $ filterRawLedger span dpats False False $ canonicaliseAmounts False l
where
(apats,dpats) = parseAccountDescriptionArgs [] args
span = (DateSpan Nothing Nothing)
l = rawledgerfromstring s
-- | Get a Ledger from the given string and options, or raise an error.
ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> Ledger
ledgerfromstringwithopts opts args refdate s =
prepareLedger opts args refdate $ rawledgerfromstring s
-- | Get a filtered and cached Ledger from the given file path and
-- arguments, or raise an error. Does not handle all the command-line
-- options that parseLedgerAndDo does.
ledgerfromfile :: [String] -> FilePath -> IO Ledger
ledgerfromfile args f =
rawledgerfromfile f >>= return .
cacheLedger apats .
filterRawLedger (DateSpan Nothing Nothing) dpats False False .
canonicaliseAmounts False
where
(apats,dpats) = parseAccountDescriptionArgs [] args
-- | 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
-- | Get a cached Ledger from the file your LEDGER environment variable
-- variable points to, or raise an error. Assumes no command-line arguments.
-- | Get a Ledger from your default ledger file, or raise an error.
-- Assumes no options.
myledger :: IO Ledger
myledger = myrawledger >>= return .
cacheLedger [] .
filterRawLedger (DateSpan Nothing Nothing) [] False False .
canonicaliseAmounts False
-- | Get a named account from your ledger file.
myaccount :: AccountName -> IO Account
myaccount a = myledger >>= (return . fromMaybe nullacct . Map.lookup a . accountmap)
myledger = ledgerFilePathFromOpts [] >>= ledgerfromfilewithopts [] []

View File

@ -18,16 +18,16 @@ You can use the command line:
or ghci:
> $ ghci hledger
> > l <- ledgerfromfile [] "sample.ledger"
> > l <- ledgerfromfilewithopts [] [] "sample.ledger"
> > balance [] [] l
> $-1 assets
> $2 expenses
> $-2 income
> $1 liabilities:debts
> $1 liabilities
> > register [] ["income","expenses"] l
> 2007/01/01 income income:salary $-1 $-1
> 2007/01/01 gift income:gifts $-1 $-2
> 2007/01/01 eat & shop expenses:food $1 $-1
> 2008/01/01 income income:salary $-1 $-1
> 2008/06/01 gift income:gifts $-1 $-2
> 2008/06/03 eat & shop expenses:food $1 $-1
> expenses:supplies $1 0
-}