lib, app, web: use readJournalFileWithOpts instead of readJournalFile whenever possible

This commit is contained in:
Dmitry Astapov 2018-04-17 18:48:10 +01:00 committed by Simon Michael
parent c7d86f3572
commit 37607beaea
7 changed files with 13 additions and 11 deletions

4
dev.hs
View File

@ -51,7 +51,7 @@ timeReadJournal msg s = timeit msg $ either error id <$> readJournal Nothing Not
main = do main = do
-- putStrLn $ regexReplaceCI "^aa" "xx" "aa:bb:cc:dd:ee" -- putStrLn $ regexReplaceCI "^aa" "xx" "aa:bb:cc:dd:ee"
(_t0,_j) <- timeit ("read "++journal) $ either error id <$> readJournalFile Nothing Nothing True journal (_t0,_j) <- timeit ("read "++journal) $ either error id <$> readJournalFileWithOpts def journal
return () return ()
-- printf "Total: %0.2fs\n" (sum [t0,t1,t2,t3,t4]) -- printf "Total: %0.2fs\n" (sum [t0,t1,t2,t3,t4])
@ -156,7 +156,7 @@ main = do
-- benchWithTimeit = do -- benchWithTimeit = do
-- getCurrentDirectory >>= printf "Benchmarking hledger in %s with timeit\n" -- getCurrentDirectory >>= printf "Benchmarking hledger in %s with timeit\n"
-- let opts = defcliopts{output_file_=Just outputfile} -- let opts = defcliopts{output_file_=Just outputfile}
-- (t0,j) <- timeit ("read "++inputfile) $ either error id <$> readJournalFile Nothing Nothing True inputfile -- (t0,j) <- timeit ("read "++inputfile) $ either error id <$> readJournalFileWithOpts def inputfile
-- (t1,_) <- timeit ("print") $ print' opts j -- (t1,_) <- timeit ("print") $ print' opts j
-- (t2,_) <- timeit ("register") $ register opts j -- (t2,_) <- timeit ("register") $ register opts j
-- (t3,_) <- timeit ("balance") $ balance opts j -- (t3,_) <- timeit ("balance") $ balance opts j

View File

@ -91,7 +91,7 @@ main = do
let let
defd = "." defd = "."
d = getArgWithDefault args defd (longOption "static-dir") d = getArgWithDefault args defd (longOption "static-dir")
readJournalFile Nothing def f >>= either error' (serveApi h p d f) readJournalFileWithOpts def f >>= either error' (serveApi h p d f)
serveApi :: String -> Int -> FilePath -> FilePath -> Journal -> IO () serveApi :: String -> Int -> FilePath -> FilePath -> Journal -> IO ()
serveApi h p d f j = do serveApi h p d f j = do

View File

