@@ -4,11 +4,11 @@ module Hledger.Flow.CSVImport
4
4
( importCSVs
5
5
) where
6
6
7
- import Turtle
7
+ import Turtle hiding ( stdout , stderr , proc , procStrictWithErr )
8
8
import Prelude hiding (FilePath , putStrLn , take )
9
9
import qualified Data.Text as T
10
10
import qualified Data.List.NonEmpty as NonEmpty
11
- import Hledger.Flow.Types ( LogMessage )
11
+ import qualified Hledger.Flow.Types as FlowTypes
12
12
import Hledger.Flow.Import.Types
13
13
import Hledger.Flow.Common
14
14
import Control.Concurrent.STM
@@ -18,10 +18,10 @@ importCSVs opts = sh (
18
18
do
19
19
ch <- liftIO newTChanIO
20
20
logHandle <- fork $ consoleChannelLoop ch
21
- liftIO $ if (showOptions opts) then channelOut ch (repr opts) else return ()
21
+ liftIO $ if (showOptions opts) then channelOutLn ch (repr opts) else return ()
22
22
liftIO $ logVerbose opts ch " Starting import"
23
23
(journals, diff) <- time $ liftIO $ importCSVs' opts ch
24
- liftIO $ channelOut ch $ format (" Imported " % d% " journals in " % s) (length journals) $ repr diff
24
+ liftIO $ channelOutLn ch $ format (" Imported " % d% " journals in " % s) (length journals) $ repr diff
25
25
liftIO $ terminateChannelLoop ch
26
26
wait logHandle
27
27
)
@@ -32,9 +32,9 @@ pathSeparators = ['/', '\\', ':']
32
32
inputFilePattern :: Pattern Text
33
33
inputFilePattern = contains (once (oneOf pathSeparators) <> asciiCI " 1-in" <> once (oneOf pathSeparators) <> plus digit <> once (oneOf pathSeparators))
34
34
35
- importCSVs' :: ImportOptions -> TChan LogMessage -> IO [FilePath ]
35
+ importCSVs' :: ImportOptions -> TChan FlowTypes. LogMessage -> IO [FilePath ]
36
36
importCSVs' opts ch = do
37
- channelOut ch " Collecting input files..."
37
+ channelOutLn ch " Collecting input files..."
38
38
(inputFiles, diff) <- time $ single . shellToList . onlyFiles $ find inputFilePattern $ baseDir opts
39
39
let fileCount = length inputFiles
40
40
if (fileCount == 0 ) then
@@ -45,20 +45,20 @@ importCSVs' opts ch = do
45
45
errExit 1 ch msg []
46
46
else
47
47
do
48
- channelOut ch $ format (" Found " % d% " input files in " % s% " . Proceeding with import..." ) fileCount (repr diff)
48
+ channelOutLn ch $ format (" Found " % d% " input files in " % s% " . Proceeding with import..." ) fileCount (repr diff)
49
49
let actions = map (extractAndImport opts ch) inputFiles :: [IO FilePath ]
50
50
importedJournals <- if (sequential opts) then sequence actions else single . shellToList $ parallel actions
51
51
sh $ writeIncludesUpTo opts ch " import" importedJournals
52
52
return importedJournals
53
53
54
- extractAndImport :: ImportOptions -> TChan LogMessage -> FilePath -> IO FilePath
54
+ extractAndImport :: ImportOptions -> TChan FlowTypes. LogMessage -> FilePath -> IO FilePath
55
55
extractAndImport opts ch inputFile = do
56
56
case extractImportDirs inputFile of
57
57
Right importDirs -> importCSV opts ch importDirs inputFile
58
58
Left errorMessage -> do
59
59
errExit 1 ch errorMessage inputFile
60
60
61
- importCSV :: ImportOptions -> TChan LogMessage -> ImportDirs -> FilePath -> IO FilePath
61
+ importCSV :: ImportOptions -> TChan FlowTypes. LogMessage -> ImportDirs -> FilePath -> IO FilePath
62
62
importCSV opts ch importDirs srcFile = do
63
63
let preprocessScript = accountDir importDirs </> " preprocess"
64
64
let constructScript = accountDir importDirs </> " construct"
@@ -74,43 +74,46 @@ importCSV opts ch importDirs srcFile = do
74
74
mktree $ directory journalOut
75
75
importFun csvFile journalOut
76
76
77
- preprocessIfNeeded :: ImportOptions -> TChan LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> IO FilePath
77
+ preprocessIfNeeded :: ImportOptions -> TChan FlowTypes. LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> IO FilePath
78
78
preprocessIfNeeded opts ch script bank account owner src = do
79
79
shouldPreprocess <- verboseTestFile opts ch script
80
80
if shouldPreprocess
81
81
then preprocess opts ch script bank account owner src
82
82
else return src
83
83
84
- preprocess :: ImportOptions -> TChan LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> IO FilePath
84
+ preprocess :: ImportOptions -> TChan FlowTypes. LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> IO FilePath
85
85
preprocess opts ch script bank account owner src = do
86
86
let csvOut = changePathAndExtension " 2-preprocessed" " csv" src
87
87
mktree $ directory csvOut
88
88
let script' = format fp script :: Text
89
- let action = proc script' [format fp src, format fp csvOut, lineToText bank, lineToText account, lineToText owner] empty
89
+ let action = (parAwareProc opts) script' [format fp src, format fp csvOut, lineToText bank, lineToText account, lineToText owner] empty
90
90
let relScript = relativeToBase opts script
91
91
let relSrc = relativeToBase opts src
92
92
let msg = format (" executing '" % fp% " ' on '" % fp% " '" ) relScript relSrc
93
- _ <- logVerboseTime opts ch msg action
93
+ ((_, stdOut, _), _) <- timeAndExitOnErr opts ch msg action
94
+ channelOut ch stdOut
94
95
return csvOut
95
96
96
- hledgerImport :: ImportOptions -> TChan LogMessage -> FilePath -> FilePath -> IO FilePath
97
+ hledgerImport :: ImportOptions -> TChan FlowTypes. LogMessage -> FilePath -> FilePath -> IO FilePath
97
98
hledgerImport opts ch csvSrc journalOut = do
98
99
case extractImportDirs csvSrc of
99
100
Right importDirs -> hledgerImport' opts ch importDirs csvSrc journalOut
100
101
Left errorMessage -> do
101
102
errExit 1 ch errorMessage csvSrc
102
103
103
- hledgerImport' :: ImportOptions -> TChan LogMessage -> ImportDirs -> FilePath -> FilePath -> IO FilePath
104
+ hledgerImport' :: ImportOptions -> TChan FlowTypes. LogMessage -> ImportDirs -> FilePath -> FilePath -> IO FilePath
104
105
hledgerImport' opts ch importDirs csvSrc journalOut = do
105
106
let candidates = rulesFileCandidates csvSrc importDirs
106
107
maybeRulesFile <- firstExistingFile candidates
107
108
let relCSV = relativeToBase opts csvSrc
108
109
case maybeRulesFile of
109
110
Just rf -> do
110
111
let relRules = relativeToBase opts rf
111
- let action = proc " hledger" [" print" , " --rules-file" , format fp rf, " --file" , format fp csvSrc, " --output-file" , format fp journalOut] empty
112
+ let hledger = format fp $ FlowTypes. hlPath . hledgerInfo $ opts :: Text
113
+ let action = (parAwareProc opts) hledger [" print" , " --rules-file" , format fp rf, " --file" , format fp csvSrc, " --output-file" , format fp journalOut] empty
112
114
let msg = format (" importing '" % fp% " ' using rules file '" % fp% " '" ) relCSV relRules
113
- _ <- logVerboseTime opts ch msg action
115
+ ((_, stdOut, _), _) <- timeAndExitOnErr opts ch msg action
116
+ channelOut ch stdOut
114
117
return journalOut
115
118
Nothing ->
116
119
do
@@ -151,14 +154,15 @@ statementSpecificRulesFiles csvSrc importDirs = do
151
154
map (</> srcSpecificFilename) [accountDir importDirs, bankDir importDirs, importDir importDirs]
152
155
else []
153
156
154
- customConstruct :: ImportOptions -> TChan LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> FilePath -> IO FilePath
157
+ customConstruct :: ImportOptions -> TChan FlowTypes. LogMessage -> FilePath -> Line -> Line -> Line -> FilePath -> FilePath -> IO FilePath
155
158
customConstruct opts ch constructScript bank account owner csvSrc journalOut = do
156
159
let script = format fp constructScript :: Text
157
160
let importOut = inproc script [format fp csvSrc, " -" , lineToText bank, lineToText account, lineToText owner] empty
158
- let action = proc " hledger" [" print" , " --ignore-assertions" , " --file" , " -" , " --output-file" , format fp journalOut] importOut
161
+ let hledger = format fp $ FlowTypes. hlPath . hledgerInfo $ opts :: Text
162
+ let action = (parAwareProc opts) hledger [" print" , " --ignore-assertions" , " --file" , " -" , " --output-file" , format fp journalOut] importOut
159
163
let relScript = relativeToBase opts constructScript
160
164
let relSrc = relativeToBase opts csvSrc
161
165
let msg = format (" executing '" % fp% " ' on '" % fp% " '" ) relScript relSrc
162
- _ <- logVerboseTime opts ch msg action
163
-
166
+ ((_, stdOut, _), _) <- timeAndExitOnErr opts ch msg action
167
+ channelOut ch stdOut
164
168
return journalOut
0 commit comments