{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
module Hledger.Read (
defaultJournal,
defaultJournalSafely,
defaultJournalWith,
defaultJournalWithSafely,
defaultJournalPath,
defaultJournalPathSafely,
defaultExistingJournalPath,
defaultExistingJournalPathSafely,
requireJournalFileExists,
ensureJournalFileExists,
journalEnvVar,
journalDefaultFilename,
runExceptT,
readJournal,
readJournalFile,
readPossibleJournalFile,
readPossibleJournalFiles,
readJournalFiles,
readJournalFilesAndLatestDates,
readJournal',
readJournal'',
readJournalFile',
readJournalFiles',
orDieTrying,
isWindowsUnsafeDotPath,
JournalReader.tmpostingrulep,
JournalReader.parseAssertDirective,
findReader,
splitReaderPrefix,
runJournalParser,
module Hledger.Read.Common,
module Hledger.Read.InputOptions,
module Hledger.Read.LatestDates,
tests_Read,
) where
import Control.Exception qualified as C
import Control.Monad (unless, when, (>=>))
import "mtl" Control.Monad.Except (ExceptT(..), runExceptT, liftEither)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Default (def)
import Data.Foldable (asum)
import Data.List (intercalate, union)
import Data.List.NonEmpty (nonEmpty, NonEmpty((:|)))
import Data.Containers.ListUtils (nubOrd)
import Data.Map qualified as M
import Data.Maybe (catMaybes, fromMaybe, isJust)
import Data.Semigroup (sconcat)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.IO qualified as T
import System.Directory (createDirectoryIfMissing, doesFileExist)
import System.Environment (getEnv)
import System.FilePath ((</>), splitDirectories, takeDirectory, takeFileName)
import System.Info (os)
import System.IO (Handle, hPutStrLn, stderr)
import Hledger.Data.Amount (nullamt, showAmount)
import Hledger.Data.Dates (getCurrentDay)
import Hledger.Data.Journal (commodityCombine, journalNumberAndTieTransactions, nulljournal)
import Hledger.Data.JournalChecks (journalStrictChecks)
import Hledger.Data.Types
import Hledger.Read.Common
import Hledger.Read.InputOptions
import Hledger.Read.JournalReader as JournalReader
import Hledger.Read.LatestDates
import Hledger.Read.CsvReader (tests_CsvReader)
import Hledger.Read.RulesReader (tests_RulesReader)
import Hledger.Utils
journalEnvVar :: String
journalEnvVar = String
"LEDGER_FILE"
journalDefaultFilename :: String
journalDefaultFilename = String
".hledger.journal"
defaultJournal :: IO Journal
defaultJournal :: IO Journal
defaultJournal = IO (Either String Journal)
defaultJournalSafely IO (Either String Journal)
-> (Either String Journal -> IO Journal) -> IO Journal
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (String -> IO Journal)
-> (Journal -> IO Journal) -> Either String Journal -> IO Journal
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> IO Journal
forall a. String -> a
error' Journal -> IO Journal
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
defaultJournalSafely :: IO (Either String Journal)
defaultJournalSafely :: IO (Either String Journal)
defaultJournalSafely = InputOpts -> IO (Either String Journal)
defaultJournalWithSafely InputOpts
definputopts
defaultJournalWith :: InputOpts -> IO Journal
defaultJournalWith :: InputOpts -> IO Journal
defaultJournalWith InputOpts
iopts = InputOpts -> IO (Either String Journal)
defaultJournalWithSafely InputOpts
iopts IO (Either String Journal)
-> (Either String Journal -> IO Journal) -> IO Journal
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (String -> IO Journal)
-> (Journal -> IO Journal) -> Either String Journal -> IO Journal
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> IO Journal
forall a. String -> a
error' Journal -> IO Journal
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
defaultJournalWithSafely :: InputOpts -> IO (Either String Journal)
defaultJournalWithSafely :: InputOpts -> IO (Either String Journal)
defaultJournalWithSafely InputOpts
iopts = (do
f <- IO String
defaultJournalPath
runExceptT $ readJournalFile iopts f
)
IO (Either String Journal)
-> [Handler (Either String Journal)] -> IO (Either String Journal)
forall a. IO a -> [Handler a] -> IO a
`C.catches` [
(ErrorCall -> IO (Either String Journal))
-> Handler (Either String Journal)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(ErrorCall
e :: C.ErrorCall) -> Either String Journal -> IO (Either String Journal)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Journal -> IO (Either String Journal))
-> Either String Journal -> IO (Either String Journal)
forall a b. (a -> b) -> a -> b
$ String -> Either String Journal
forall a b. a -> Either a b
Left (String -> Either String Journal)
-> String -> Either String Journal
forall a b. (a -> b) -> a -> b
$ ErrorCall -> String
forall a. Show a => a -> String
show ErrorCall
e)
,(IOException -> IO (Either String Journal))
-> Handler (Either String Journal)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(IOException
e :: C.IOException) -> Either String Journal -> IO (Either String Journal)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Journal -> IO (Either String Journal))
-> Either String Journal -> IO (Either String Journal)
forall a b. (a -> b) -> a -> b
$ String -> Either String Journal
forall a b. a -> Either a b
Left (String -> Either String Journal)
-> String -> Either String Journal
forall a b. (a -> b) -> a -> b
$ IOException -> String
forall a. Show a => a -> String
show IOException
e)
]
defaultJournalPath :: IO String
defaultJournalPath :: IO String
defaultJournalPath = do
p <- String -> IO String
getEnv String
journalEnvVar IO String -> (IOException -> IO String) -> IO String
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`C.catch` (\(IOException
_::C.IOException) -> String -> IO String
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return String
"")
if null p
then do
homedir <- fromMaybe "" <$> getHomeSafe
let defaultfile = String
homedir String -> String -> String
</> String
journalDefaultFilename
return defaultfile
else
expandPathOrGlob "." p
defaultJournalPathSafely :: IO (Either String String)
defaultJournalPathSafely :: IO (Either String String)
defaultJournalPathSafely = (do
f <- IO String
defaultJournalPath
return $ Right f
)
IO (Either String String)
-> [Handler (Either String String)] -> IO (Either String String)
forall a. IO a -> [Handler a] -> IO a
`C.catches` [
(ErrorCall -> IO (Either String String))
-> Handler (Either String String)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(ErrorCall
e :: C.ErrorCall) -> Either String String -> IO (Either String String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String String -> IO (Either String String))
-> Either String String -> IO (Either String String)
forall a b. (a -> b) -> a -> b
$ String -> Either String String
forall a b. a -> Either a b
Left (String -> Either String String) -> String -> Either String String
forall a b. (a -> b) -> a -> b
$ ErrorCall -> String
forall a. Show a => a -> String
show ErrorCall
e)
,(IOException -> IO (Either String String))
-> Handler (Either String String)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(IOException
e :: C.IOException) -> Either String String -> IO (Either String String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String String -> IO (Either String String))
-> Either String String -> IO (Either String String)
forall a b. (a -> b) -> a -> b
$ String -> Either String String
forall a b. a -> Either a b
Left (String -> Either String String) -> String -> Either String String
forall a b. (a -> b) -> a -> b
$ IOException -> String
forall a. Show a => a -> String
show IOException
e)
]
defaultExistingJournalPath :: IO String
defaultExistingJournalPath :: IO String
defaultExistingJournalPath = do
f <- IO String
defaultJournalPath
requireJournalFileExists f
return f
defaultExistingJournalPathSafely :: IO (Either String String)
defaultExistingJournalPathSafely :: IO (Either String String)
defaultExistingJournalPathSafely = (do
f <- IO String
defaultExistingJournalPath
return $ Right f
)
IO (Either String String)
-> [Handler (Either String String)] -> IO (Either String String)
forall a. IO a -> [Handler a] -> IO a
`C.catches` [
(ErrorCall -> IO (Either String String))
-> Handler (Either String String)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(ErrorCall
e :: C.ErrorCall) -> Either String String -> IO (Either String String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String String -> IO (Either String String))
-> Either String String -> IO (Either String String)
forall a b. (a -> b) -> a -> b
$ String -> Either String String
forall a b. a -> Either a b
Left (String -> Either String String) -> String -> Either String String
forall a b. (a -> b) -> a -> b
$ ErrorCall -> String
forall a. Show a => a -> String
show ErrorCall
e)
,(IOException -> IO (Either String String))
-> Handler (Either String String)
forall a e. Exception e => (e -> IO a) -> Handler a
C.Handler (\(IOException
e :: C.IOException) -> Either String String -> IO (Either String String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String String -> IO (Either String String))
-> Either String String -> IO (Either String String)
forall a b. (a -> b) -> a -> b
$ String -> Either String String
forall a b. a -> Either a b
Left (String -> Either String String) -> String -> Either String String
forall a b. (a -> b) -> a -> b
$ IOException -> String
forall a. Show a => a -> String
show IOException
e)
]
readJournal :: InputOpts -> Maybe FilePath -> Handle -> ExceptT String IO Journal
readJournal :: InputOpts -> Maybe String -> Handle -> ExceptT String IO Journal
readJournal iopts :: InputOpts
iopts@InputOpts{Bool
strict_ :: Bool
strict_ :: InputOpts -> Bool
strict_, Bool
_defer :: Bool
_defer :: InputOpts -> Bool
_defer} Maybe String
mpath Handle
hdl = do
let Reader IO
r :: Reader IO = Reader IO -> Maybe (Reader IO) -> Reader IO
forall a. a -> Maybe a -> a
fromMaybe Reader IO
forall (m :: * -> *). MonadIO m => Reader m
JournalReader.reader (Maybe (Reader IO) -> Reader IO) -> Maybe (Reader IO) -> Reader IO
forall a b. (a -> b) -> a -> b
$ Maybe StorageFormat -> Maybe String -> Maybe (Reader IO)
forall (m :: * -> *).
MonadIO m =>
Maybe StorageFormat -> Maybe String -> Maybe (Reader m)
findReader (InputOpts -> Maybe StorageFormat
mformat_ InputOpts
iopts) Maybe String
mpath
String -> StorageFormat -> ExceptT String IO ()
forall (m :: * -> *) a. (MonadIO m, Show a) => String -> a -> m ()
dbg6IO String
"readJournal: trying reader" (Reader IO -> StorageFormat
forall (m :: * -> *). Reader m -> StorageFormat
rFormat Reader IO
r)
j <- Reader IO
-> InputOpts -> String -> Handle -> ExceptT String IO Journal
forall (m :: * -> *).
Reader m
-> InputOpts -> String -> Handle -> ExceptT String IO Journal
rReadFn Reader IO
r InputOpts
iopts (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"-" Maybe String
mpath) Handle
hdl
when (strict_ && not _defer) $ liftEither $ journalStrictChecks j
return j
readJournalFile :: InputOpts -> PrefixedFilePath -> ExceptT String IO Journal
readJournalFile :: InputOpts -> String -> ExceptT String IO Journal
readJournalFile iopts :: InputOpts
iopts@InputOpts{Bool
new_ :: Bool
new_ :: InputOpts -> Bool
new_, Bool
new_save_ :: Bool
new_save_ :: InputOpts -> Bool
new_save_, Bool
_defer :: InputOpts -> Bool
_defer :: Bool
_defer} String
prefixedfile = do
(j, mlatestdates) <- InputOpts
-> String -> ExceptT String IO (Journal, Maybe LatestDatesForFile)
readJournalFileAndLatestDates InputOpts
iopts String
prefixedfile
when (new_ && new_save_ && not _defer) $ liftIO $
case mlatestdates of
Maybe LatestDatesForFile
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just (LatestDatesForFile String
f LatestDates
ds) -> LatestDates -> String -> IO ()
saveLatestDates LatestDates
ds String
f
return j
readJournalFileAndLatestDates :: InputOpts -> PrefixedFilePath -> ExceptT String IO (Journal, Maybe LatestDatesForFile)
readJournalFileAndLatestDates :: InputOpts
-> String -> ExceptT String IO (Journal, Maybe LatestDatesForFile)
readJournalFileAndLatestDates InputOpts
iopts String
prefixedfile = do
let
(Maybe StorageFormat
mfmt, String
f) = String -> (Maybe StorageFormat, String)
splitReaderPrefix String
prefixedfile
iopts' :: InputOpts
iopts' = InputOpts
iopts{mformat_=asum [mfmt, mformat_ iopts]}
moverride <- IO (Maybe Text) -> ExceptT String IO (Maybe Text)
forall a. IO a -> ExceptT String IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe Text) -> ExceptT String IO (Maybe Text))
-> IO (Maybe Text) -> ExceptT String IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ InputOpts -> String -> IO (Maybe Text)
fileOverride InputOpts
iopts String
f
h <-
dbg6Msg ("readJournalFile: "++takeFileName f) $
liftIO $ case moverride of
Just Text
t -> Text -> IO Handle
textToHandle Text
t
Maybe Text
Nothing -> String -> IO ()
requireJournalFileExists String
f IO () -> IO Handle -> IO Handle
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> IO Handle
openFileOrStdin String
f
j <- readJournal iopts' (Just f) h
if new_ iopts
then do
ds <- liftIO $ previousLatestDates f
let (newj, newds) = journalFilterSinceLatestDates ds j
return (newj, Just $ LatestDatesForFile f newds)
else
return (j, Nothing)
readPossibleJournalFile :: InputOpts -> PrefixedFilePath -> ExceptT String IO Journal
readPossibleJournalFile :: InputOpts -> String -> ExceptT String IO Journal
readPossibleJournalFile InputOpts
iopts String
prefixedfile = do
let (Maybe StorageFormat
_, String
f) = String -> (Maybe StorageFormat, String)
splitReaderPrefix String
prefixedfile
if String
f String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"-"
then InputOpts -> String -> ExceptT String IO Journal
readJournalFile InputOpts
iopts String
prefixedfile
else do
exists <- IO Bool -> ExceptT String IO Bool
forall a. IO a -> ExceptT String IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT String IO Bool)
-> IO Bool -> ExceptT String IO Bool
forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
f
if exists
then readJournalFile iopts prefixedfile
else return $ nulljournal{jfiles = [JournalFile f "" 0]}
readPossibleJournalFiles :: InputOpts -> [PrefixedFilePath] -> ExceptT String IO Journal
readPossibleJournalFiles :: InputOpts -> [String] -> ExceptT String IO Journal
readPossibleJournalFiles InputOpts
iopts [String]
pfs = case [String]
pfs of
[] -> Journal -> ExceptT String IO Journal
forall a. a -> ExceptT String IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Journal
nulljournal
(String
f:[String]
fs) -> do
let iopts' :: InputOpts
iopts' = InputOpts
iopts{_defer=True}
j1 <- InputOpts -> String -> ExceptT String IO Journal
readPossibleJournalFile InputOpts
iopts' String
f
js <- mapM (readJournalFile iopts') fs
let combined = Journal -> Journal
journalNumberAndTieTransactions (Journal -> Journal) -> Journal -> Journal
forall a b. (a -> b) -> a -> b
$ NonEmpty Journal -> Journal
forall a. Semigroup a => NonEmpty a -> a
sconcat (Journal
j1 Journal -> [Journal] -> NonEmpty Journal
forall a. a -> [a] -> NonEmpty a
:| [Journal]
js)
when (strict_ iopts) $ liftEither $ journalStrictChecks combined
return combined
readJournalFiles :: InputOpts -> [PrefixedFilePath] -> ExceptT String IO Journal
readJournalFiles :: InputOpts -> [String] -> ExceptT String IO Journal
readJournalFiles iopts :: InputOpts
iopts@InputOpts{Bool
strict_ :: InputOpts -> Bool
strict_ :: Bool
strict_, Bool
new_ :: InputOpts -> Bool
new_ :: Bool
new_, Bool
new_save_ :: InputOpts -> Bool
new_save_ :: Bool
new_save_, Bool
_scanfiles :: Bool
_scanfiles :: InputOpts -> Bool
_scanfiles} [String]
prefixedfiles = do
let iopts' :: InputOpts
iopts' = InputOpts
iopts{_defer=True}
(j, latestdatesforfiles) <-
String
-> ExceptT String IO (Journal, [LatestDatesForFile])
-> ExceptT String IO (Journal, [LatestDatesForFile])
forall a. String -> a -> a
dbg6Msg (String
"readJournalFiles: "String -> String -> String
forall a. [a] -> [a] -> [a]
++[String] -> String
forall a. Show a => a -> String
show [String]
prefixedfiles) (ExceptT String IO (Journal, [LatestDatesForFile])
-> ExceptT String IO (Journal, [LatestDatesForFile]))
-> ExceptT String IO (Journal, [LatestDatesForFile])
-> ExceptT String IO (Journal, [LatestDatesForFile])
forall a b. (a -> b) -> a -> b
$
InputOpts
-> [String] -> ExceptT String IO (Journal, [LatestDatesForFile])
readJournalFilesAndLatestDates InputOpts
iopts' [String]
prefixedfiles
when (strict_ && not _scanfiles) $ liftEither $ journalStrictChecks j
when (new_ && new_save_) $ liftIO $ saveLatestDatesForFiles latestdatesforfiles
return j
readJournalFilesAndLatestDates :: InputOpts -> [PrefixedFilePath] -> ExceptT String IO (Journal, [LatestDatesForFile])
readJournalFilesAndLatestDates :: InputOpts
-> [String] -> ExceptT String IO (Journal, [LatestDatesForFile])
readJournalFilesAndLatestDates InputOpts
iopts [String]
pfs = do
(js, lastdates) <- [(Journal, Maybe LatestDatesForFile)]
-> ([Journal], [Maybe LatestDatesForFile])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Journal, Maybe LatestDatesForFile)]
-> ([Journal], [Maybe LatestDatesForFile]))
-> ExceptT String IO [(Journal, Maybe LatestDatesForFile)]
-> ExceptT String IO ([Journal], [Maybe LatestDatesForFile])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> ExceptT String IO (Journal, Maybe LatestDatesForFile))
-> [String]
-> ExceptT String IO [(Journal, Maybe LatestDatesForFile)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (InputOpts
-> String -> ExceptT String IO (Journal, Maybe LatestDatesForFile)
readJournalFileAndLatestDates InputOpts
iopts) [String]
pfs
mapM_ dbg1MsgIO $ commodityStyleConflicts js
let j = [Journal] -> Journal -> Journal
combineCommodityDeclarations [Journal]
js (Journal -> Journal) -> Journal -> Journal
forall a b. (a -> b) -> a -> b
$ Journal
-> (NonEmpty Journal -> Journal)
-> Maybe (NonEmpty Journal)
-> Journal
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Journal
forall a. Default a => a
def NonEmpty Journal -> Journal
forall a. Semigroup a => NonEmpty a -> a
sconcat (Maybe (NonEmpty Journal) -> Journal)
-> Maybe (NonEmpty Journal) -> Journal
forall a b. (a -> b) -> a -> b
$ [Journal] -> Maybe (NonEmpty Journal)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [Journal]
js
return (journalNumberAndTieTransactions j, catMaybes lastdates)
combineCommodityDeclarations :: [Journal] -> Journal -> Journal
combineCommodityDeclarations :: [Journal] -> Journal -> Journal
combineCommodityDeclarations [Journal]
js Journal
j = Journal
j{
jdeclaredcommodities = M.unionsWith commodityCombine $ reverse $ map jdeclaredcommodities js
,jdeclaredcommoditytags = M.unionsWith union $ reverse $ map jdeclaredcommoditytags js
}
commodityStyleConflicts :: [Journal] -> [String]
commodityStyleConflicts :: [Journal] -> [String]
commodityStyleConflicts [Journal]
js =
[ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (String
"commodity " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
sym String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" is declared with different display styles; using the last:")
String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [ String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SourcePos -> String
sourcePosFileLine (Commodity -> SourcePos
csourcepos Commodity
c) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Amount -> String
showAmount Amount
nullamt{acommodity=sym, aquantity=1000, astyle=st}
| Commodity
c <- [Commodity]
cs, Just AmountStyle
st <- [Commodity -> Maybe AmountStyle
cformat Commodity
c] ]
| (Text
sym, [Commodity]
cs) <- Map Text [Commodity] -> [(Text, [Commodity])]
forall k a. Map k a -> [(k, a)]
M.toList Map Text [Commodity]
declsBySymbol
, [AmountStyle] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([AmountStyle] -> [AmountStyle]
forall a. Ord a => [a] -> [a]
nubOrd [AmountStyle
st{asrounding=NoRounding} | Commodity
c <- [Commodity]
cs, Just AmountStyle
st <- [Commodity -> Maybe AmountStyle
cformat Commodity
c]]) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
]
where
declsBySymbol :: Map Text [Commodity]
declsBySymbol = ([Commodity] -> [Commodity] -> [Commodity])
-> [(Text, [Commodity])] -> Map Text [Commodity]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith (([Commodity] -> [Commodity] -> [Commodity])
-> [Commodity] -> [Commodity] -> [Commodity]
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Commodity] -> [Commodity] -> [Commodity]
forall a. Semigroup a => a -> a -> a
(<>)) [(Commodity -> Text
csymbol Commodity
c, [Commodity
c]) | Journal
j <- [Journal]
js, Commodity
c <- Map Text Commodity -> [Commodity]
forall k a. Map k a -> [a]
M.elems (Map Text Commodity -> [Commodity])
-> Map Text Commodity -> [Commodity]
forall a b. (a -> b) -> a -> b
$ Journal -> Map Text Commodity
jdeclaredcommodities Journal
j, Maybe AmountStyle -> Bool
forall a. Maybe a -> Bool
isJust (Maybe AmountStyle -> Bool) -> Maybe AmountStyle -> Bool
forall a b. (a -> b) -> a -> b
$ Commodity -> Maybe AmountStyle
cformat Commodity
c]
readJournal' :: Handle -> IO Journal
readJournal' :: Handle -> IO Journal
readJournal' = ExceptT String IO Journal -> IO Journal
forall (m :: * -> *) a. MonadIO m => ExceptT String m a -> m a
orDieTrying (ExceptT String IO Journal -> IO Journal)
-> (Handle -> ExceptT String IO Journal) -> Handle -> IO Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InputOpts -> Maybe String -> Handle -> ExceptT String IO Journal
readJournal InputOpts
definputopts Maybe String
forall a. Maybe a
Nothing
readJournal'' :: Text -> IO Journal
readJournal'' :: Text -> IO Journal
readJournal'' = Text -> IO Handle
textToHandle (Text -> IO Handle) -> (Handle -> IO Journal) -> Text -> IO Journal
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Handle -> IO Journal
readJournal'
readJournalFile' :: PrefixedFilePath -> IO Journal
readJournalFile' :: String -> IO Journal
readJournalFile' = ExceptT String IO Journal -> IO Journal
forall (m :: * -> *) a. MonadIO m => ExceptT String m a -> m a
orDieTrying (ExceptT String IO Journal -> IO Journal)
-> (String -> ExceptT String IO Journal) -> String -> IO Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InputOpts -> String -> ExceptT String IO Journal
readJournalFile InputOpts
definputopts
readJournalFiles' :: [PrefixedFilePath] -> IO Journal
readJournalFiles' :: [String] -> IO Journal
readJournalFiles' = ExceptT String IO Journal -> IO Journal
forall (m :: * -> *) a. MonadIO m => ExceptT String m a -> m a
orDieTrying (ExceptT String IO Journal -> IO Journal)
-> ([String] -> ExceptT String IO Journal)
-> [String]
-> IO Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InputOpts -> [String] -> ExceptT String IO Journal
readJournalFiles InputOpts
definputopts
orDieTrying :: MonadIO m => ExceptT String m a -> m a
orDieTrying :: forall (m :: * -> *) a. MonadIO m => ExceptT String m a -> m a
orDieTrying ExceptT String m a
a = (String -> m a) -> (a -> m a) -> Either String a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (String -> IO a) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO a
forall a. HasCallStack => String -> IO a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
String -> m a
fail) a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String a -> m a) -> m (Either String a) -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ExceptT String m a -> m (Either String a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT String m a
a
requireJournalFileExists :: FilePath -> IO ()
requireJournalFileExists :: String -> IO ()
requireJournalFileExists String
"-" = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
requireJournalFileExists String
f = do
exists <- String -> IO Bool
doesFileExist String
f
unless exists $ error' $ unlines
[ "data file \"" <> f <> "\" was not found."
,"Please create it first, eg with \"hledger add\" or a text editor."
,"Or, specify an existing data file with -f or $LEDGER_FILE."
]
ensureJournalFileExists :: FilePath -> IO ()
ensureJournalFileExists :: String -> IO ()
ensureJournalFileExists String
f = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (String
osString -> String -> Bool
forall a. Eq a => a -> a -> Bool
==String
"mingw32" Bool -> Bool -> Bool
&& String -> Bool
isWindowsUnsafeDotPath String
f) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
String -> IO ()
forall a. String -> a
error' (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Part of file path \"" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
f String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\"\n ends with a dot, which is unsafe on Windows; please use a different path.\n"
exists <- String -> IO Bool
doesFileExist String
f
unless exists $ do
hPutStrLn stderr $ "Creating hledger journal file " <> show f
let dir = String -> String
takeDirectory String
f
unless (null dir || dir == ".") $
createDirectoryIfMissing True dir
newJournalContent >>= T.writeFile f
isWindowsUnsafeDotPath :: FilePath -> Bool
isWindowsUnsafeDotPath :: String -> Bool
isWindowsUnsafeDotPath = (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\String
x -> String -> Char
forall a. HasCallStack => [a] -> a
last String
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.' Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
'.') String
x) ([String] -> Bool) -> (String -> [String]) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
splitDirectories
newJournalContent :: IO Text
newJournalContent :: IO Text
newJournalContent = do
d <- IO Day
getCurrentDay
return $ "; journal created " <> T.pack (show d) <> " by hledger\n"
tests_Read :: TestTree
tests_Read = String -> [TestTree] -> TestTree
testGroup String
"Read" [
TestTree
tests_Common
,TestTree
tests_CsvReader
,TestTree
tests_JournalReader
,TestTree
tests_RulesReader
]