@ -15,6 +15,7 @@ module Hledger.Read (
defaultJournal, defaultJournal,
defaultJournalPath, defaultJournalPath,
readJournalFilesWithOpts, readJournalFilesWithOpts,
readJournalFileWithOpts,
readJournalFiles, readJournalFiles,
readJournalFile, readJournalFile,
requireJournalFileExists, requireJournalFileExists,
@ -91,7 +92,7 @@ type PrefixedFilePath = FilePath
-- | Read the default journal file specified by the environment, or raise an error. -- | Read the default journal file specified by the environment, or raise an error.
defaultJournal :: IO Journal defaultJournal :: IO Journal
defaultJournal = defaultJournalPath >>= readJournalFile Nothing def >>= either error' return defaultJournal = defaultJournalPath >>= readJournalFileWithOpts def >>= either error' return
-- | Get the default journal file path specified by the environment. -- | Get the default journal file path specified by the environment.
-- Like ledger, we look first for the LEDGER_FILE environment -- Like ledger, we look first for the LEDGER_FILE environment

View File

@ -39,7 +39,7 @@ import Handler.SidebarR
import Hledger.Web.WebOptions (WebOpts(..), defwebopts) import Hledger.Web.WebOptions (WebOpts(..), defwebopts)
import Hledger.Data (Journal, nulljournal) import Hledger.Data (Journal, nulljournal)
import Hledger.Read (readJournalFile) import Hledger.Read (readJournalFileWithOpts)
import Hledger.Utils (error') import Hledger.Utils (error')
import Hledger.Cli.CliOptions (defcliopts, journalFilePathFromOpts) import Hledger.Cli.CliOptions (defcliopts, journalFilePathFromOpts)
@ -80,7 +80,7 @@ makeFoundation conf opts = do
getApplicationDev :: IO (Int, Application) getApplicationDev :: IO (Int, Application)
getApplicationDev = do getApplicationDev = do
f <- head `fmap` journalFilePathFromOpts defcliopts -- XXX head should be safe for now f <- head `fmap` journalFilePathFromOpts defcliopts -- XXX head should be safe for now
j <- either error' id `fmap` readJournalFile Nothing def f j <- either error' id `fmap` readJournalFileWithOpts def f
defaultDevelApp loader (makeApplication defwebopts j) defaultDevelApp loader (makeApplication defwebopts j)
where where
loader = Yesod.Default.Config.loadConfig (configSettings Development) loader = Yesod.Default.Config.loadConfig (configSettings Development)

View File

@ -66,7 +66,7 @@ withJournalDo' opts@WebOpts {cliopts_ = cliopts} cmd = do
. journalApplyAliases (aliasesFromOpts cliopts) . journalApplyAliases (aliasesFromOpts cliopts)
<=< journalApplyValue (reportopts_ cliopts) <=< journalApplyValue (reportopts_ cliopts)
<=< journalAddForecast cliopts <=< journalAddForecast cliopts
readJournalFile Nothing def f >>= either error' fn readJournalFileWithOpts def f >>= either error' fn
-- | The web command. -- | The web command.
web :: WebOpts -> Journal -> IO () web :: WebOpts -> Journal -> IO ()

View File

@ -19,7 +19,7 @@ You can use the command line:
or ghci: or ghci:
> $ ghci hledger > $ ghci hledger
> > j <- readJournalFile Nothing Nothing True "examples/sample.journal" > > j <- readJournalFileWithOpts def "examples/sample.journal"
> > register [] ["income","expenses"] j > > register [] ["income","expenses"] j
> 2008/01/01 income income:salary $-1 $-1 > 2008/01/01 income income:salary $-1 $-1
> 2008/06/01 gift income:gifts $-1 $-2 > 2008/06/01 gift income:gifts $-1 $-2

View File

@ -4,6 +4,7 @@
import Criterion.Main (defaultMainWith, defaultConfig, bench, nfIO) import Criterion.Main (defaultMainWith, defaultConfig, bench, nfIO)
-- import QuickBench (defaultMain) -- import QuickBench (defaultMain)
import Data.Default
import System.Directory (getCurrentDirectory) import System.Directory (getCurrentDirectory)
import System.Environment (getArgs, withArgs) import System.Environment (getArgs, withArgs)
import System.TimeIt (timeItT) import System.TimeIt (timeItT)
@ -33,7 +34,7 @@ main = do
benchWithTimeit = do benchWithTimeit = do
getCurrentDirectory >>= printf "Benchmarking hledger in %s with timeit\n" getCurrentDirectory >>= printf "Benchmarking hledger in %s with timeit\n"
let opts = defcliopts{output_file_=Just outputfile} let opts = defcliopts{output_file_=Just outputfile}
(t0,j) <- timeit ("read "++inputfile) $ either error id <$> readJournalFile Nothing Nothing True inputfile (t0,j) <- timeit ("read "++inputfile) $ either error id <$> readJournalFileWithOpts def inputfile
(t1,_) <- timeit ("print") $ print' opts j (t1,_) <- timeit ("print") $ print' opts j
(t2,_) <- timeit ("register") $ register opts j (t2,_) <- timeit ("register") $ register opts j
(t3,_) <- timeit ("balance") $ balance opts j (t3,_) <- timeit ("balance") $ balance opts j
@ -49,9 +50,9 @@ timeit name action = do
benchWithCriterion = do benchWithCriterion = do
getCurrentDirectory >>= printf "Benchmarking hledger in %s with criterion\n" getCurrentDirectory >>= printf "Benchmarking hledger in %s with criterion\n"
let opts = defcliopts{output_file_=Just "/dev/null"} let opts = defcliopts{output_file_=Just "/dev/null"}
j <- either error id <$> readJournalFile Nothing Nothing True inputfile j <- either error id <$> readJournalFileWithOpts def inputfile
Criterion.Main.defaultMainWith defaultConfig $ [ Criterion.Main.defaultMainWith defaultConfig $ [
bench ("read "++inputfile) $ nfIO $ (either error const <$> readJournalFile Nothing Nothing True inputfile), bench ("read "++inputfile) $ nfIO $ (either error const <$> readJournalFileWithOpts def inputfile),
bench ("print") $ nfIO $ print' opts j, bench ("print") $ nfIO $ print' opts j,
bench ("register") $ nfIO $ register opts j, bench ("register") $ nfIO $ register opts j,
bench ("balance") $ nfIO $ balance opts j, bench ("balance") $ nfIO $ balance opts j,