{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Hledger.Read.CsvReader (
reader,
tests_CsvReader,
)
where
import Prelude hiding (Applicative(..))
import Control.Monad.Except (ExceptT(..), liftEither, withExceptT)
import Control.Monad.IO.Class (MonadIO)
import Data.Encoding (DynEncoding)
import Data.Text (Text)
import System.IO (Handle, IOMode(..), openFile)
import Hledger.Data
import Hledger.Utils
import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), includeFileParser, journalFinalise)
import Hledger.Read.RulesReader (readParsedJournalFromCsv, getRulesFile, rulesEncoding, readRules)
import Control.Monad.Trans (lift)
reader :: MonadIO m => SepFormat -> Reader m
reader :: forall (m :: * -> *). MonadIO m => SepFormat -> Reader m
reader SepFormat
sep = Reader
{rFormat :: StorageFormat
rFormat = SepFormat -> StorageFormat
Sep SepFormat
sep
,rExtensions :: [String]
rExtensions = [SepFormat -> String
forall a. Show a => a -> String
show SepFormat
sep]
,rReadFn :: InputOpts -> String -> Handle -> ExceptT String IO Journal
rReadFn = SepFormat
-> InputOpts -> String -> Handle -> ExceptT String IO Journal
parse SepFormat
sep
,rParser :: MonadIO m => InputOpts -> ErroringJournalParser m Journal
rParser = \InputOpts
iopts -> (String -> ExceptT String IO Journal)
-> ErroringJournalParser m Journal
forall (m :: * -> *).
MonadIO m =>
(String -> ExceptT String IO Journal)
-> ErroringJournalParser m Journal
includeFileParser ((String -> ExceptT String IO Journal)
-> ErroringJournalParser m Journal)
-> (String -> ExceptT String IO Journal)
-> ErroringJournalParser m Journal
forall a b. (a -> b) -> a -> b
$ \String
f ->
(Journal, String -> String) -> Journal
forall a b. (a, b) -> a
fst ((Journal, String -> String) -> Journal)
-> ExceptT String IO (Journal, String -> String)
-> ExceptT String IO Journal
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SepFormat
-> Bool
-> Maybe String
-> String
-> (Maybe DynEncoding -> IO Text)
-> ExceptT String IO (Journal, String -> String)
readCsvWith SepFormat
sep (InputOpts -> Bool
_scanfiles InputOpts
iopts) Maybe String
forall a. Maybe a
Nothing String
f (\Maybe DynEncoding
enc -> String -> IOMode -> IO Handle
openFile String
f IOMode
ReadMode IO Handle -> (Handle -> IO Text) -> IO Text
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe DynEncoding -> Handle -> IO Text
hGetContentsPortably Maybe DynEncoding
enc)
}
parse :: SepFormat -> InputOpts -> FilePath -> Handle -> ExceptT String IO Journal
parse :: SepFormat
-> InputOpts -> String -> Handle -> ExceptT String IO Journal
parse SepFormat
sep InputOpts
iopts String
f Handle
h = do
(j1, adderrorcontext) <- SepFormat
-> Bool
-> Maybe String
-> String
-> (Maybe DynEncoding -> IO Text)
-> ExceptT String IO (Journal, String -> String)
readCsvWith SepFormat
sep (InputOpts -> Bool
_scanfiles InputOpts
iopts) (InputOpts -> Maybe String
mrules_file_ InputOpts
iopts) String
f (Maybe DynEncoding -> Handle -> IO Text
`hGetContentsPortably` Handle
h)
j2 <- liftEither $ journalApplyAliases (aliasesFromOpts iopts) j1
withExceptT adderrorcontext $
journalFinalise iopts{balancingopts_=(balancingopts_ iopts){ignore_assertions_=True}} f "" j2
readCsvWith :: SepFormat -> Bool -> Maybe FilePath -> FilePath -> (Maybe DynEncoding -> IO Text)
-> ExceptT String IO (ParsedJournal, String -> String)
readCsvWith :: SepFormat
-> Bool
-> Maybe String
-> String
-> (Maybe DynEncoding -> IO Text)
-> ExceptT String IO (Journal, String -> String)
readCsvWith SepFormat
sep Bool
scanning Maybe String
mrulesfile String
f Maybe DynEncoding -> IO Text
readdata = do
let rulesfile :: String
rulesfile = String -> Maybe String -> String
getRulesFile String
f Maybe String
mrulesfile
(rules, rulesfiles) <- String -> ExceptT String IO (CsvRules, [String])
readRules String
rulesfile
mencoding <- rulesEncoding rulesfile rules
csvtext <- if scanning then return "" else lift $ readdata mencoding
readParsedJournalFromCsv rulesfile rules (f, rulesfiles) f csvtext (Just sep)
tests_CsvReader :: TestTree
tests_CsvReader = String -> [TestTree] -> TestTree
testGroup String
"CsvReader" [
]