clean up ledger loading utilities
This commit is contained in:
parent
e34b8b5662
commit
1c969b5744
52
Utils.hs
52
Utils.hs
@ -29,51 +29,19 @@ prepareLedger opts args refdate rl =
|
|||||||
rawledgerfromstring :: String -> RawLedger
|
rawledgerfromstring :: String -> RawLedger
|
||||||
rawledgerfromstring = fromparse . parsewith ledgerfile
|
rawledgerfromstring = fromparse . parsewith ledgerfile
|
||||||
|
|
||||||
-- | Get a RawLedger from the given file path, or raise an error.
|
-- | Get a Ledger from the given string and options, 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
|
|
||||||
|
|
||||||
ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> Ledger
|
ledgerfromstringwithopts :: [Opt] -> [String] -> Day -> String -> Ledger
|
||||||
ledgerfromstringwithopts opts args refdate s =
|
ledgerfromstringwithopts opts args refdate s =
|
||||||
prepareLedger opts args refdate $ rawledgerfromstring s
|
prepareLedger opts args refdate $ rawledgerfromstring s
|
||||||
|
|
||||||
-- | Get a filtered and cached Ledger from the given file path and
|
-- | Get a Ledger from the given file path and options, or raise an error.
|
||||||
-- arguments, or raise an error. Does not handle all the command-line
|
ledgerfromfilewithopts :: [Opt] -> [String] -> FilePath -> IO Ledger
|
||||||
-- options that parseLedgerAndDo does.
|
ledgerfromfilewithopts opts args f = do
|
||||||
ledgerfromfile :: [String] -> FilePath -> IO Ledger
|
rl <- readFile f >>= return . rawledgerfromstring
|
||||||
ledgerfromfile args f =
|
refdate <- today
|
||||||
rawledgerfromfile f >>= return .
|
return $ prepareLedger opts args refdate rl
|
||||||
cacheLedger apats .
|
|
||||||
filterRawLedger (DateSpan Nothing Nothing) dpats False False .
|
|
||||||
canonicaliseAmounts False
|
|
||||||
where
|
|
||||||
(apats,dpats) = parseAccountDescriptionArgs [] args
|
|
||||||
|
|
||||||
-- | Get a cached Ledger from the file your LEDGER environment variable
|
-- | Get a Ledger from your default ledger file, or raise an error.
|
||||||
-- variable points to, or raise an error. Assumes no command-line arguments.
|
-- Assumes no options.
|
||||||
myledger :: IO Ledger
|
myledger :: IO Ledger
|
||||||
myledger = myrawledger >>= return .
|
myledger = ledgerFilePathFromOpts [] >>= ledgerfromfilewithopts [] []
|
||||||
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)
|
|
||||||
|
|
||||||
|
|||||||
10
hledger.hs
10
hledger.hs
@ -18,16 +18,16 @@ You can use the command line:
|
|||||||
or ghci:
|
or ghci:
|
||||||
|
|
||||||
> $ ghci hledger
|
> $ ghci hledger
|
||||||
> > l <- ledgerfromfile [] "sample.ledger"
|
> > l <- ledgerfromfilewithopts [] [] "sample.ledger"
|
||||||
> > balance [] [] l
|
> > balance [] [] l
|
||||||
> $-1 assets
|
> $-1 assets
|
||||||
> $2 expenses
|
> $2 expenses
|
||||||
> $-2 income
|
> $-2 income
|
||||||
> $1 liabilities:debts
|
> $1 liabilities
|
||||||
> > register [] ["income","expenses"] l
|
> > register [] ["income","expenses"] l
|
||||||
> 2007/01/01 income income:salary $-1 $-1
|
> 2008/01/01 income income:salary $-1 $-1
|
||||||
> 2007/01/01 gift income:gifts $-1 $-2
|
> 2008/06/01 gift income:gifts $-1 $-2
|
||||||
> 2007/01/01 eat & shop expenses:food $1 $-1
|
> 2008/06/03 eat & shop expenses:food $1 $-1
|
||||||
> expenses:supplies $1 0
|
> expenses:supplies $1 0
|
||||||
|
|
||||||
-}
|
-}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user