hledger/hledger-lib/Hledger/Read/CsvReader.hs

1023 lines
39 KiB
Haskell
Raw Normal View History

{-|
A reader for CSV data, using an extra rules file to help interpret the data.
-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PackageImports #-}
module Hledger.Read.CsvReader (
-- * Reader
reader,
2012-05-30 01:00:49 +04:00
-- * Misc.
CsvRecord,
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
CSV, Record, Field,
-- rules,
2012-05-30 01:00:49 +04:00
rulesFileFor,
parseRulesFile,
parseAndValidateCsvRules,
expandIncludes,
2012-05-30 01:00:49 +04:00
transactionFromCsvRecord,
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
printCSV,
-- * Tests
2018-09-06 23:08:26 +03:00
tests_CsvReader,
)
where
import Prelude ()
import "base-compat-batteries" Prelude.Compat hiding (fail)
import qualified "base-compat-batteries" Control.Monad.Fail.Compat as Fail (fail)
2019-09-14 04:11:40 +03:00
import Control.Exception (IOException, handle, throw)
import Control.Monad (liftM, unless, when)
import Control.Monad.Except (ExceptT, throwError)
2019-09-14 04:11:40 +03:00
import Control.Monad.IO.Class (liftIO)
import Control.Monad.State.Strict (StateT, get, modify', evalStateT)
2019-09-14 04:11:40 +03:00
import Control.Monad.Trans.Class (lift)
import Data.Char (toLower, isDigit, isSpace, ord)
import Data.Bifunctor (first)
import "base-compat-batteries" Data.List.Compat
import Data.Maybe
import Data.Ord
import qualified Data.Set as S
lib: textification: parse stream 10% more allocation, but 35% lower maximum residency, and slightly quicker. hledger -f data/100x100x10.journal stats <<ghc: 39327768 bytes, 77 GCs, 196834/269496 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.010 elapsed), 0.020 MUT (0.092 elapsed), 0.014 GC (0.119 elapsed) :ghc>> <<ghc: 42842136 bytes, 84 GCs, 194010/270912 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.009 elapsed), 0.016 MUT (0.029 elapsed), 0.012 GC (0.120 elapsed) :ghc>> hledger -f data/1000x1000x10.journal stats <<ghc: 314291440 bytes, 612 GCs, 2070776/6628048 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.128 MUT (0.144 elapsed), 0.059 GC (0.070 elapsed) :ghc>> <<ghc: 349558872 bytes, 681 GCs, 1397597/4106384 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.004 elapsed), 0.124 MUT (0.133 elapsed), 0.047 GC (0.053 elapsed) :ghc>> hledger -f data/10000x1000x10.journal stats <<ghc: 3070026824 bytes, 5973 GCs, 12698030/62951784 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.268 MUT (1.354 elapsed), 0.514 GC (0.587 elapsed) :ghc>> <<ghc: 3424013128 bytes, 6658 GCs, 11405501/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.001 elapsed), 1.343 MUT (1.406 elapsed), 0.511 GC (0.573 elapsed) :ghc>> hledger -f data/100000x1000x10.journal stats <<ghc: 30753387392 bytes, 59811 GCs, 117615462/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.000 elapsed), 12.068 MUT (12.238 elapsed), 6.015 GC (7.190 elapsed) :ghc>> <<ghc: 34306530696 bytes, 66727 GCs, 76806196/414629312 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.010 elapsed), 14.357 MUT (16.370 elapsed), 5.298 GC (6.534 elapsed) :ghc>>
2016-05-25 01:58:23 +03:00
import Data.Text (Text)
lib: textification begins! account names The first of several conversions from String to (strict) Text, hopefully reducing space and time usage. This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1: hledger -f data/100x100x10.journal stats string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>> text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>> hledger -f data/1000x100x10.journal stats string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>> text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>> hledger -f data/10000x100x10.journal stats string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>> text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>> hledger -f data/100000x100x10.journal stats string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>> text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
import qualified Data.Text as T
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Time.Calendar (Day)
2015-03-28 01:42:32 +03:00
#if MIN_VERSION_time(1,5,0)
import Data.Time.Format (parseTimeM, defaultTimeLocale)
2015-03-28 01:42:32 +03:00
#else
import Data.Time.Format (parseTime)
2015-03-28 01:42:32 +03:00
import System.Locale (defaultTimeLocale)
#endif
import Safe
import System.Directory (doesFileExist)
import System.FilePath
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
import qualified Data.Csv as Cassava
import qualified Data.Csv.Parser.Megaparsec as CassavaMP
import qualified Data.ByteString as B
2018-09-30 04:32:08 +03:00
import qualified Data.ByteString.Lazy as BL
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
import Data.Foldable
import Text.Megaparsec hiding (parse)
import Text.Megaparsec.Char
2018-09-30 04:32:08 +03:00
import Text.Megaparsec.Custom
import Text.Printf (printf)
import Hledger.Data
import Hledger.Utils
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos, getSpecialSeparators, finaliseJournal)
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
type CSV = [Record]
type Record = [Field]
type Field = String
reader :: Reader
reader = Reader
{rFormat = "csv"
,rExtensions = ["csv"]
,rParser = parse
,rExperimental = False
}
-- | Parse and post-process a "Journal" from CSV data, or give an error.
-- Does not check balance assertions.
-- XXX currently ignores the provided data, reads it from the file path instead.
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
parse iopts f t = do
let rulesfile = mrules_file_ iopts
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
let separator = separator_ iopts
r <- liftIO $ readJournalFromCsv separator rulesfile f t
case r of Left e -> throwError e
Right pj -> finaliseJournal iopts{ignore_assertions_=True} f t pj'
where
-- finaliseJournal assumes the journal's items are
-- reversed, as produced by JournalReader's parser.
-- But here they are already properly ordered. So we'd
-- better preemptively reverse them once more. XXX inefficient
pj' = journalReverse pj
-- | Decide which separator to get.
-- If the external separator is provided, take it. Otherwise, look at the rules. Finally, return ','.
getSeparator :: Maybe Char -> CsvRules -> Char
getSeparator externalSeparator rules = head $
catMaybes [ externalSeparator
, getDirective "separator" rules >>= getSpecialSeparators
, Just ',']
2020-01-02 20:09:57 +03:00
-- | Read a Journal from the given CSV data (and filename, used for error
-- messages), or return an error. Proceed as follows:
-- @
2017-02-06 12:45:32 +03:00
-- 1. parse CSV conversion rules from the specified rules file, or from
-- the default rules file for the specified CSV file, if it exists,
-- or throw a parse error; if it doesn't exist, use built-in default rules
-- 2. parse the CSV data, or throw a parse error
-- 3. convert the CSV records to transactions using the rules
-- 4. if the rules file didn't exist, create it with the default rules and filename
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
-- 5. return the transactions as a Journal
-- @
readJournalFromCsv :: Maybe Char -> Maybe FilePath -> FilePath -> Text -> IO (Either String Journal)
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
readJournalFromCsv _ Nothing "-" _ = return $ Left "please use --rules-file when reading CSV from stdin"
readJournalFromCsv commandLineSeparator mrulesfile csvfile csvdata =
handle (\(e::IOException) -> return $ Left $ show e) $ do
-- make and throw an IO exception.. which we catch and convert to an Either above ?
let throwerr = throw . userError
-- parse the csv rules
let rulesfile = fromMaybe (rulesFileFor csvfile) mrulesfile
rulesfileexists <- doesFileExist rulesfile
rulestext <-
if rulesfileexists
then do
dbg1IO "using conversion rules file" rulesfile
readFilePortably rulesfile >>= expandIncludes (takeDirectory rulesfile)
2020-01-02 19:23:40 +03:00
else
return $ defaultRulesText rulesfile
rules <- either throwerr return $ parseAndValidateCsvRules rulesfile rulestext
2015-05-14 22:49:17 +03:00
dbg2IO "rules" rules
-- parse the skip directive's value, if any
let skiplines = case getDirective "skip" rules of
Nothing -> 0
Just "" -> 1
Just s -> readDef (throwerr $ "could not parse skip value: " ++ show s) s
-- parse csv
-- parsec seems to fail if you pass it "-" here TODO: try again with megaparsec
let parsecfilename = if csvfile == "-" then "(stdin)" else csvfile
records <- (either throwerr id .
dbg2 "validateCsv" . validateCsv rules skiplines .
2015-05-14 22:49:17 +03:00
dbg2 "parseCsv")
`fmap` parseCsv (getSeparator commandLineSeparator rules) parsecfilename csvdata
2015-05-14 22:49:17 +03:00
dbg1IO "first 3 csv records" $ take 3 records
-- identify header lines
-- let (headerlines, datalines) = identifyHeaderLines records
-- mfieldnames = lastMay headerlines
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
let
-- convert CSV records to transactions
txns = snd $ mapAccumL
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
(\pos r ->
let
SourcePos name line col = pos
line' = (mkPos . (+1) . unPos) line
pos' = SourcePos name line' col
in
(pos, transactionFromCsvRecord pos' rules r)
)
(initialPos parsecfilename) records
-- Ensure transactions are ordered chronologically.
-- First, if the CSV records seem to be most-recent-first (because
-- there's an explicit "newest-first" directive, or there's more
-- than one date and the first date is more recent than the last):
-- reverse them to get same-date transactions ordered chronologically.
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
txns' =
(if newestfirst || mseemsnewestfirst == Just True then reverse else id) txns
where
newestfirst = dbg3 "newestfirst" $ isJust $ getDirective "newest-first" rules
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
mseemsnewestfirst = dbg3 "mseemsnewestfirst" $
case nub $ map tdate txns of
ds | length ds > 1 -> Just $ head ds > last ds
_ -> Nothing
-- Second, sort by date.
txns'' = sortBy (comparing tdate) txns'
when (not rulesfileexists) $ do
dbg1IO "creating conversion rules file" rulesfile
writeFile rulesfile $ T.unpack rulestext
return $ Right nulljournal{jtxns=txns''}
parseCsv :: Char -> FilePath -> Text -> IO (Either String CSV)
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
parseCsv separator filePath csvdata =
case filePath of
"-" -> liftM (parseCassava separator "(stdin)") T.getContents
_ -> return $ parseCassava separator filePath csvdata
parseCassava :: Char -> FilePath -> Text -> Either String CSV
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
parseCassava separator path content =
either (Left . errorBundlePretty) (Right . parseResultToCsv) <$>
CassavaMP.decodeWith (decodeOptions separator) Cassava.NoHeader path $
BL.fromStrict $ T.encodeUtf8 content
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
decodeOptions :: Char -> Cassava.DecodeOptions
decodeOptions separator = Cassava.defaultDecodeOptions {
Cassava.decDelimiter = fromIntegral (ord separator)
}
parseResultToCsv :: (Foldable t, Functor t) => t (t B.ByteString) -> CSV
parseResultToCsv = toListList . unpackFields
where
toListList = toList . fmap toList
unpackFields = (fmap . fmap) (T.unpack . T.decodeUtf8)
printCSV :: CSV -> String
printCSV records = unlined (printRecord `map` records)
where printRecord = concat . intersperse "," . map printField
printField f = "\"" ++ concatMap escape f ++ "\""
escape '"' = "\"\""
escape x = [x]
unlined = concat . intersperse "\n"
-- | Return the cleaned up and validated CSV data (can be empty), or an error.
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]
validateCsv _ _ (Left err) = Left err
validateCsv rules numhdrlines (Right rs) = validate $ applyConditionalSkips $ drop numhdrlines $ filternulls rs
where
filternulls = filter (/=[""])
skipCount r =
case (getEffectiveAssignment rules r "end", getEffectiveAssignment rules r "skip") of
(Nothing, Nothing) -> Nothing
(Just _, _) -> Just maxBound
(Nothing, Just "") -> Just 1
(Nothing, Just x) -> Just (read x)
applyConditionalSkips [] = []
applyConditionalSkips (r:rest) =
case skipCount r of
Nothing -> r:(applyConditionalSkips rest)
Just cnt -> applyConditionalSkips (drop (cnt-1) rest)
validate [] = Right []
validate rs@(_first:_)
| isJust lessthan2 = let r = fromJust lessthan2 in
Left $ printf "CSV record %s has less than two fields" (show r)
| otherwise = Right rs
where
lessthan2 = headMay $ filter ((<2).length) rs
-- -- | The highest (0-based) field index referenced in the field
-- -- definitions, or -1 if no fields are defined.
-- maxFieldIndex :: CsvRules -> Int
-- maxFieldIndex r = maximumDef (-1) $ catMaybes [
-- dateField r
-- ,statusField r
-- ,codeField r
-- ,amountField r
-- ,amountInField r
-- ,amountOutField r
-- ,currencyField r
-- ,accountField r
-- ,account2Field r
-- ,date2Field r
-- ]
-- rulesFileFor :: CliOpts -> FilePath -> FilePath
-- rulesFileFor CliOpts{rules_file_=Just f} _ = f
-- rulesFileFor CliOpts{rules_file_=Nothing} csvfile = replaceExtension csvfile ".rules"
rulesFileFor :: FilePath -> FilePath
rulesFileFor = (++ ".rules")
csvFileFor :: FilePath -> FilePath
csvFileFor = reverse . drop 6 . reverse
defaultRulesText :: FilePath -> Text
defaultRulesText csvfile = T.pack $ unlines
["# hledger csv conversion rules for " ++ csvFileFor (takeFileName csvfile)
,"# cf http://hledger.org/manual#csv-files"
,""
,"account1 assets:bank:checking"
,""
,"fields date, description, amount1"
,""
,"#skip 1"
,"#newest-first"
,""
,"#date-format %-d/%-m/%Y"
,"#date-format %-m/%-d/%Y"
,"#date-format %Y-%h-%d"
,""
,"#currency $"
,""
,"if ITUNES"
," account2 expenses:entertainment"
,""
,"if (TO|FROM) SAVINGS"
," account2 assets:bank:savings\n"
]
--------------------------------------------------------------------------------
-- Conversion rules parsing
{-
Grammar for the CSV conversion rules, more or less:
RULES: RULE*
RULE: ( FIELD-LIST | FIELD-ASSIGNMENT | CONDITIONAL-BLOCK | SKIP | NEWEST-FIRST | DATE-FORMAT | COMMENT | BLANK ) NEWLINE
FIELD-LIST: fields SPACE FIELD-NAME ( SPACE? , SPACE? FIELD-NAME )*
FIELD-NAME: QUOTED-FIELD-NAME | BARE-FIELD-NAME
QUOTED-FIELD-NAME: " (any CHAR except double-quote)+ "
BARE-FIELD-NAME: any CHAR except space, tab, #, ;
FIELD-ASSIGNMENT: JOURNAL-FIELD ASSIGNMENT-SEPARATOR FIELD-VALUE
JOURNAL-FIELD: date | date2 | status | code | description | comment | account1 | account2 | amount | JOURNAL-PSEUDO-FIELD
JOURNAL-PSEUDO-FIELD: amount-in | amount-out | currency
ASSIGNMENT-SEPARATOR: SPACE | ( : SPACE? )
FIELD-VALUE: VALUE (possibly containing CSV-FIELD-REFERENCEs)
CSV-FIELD-REFERENCE: % CSV-FIELD
CSV-FIELD: ( FIELD-NAME | FIELD-NUMBER ) (corresponding to a CSV field)
FIELD-NUMBER: DIGIT+
CONDITIONAL-BLOCK: if ( FIELD-MATCHER NEWLINE )+ INDENTED-BLOCK
FIELD-MATCHER: ( CSV-FIELD-NAME SPACE? )? ( MATCHOP SPACE? )? PATTERNS
MATCHOP: ~
PATTERNS: ( NEWLINE REGEXP )* REGEXP
INDENTED-BLOCK: ( SPACE ( FIELD-ASSIGNMENT | COMMENT ) NEWLINE )+
REGEXP: ( NONSPACE CHAR* ) SPACE?
VALUE: SPACE? ( CHAR* ) SPACE?
COMMENT: SPACE? COMMENT-CHAR VALUE
COMMENT-CHAR: # | ;
NONSPACE: any CHAR not a SPACE-CHAR
BLANK: SPACE?
SPACE: SPACE-CHAR+
SPACE-CHAR: space | tab
CHAR: any character except newline
DIGIT: 0-9
-}
{- |
A set of data definitions and account-matching patterns sufficient to
convert a particular CSV data file into meaningful journal transactions.
-}
data CsvRules = CsvRules {
rdirectives :: [(DirectiveName,String)],
rcsvfieldindexes :: [(CsvFieldName, CsvFieldIndex)],
rassignments :: [(JournalFieldName, FieldTemplate)],
rconditionalblocks :: [ConditionalBlock]
} deriving (Show, Eq)
type CsvRulesParser a = StateT CsvRules SimpleTextParser a
type DirectiveName = String
type CsvFieldName = String
type CsvFieldIndex = Int
type JournalFieldName = String
type FieldTemplate = String
type ConditionalBlock = ([RecordMatcher], [(JournalFieldName, FieldTemplate)]) -- block matches if all RecordMatchers match
type RecordMatcher = [RegexpPattern] -- match if any regexps match any of the csv fields
-- type FieldMatcher = (CsvFieldName, [RegexpPattern]) -- match if any regexps match this csv field
type DateFormat = String
type RegexpPattern = String
defrules = CsvRules {
rdirectives=[],
rcsvfieldindexes=[],
rassignments=[],
rconditionalblocks=[]
}
addDirective :: (DirectiveName, String) -> CsvRules -> CsvRules
addDirective d r = r{rdirectives=d:rdirectives r}
addAssignment :: (JournalFieldName, FieldTemplate) -> CsvRules -> CsvRules
addAssignment a r = r{rassignments=a:rassignments r}
setIndexesAndAssignmentsFromList :: [CsvFieldName] -> CsvRules -> CsvRules
setIndexesAndAssignmentsFromList fs r = addAssignmentsFromList fs . setCsvFieldIndexesFromList fs $ r
setCsvFieldIndexesFromList :: [CsvFieldName] -> CsvRules -> CsvRules
setCsvFieldIndexesFromList fs r = r{rcsvfieldindexes=zip fs [1..]}
addAssignmentsFromList :: [CsvFieldName] -> CsvRules -> CsvRules
addAssignmentsFromList fs r = foldl' maybeAddAssignment r journalfieldnames
where
maybeAddAssignment rules f = (maybe id addAssignmentFromIndex $ elemIndex f fs) rules
where
addAssignmentFromIndex i = addAssignment (f, "%"++show (i+1))
addConditionalBlock :: ConditionalBlock -> CsvRules -> CsvRules
addConditionalBlock b r = r{rconditionalblocks=b:rconditionalblocks r}
getDirective :: DirectiveName -> CsvRules -> Maybe FieldTemplate
getDirective directivename = lookup directivename . rdirectives
instance ShowErrorComponent String where
showErrorComponent = id
-- Not used by hledger; just for lib users,
-- | An pure-exception-throwing IO action that parses this file's content
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
-- as CSV conversion rules, interpolating any included files first,
-- and runs some extra validation checks.
parseRulesFile :: FilePath -> ExceptT String IO CsvRules
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
parseRulesFile f =
liftIO (readFilePortably f >>= expandIncludes (takeDirectory f))
>>= either throwError return . parseAndValidateCsvRules f
-- | Inline all files referenced by include directives in this hledger CSV rules text, recursively.
-- Included file paths may be relative to the directory of the provided file path.
-- This is done as a pre-parse step to simplify the CSV rules parser.
expandIncludes :: FilePath -> Text -> IO Text
expandIncludes dir content = mapM (expandLine dir) (T.lines content) >>= return . T.unlines
where
expandLine dir line =
case line of
(T.stripPrefix "include " -> Just f) -> expandIncludes dir' =<< T.readFile f'
where
f' = dir </> dropWhile isSpace (T.unpack f)
dir' = takeDirectory f'
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
_ -> return line
-- | An error-throwing IO action that parses this text as CSV conversion rules
-- and runs some extra validation checks. The file path is used in error messages.
parseAndValidateCsvRules :: FilePath -> T.Text -> Either String CsvRules
parseAndValidateCsvRules rulesfile s =
case parseCsvRules rulesfile s of
Left err -> Left $ customErrorBundlePretty err
Right rules -> first makeFancyParseError $ validateRules rules
where
makeFancyParseError :: String -> String
2020-01-02 19:23:44 +03:00
makeFancyParseError errorString =
parseErrorPretty (FancyError 0 (S.singleton $ ErrorFail errorString) :: ParseError Text String)
-- | Parse this text as CSV conversion rules. The file path is for error messages.
2018-09-30 04:32:08 +03:00
parseCsvRules :: FilePath -> T.Text -> Either (ParseErrorBundle T.Text CustomErr) CsvRules
-- parseCsvRules rulesfile s = runParser csvrulesfile nullrules{baseAccount=takeBaseName rulesfile} rulesfile s
parseCsvRules rulesfile s =
runParser (evalStateT rulesp defrules) rulesfile s
-- | Return the validated rules, or an error.
validateRules :: CsvRules -> Either String CsvRules
validateRules rules = do
unless (isAssigned "date") $ Left "Please specify (at top level) the date field. Eg: date %1\n"
Right rules
where
isAssigned f = isJust $ getEffectiveAssignment rules [] f
-- parsers
rulesp :: CsvRulesParser CsvRules
rulesp = do
2020-01-02 19:25:19 +03:00
_ <- many $ choiceInState
[blankorcommentlinep <?> "blank or comment line"
,(directivep >>= modify' . addDirective) <?> "directive"
,(fieldnamelistp >>= modify' . setIndexesAndAssignmentsFromList) <?> "field name list"
,(fieldassignmentp >>= modify' . addAssignment) <?> "field assignment"
,(conditionalblockp >>= modify' . addConditionalBlock) <?> "conditional block"
]
eof
r <- get
return r{rdirectives=reverse $ rdirectives r
,rassignments=reverse $ rassignments r
,rconditionalblocks=reverse $ rconditionalblocks r
}
blankorcommentlinep :: CsvRulesParser ()
2018-07-16 17:28:58 +03:00
blankorcommentlinep = lift (dbgparse 3 "trying blankorcommentlinep") >> choiceInState [blanklinep, commentlinep]
blanklinep :: CsvRulesParser ()
blanklinep = lift (skipMany spacenonewline) >> newline >> return () <?> "blank line"
commentlinep :: CsvRulesParser ()
commentlinep = lift (skipMany spacenonewline) >> commentcharp >> lift restofline >> return () <?> "comment line"
commentcharp :: CsvRulesParser Char
commentcharp = oneOf (";#*" :: [Char])
directivep :: CsvRulesParser (DirectiveName, String)
directivep = (do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying directive"
d <- fmap T.unpack $ choiceInState $ map (lift . string . T.pack) directives
v <- (((char ':' >> lift (many spacenonewline)) <|> lift (some spacenonewline)) >> directivevalp)
<|> (optional (char ':') >> lift (skipMany spacenonewline) >> lift eolof >> return "")
return (d, v)
) <?> "directive"
2020-01-02 19:26:13 +03:00
directives :: [String]
directives =
["date-format"
2020-01-02 19:26:59 +03:00
,"separator"
-- ,"default-account1"
-- ,"default-currency"
-- ,"skip-lines" -- old
,"skip"
,"newest-first"
-- ,"base-account"
-- ,"base-currency"
2019-11-13 12:24:50 +03:00
, "balance-type"
]
directivevalp :: CsvRulesParser String
2018-09-30 04:32:08 +03:00
directivevalp = anySingle `manyTill` lift eolof
fieldnamelistp :: CsvRulesParser [CsvFieldName]
fieldnamelistp = (do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying fieldnamelist"
string "fields"
optional $ char ':'
lift (skipSome spacenonewline)
let separator = lift (skipMany spacenonewline) >> char ',' >> lift (skipMany spacenonewline)
f <- fromMaybe "" <$> optional fieldnamep
fs <- some $ (separator >> fromMaybe "" <$> optional fieldnamep)
lift restofline
return $ map (map toLower) $ f:fs
) <?> "field name list"
fieldnamep :: CsvRulesParser String
fieldnamep = quotedfieldnamep <|> barefieldnamep
quotedfieldnamep :: CsvRulesParser String
quotedfieldnamep = do
char '"'
f <- some $ noneOf ("\"\n:;#~" :: [Char])
char '"'
return f
barefieldnamep :: CsvRulesParser String
barefieldnamep = some $ noneOf (" \t\n,;#~" :: [Char])
fieldassignmentp :: CsvRulesParser (JournalFieldName, FieldTemplate)
fieldassignmentp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying fieldassignmentp"
f <- journalfieldnamep
v <- choiceInState [ assignmentseparatorp >> fieldvalp
, lift eolof >> return ""
]
return (f,v)
<?> "field assignment"
journalfieldnamep :: CsvRulesParser String
journalfieldnamep = do
2018-07-16 17:28:58 +03:00
lift (dbgparse 2 "trying journalfieldnamep")
T.unpack <$> choiceInState (map (lift . string . T.pack) journalfieldnames)
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
-- Transaction fields and pseudo fields for CSV conversion.
-- Names must precede any other name they contain, for the parser
-- (amount-in before amount; date2 before date). TODO: fix
journalfieldnames =
concat [[ "account" ++ i
,"amount" ++ i ++ "-in"
,"amount" ++ i ++ "-out"
,"amount" ++ i
,"balance" ++ i
,"comment" ++ i
,"currency" ++ i
] | x <- [1..9], let i = show x]
++
["amount-in"
,"amount-out"
,"amount"
,"balance"
,"code"
,"comment"
,"currency"
,"date2"
,"date"
,"description"
,"status"
,"skip" -- skip and end are not really fields, but we list it here to allow conditional rules that skip records
,"end"
]
assignmentseparatorp :: CsvRulesParser ()
assignmentseparatorp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying assignmentseparatorp"
_ <- choiceInState [ lift (skipMany spacenonewline) >> char ':' >> lift (skipMany spacenonewline)
, lift (skipSome spacenonewline)
]
return ()
fieldvalp :: CsvRulesParser String
fieldvalp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 2 "trying fieldvalp"
2018-09-30 04:32:08 +03:00
anySingle `manyTill` lift eolof
conditionalblockp :: CsvRulesParser ConditionalBlock
conditionalblockp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying conditionalblockp"
string "if" >> lift (skipMany spacenonewline) >> optional newline
ms <- some recordmatcherp
as <- many (try $ lift (skipSome spacenonewline) >> fieldassignmentp)
when (null as) $
Fail.fail "start of conditional block found, but no assignment rules afterward\n(assignment rules in a conditional block should be indented)\n"
return (ms, as)
<?> "conditional block"
recordmatcherp :: CsvRulesParser [String]
recordmatcherp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 2 "trying recordmatcherp"
-- pos <- currentPos
_ <- optional (matchoperatorp >> lift (skipMany spacenonewline) >> optional newline)
ps <- patternsp
when (null ps) $
Fail.fail "start of record matcher found, but no patterns afterward\n(patterns should not be indented)\n"
return ps
<?> "record matcher"
matchoperatorp :: CsvRulesParser String
matchoperatorp = fmap T.unpack $ choiceInState $ map string
["~"
-- ,"!~"
-- ,"="
-- ,"!="
]
patternsp :: CsvRulesParser [String]
patternsp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying patternsp"
ps <- many regexp
return ps
regexp :: CsvRulesParser String
regexp = do
2018-07-16 17:28:58 +03:00
lift $ dbgparse 3 "trying regexp"
notFollowedBy matchoperatorp
c <- lift nonspace
2018-09-30 04:32:08 +03:00
cs <- anySingle `manyTill` lift eolof
return $ strip $ c:cs
-- fieldmatcher = do
2018-07-16 17:28:58 +03:00
-- dbgparse 2 "trying fieldmatcher"
-- f <- fromMaybe "all" `fmap` (optional $ do
-- f' <- fieldname
-- lift (skipMany spacenonewline)
-- return f')
-- char '~'
-- lift (skipMany spacenonewline)
-- ps <- patterns
-- let r = "(" ++ intercalate "|" ps ++ ")"
-- return (f,r)
-- <?> "field matcher"
--------------------------------------------------------------------------------
-- Converting CSV records to journal transactions
type CsvRecord = [String]
showRules rules record =
unlines $ catMaybes [ (("the "++fld++" rule is: ")++) <$> getEffectiveAssignment rules record fld | fld <- journalfieldnames]
transactionFromCsvRecord :: SourcePos -> CsvRules -> CsvRecord -> Transaction
transactionFromCsvRecord sourcepos rules record = t
where
mdirective = (`getDirective` rules)
mfieldtemplate = getEffectiveAssignment rules record
render = renderTemplate rules record
mskip = mdirective "skip"
mdefaultcurrency = mdirective "default-currency"
mparsedate = parseDateWithFormatOrDefaultFormats (mdirective "date-format")
-- render each field using its template and the csv record, and
-- in some cases parse the rendered string (eg dates and amounts)
mdateformat = mdirective "date-format"
date = render $ fromMaybe "" $ mfieldtemplate "date"
date' = fromMaybe (error' $ dateerror "date" date mdateformat) $ mparsedate date
mdate2 = render <$> mfieldtemplate "date2"
mdate2' = maybe Nothing (maybe (error' $ dateerror "date2" (fromMaybe "" mdate2) mdateformat) Just . mparsedate) mdate2
dateerror datefield value mdateformat = unlines
["error: could not parse \""++value++"\" as a date using date format "++maybe "\"YYYY/M/D\", \"YYYY-M-D\" or \"YYYY.M.D\"" show mdateformat
, showRecord record
,"the "++datefield++" rule is: "++(fromMaybe "required, but missing" $ mfieldtemplate datefield)
,"the date-format is: "++fromMaybe "unspecified" mdateformat
,"you may need to "
++"change your "++datefield++" rule, "
++maybe "add a" (const "change your") mdateformat++" date-format rule, "
++"or "++maybe "add a" (const "change your") mskip++" skip rule"
,"for m/d/y or d/m/y dates, use date-format %-m/%-d/%Y or date-format %-d/%-m/%Y"
]
status =
case mfieldtemplate "status" of
Nothing -> Unmarked
Just str -> either statuserror id .
runParser (statusp <* eof) "" .
T.pack $ render str
where
statuserror err = error' $ unlines
["error: could not parse \""++str++"\" as a cleared status (should be *, ! or empty)"
,"the parse error is: "++customErrorBundlePretty err
]
code = singleline $ maybe "" render $ mfieldtemplate "code"
description = singleline $ maybe "" render $ mfieldtemplate "description"
comment = singleline $ maybe "" render $ mfieldtemplate "comment"
precomment = singleline $ maybe "" render $ mfieldtemplate "precomment"
s `or` def = if null s then def else s
parsebalance currency n str
| all isSpace str = Nothing
| otherwise = Just $ (either (balanceerror n str) id $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack $ (currency++) $ simplifySign str, nullsourcepos)
balanceerror n str err = error' $ unlines
["error: could not parse \""++str++"\" as balance"++n++" amount"
,showRecord record
,showRules rules record
,"the default-currency is: "++fromMaybe "unspecified" mdefaultcurrency
,"the parse error is: "++customErrorBundlePretty err
]
parsePosting' number accountFld amountFld amountInFld amountOutFld balanceFld commentFld =
let currency = maybe (fromMaybe "" mdefaultcurrency) render $
(mfieldtemplate ("currency"++number) `or `mfieldtemplate "currency")
amount = chooseAmount rules record currency amountFld amountInFld amountOutFld
account' = ((T.pack . render) <$> (mfieldtemplate accountFld
`or` mdirective ("default-account" ++ number)))
balance = (parsebalance currency number.render) =<< mfieldtemplate balanceFld
comment = T.pack $ maybe "" render $ mfieldtemplate commentFld
account =
case account' of
-- If account is explicitly "unassigned", suppress posting
-- Otherwise, generate posting with "expenses:unknown" account if we have amount/balance information
Just "" -> Nothing
Just account -> Just account
Nothing ->
-- If we have amount or balance assertion (which implies potential amount change),
-- but no account name, lets generate "expenses:unknown" account name.
case (amount, balance) of
(Just _, _ ) -> Just "expenses:unknown"
(_, Just _) -> Just "expenses:unknown"
(Nothing, Nothing) -> Nothing
in
case account of
Nothing -> Nothing
Just account ->
Just $ (number, posting {paccount=accountNameWithoutPostingType account
, pamount=fromMaybe missingmixedamt amount
, ptransaction=Just t
, pbalanceassertion=toAssertion <$> balance
, pcomment = comment
, ptype = accountNamePostingType account})
parsePosting number =
parsePosting' number
("account"++number)
("amount"++number)
("amount"++number++"-in")
("amount"++number++"-out")
("balance"++number)
("comment" ++ number)
withAlias fld alias =
case (mfieldtemplate fld, mfieldtemplate alias) of
(Just fld, Just alias) -> error' $ unlines
[ "error: both \"" ++ fld ++ "\" and \"" ++ alias ++ "\" have values."
, showRecord record
, showRules rules record
]
(Nothing, Just _) -> alias
(_, Nothing) -> fld
posting1 = parsePosting' "1"
("account1" `withAlias` "account")
("amount1" `withAlias` "amount")
("amount1-in" `withAlias` "amount-in")
("amount1-out" `withAlias` "amount-out")
("balance1" `withAlias` "balance")
"comment1" -- comment1 does not have legacy alias
2019-10-16 01:11:53 +03:00
postings' = catMaybes $ posting1:[ parsePosting i | x<-[2..9], let i = show x]
improveUnknownAccountName p =
if paccount p /="expenses:unknown"
then p
else case isNegativeMixedAmount (pamount p) of
Just True -> p{paccount = "income:unknown"}
Just False -> p{paccount = "expenses:unknown"}
_ -> p
postings =
2019-10-16 01:11:53 +03:00
case postings' of
-- To be compatible with the behavior of the old code which allowed two postings only, we enforce
-- second posting when rules generated just first of them, and posting is of type that should be balanced.
2019-12-21 23:01:03 +03:00
-- When we have strictly first and second posting, but second posting does not have amount, we fill it in.
[("1",posting1)] ->
case ptype posting1 of
VirtualPosting -> [posting1]
_ ->
[posting1,improveUnknownAccountName (posting{paccount="expenses:unknown", pamount=costOfMixedAmount(-(pamount posting1)), ptransaction=Just t})]
[("1",posting1),("2",posting2)] ->
case (pamount posting1 == missingmixedamt , pamount posting2 == missingmixedamt) of
(False, True) -> [posting1, improveUnknownAccountName (posting2{pamount=costOfMixedAmount(-(pamount posting1))})]
_ -> [posting1, posting2]
_ -> map snd postings'
-- build the transaction
t = nulltransaction{
tsourcepos = genericSourcePos sourcepos,
tdate = date',
tdate2 = mdate2',
tstatus = status,
lib: textification: descriptions & codes Slightly higher (with small files) and lower (with large files) maximum residency, and slightly quicker for all. hledger -f data/100x100x10.journal stats <<ghc: 42858472 bytes, 84 GCs, 193712/269608 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.015 elapsed), 0.016 MUT (0.042 elapsed), 0.011 GC (0.119 elapsed) :ghc>> <<ghc: 42891776 bytes, 84 GCs, 190816/260920 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.004 elapsed), 0.017 MUT (0.025 elapsed), 0.010 GC (0.015 elapsed) :ghc>> hledger -f data/1000x1000x10.journal stats <<ghc: 349575240 bytes, 681 GCs, 1396425/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.146 elapsed), 0.050 GC (0.057 elapsed) :ghc>> <<ghc: 349927568 bytes, 681 GCs, 1397825/4097248 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.133 elapsed), 0.050 GC (0.057 elapsed) :ghc>> hledger -f data/10000x1000x10.journal stats <<ghc: 3424029496 bytes, 6658 GCs, 11403141/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.278 MUT (1.310 elapsed), 0.493 GC (0.546 elapsed) :ghc>> <<ghc: 3427418064 bytes, 6665 GCs, 11127869/37790168 avg/max bytes residency (11 samples), 109M in use, 0.000 INIT (0.001 elapsed), 1.212 MUT (1.229 elapsed), 0.466 GC (0.519 elapsed) :ghc>> hledger -f data/100000x1000x10.journal stats <<ghc: 34306546248 bytes, 66727 GCs, 77030638/414617944 avg/max bytes residency (14 samples), 1012M in use, 0.000 INIT (0.000 elapsed), 12.965 MUT (13.164 elapsed), 4.771 GC (5.447 elapsed) :ghc>> <<ghc: 34340246056 bytes, 66779 GCs, 76983178/416011480 avg/max bytes residency (14 samples), 1011M in use, 0.000 INIT (0.008 elapsed), 12.666 MUT (12.836 elapsed), 4.595 GC (5.175 elapsed) :ghc>>
2016-05-25 04:51:52 +03:00
tcode = T.pack code,
tdescription = T.pack description,
lib: textification: comments and tags No change. hledger -f data/100x100x10.journal stats <<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.020 elapsed), 0.009 GC (0.011 elapsed) :ghc>> <<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.018 elapsed), 0.009 GC (0.013 elapsed) :ghc>> hledger -f data/1000x1000x10.journal stats <<ghc: 349576344 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.124 MUT (0.130 elapsed), 0.047 GC (0.055 elapsed) :ghc>> <<ghc: 349576280 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.132 elapsed), 0.049 GC (0.058 elapsed) :ghc>> hledger -f data/10000x1000x10.journal stats <<ghc: 3424030664 bytes, 6658 GCs, 11403359/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.207 MUT (1.228 elapsed), 0.473 GC (0.528 elapsed) :ghc>> <<ghc: 3424030760 bytes, 6658 GCs, 11403874/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.002 elapsed), 1.234 MUT (1.256 elapsed), 0.470 GC (0.520 elapsed) :ghc>> hledger -f data/100000x1000x10.journal stats <<ghc: 34306547448 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.003 elapsed), 12.615 MUT (12.813 elapsed), 4.656 GC (5.291 elapsed) :ghc>> <<ghc: 34306547320 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.009 elapsed), 12.802 MUT (13.065 elapsed), 4.774 GC (5.441 elapsed) :ghc>>
2016-05-25 03:09:20 +03:00
tcomment = T.pack comment,
tprecedingcomment = T.pack precomment,
tpostings = postings
}
defaultAssertion =
2019-11-13 12:24:50 +03:00
case mdirective "balance-type" of
Nothing -> nullassertion
Just "=" -> nullassertion
Just "==" -> nullassertion{batotal=True}
Just "=*" -> nullassertion{bainclusive=True}
Just "==*" -> nullassertion{batotal=True, bainclusive=True}
Just x -> error' $ unlines
2019-11-13 12:24:50 +03:00
[ "balance-type \"" ++ x ++"\" is invalid. Use =, ==, =* or ==*."
, showRecord record
, showRules rules record
]
toAssertion (a, b) = defaultAssertion{
baamount = a,
baposition = b
}
chooseAmount :: CsvRules -> CsvRecord -> String -> String -> String -> String -> Maybe MixedAmount
chooseAmount rules record currency amountFld amountInFld amountOutFld =
let
mamount = getEffectiveAssignment rules record amountFld
mamountin = getEffectiveAssignment rules record amountInFld
mamountout = getEffectiveAssignment rules record amountOutFld
parse amt = notZero =<< (parseAmount currency <$> notEmpty =<< (strip . renderTemplate rules record) <$> amt)
in
case (parse mamount, parse mamountin, parse mamountout) of
(Nothing, Nothing, Nothing) -> Nothing
(Just a, Nothing, Nothing) -> Just a
(Nothing, Just i, Nothing) -> Just i
(Nothing, Nothing, Just o) -> Just $ negate o
(Nothing, Just i, Just o) -> error' $ "both "++amountInFld++" and "++amountOutFld++" have a value\n"
++ " "++amountInFld++": " ++ show i ++ "\n"
++ " "++amountOutFld++": " ++ show o ++ "\n"
++ " record: " ++ showRecord record
_ -> error' $ "found values for "++amountFld++" and for "++amountInFld++"/"++amountOutFld++"\n"
++ "please use either "++amountFld++" or "++amountInFld++"/"++amountOutFld++"\n"
++ " record: " ++ showRecord record
where
notZero amt = if isZeroMixedAmount amt then Nothing else Just amt
notEmpty str = if str=="" then Nothing else Just str
parseAmount currency amountstr =
either (amounterror amountstr) (Mixed . (:[]))
<$> runParser (evalStateT (amountp <* eof) mempty) ""
<$> T.pack
<$> (currency++)
<$> simplifySign
<$> amountstr
amounterror amountstr err = error' $ unlines
["error: could not parse \""++fromJust amountstr++"\" as an amount"
,showRecord record
,showRules rules record
,"the default-currency is: "++fromMaybe "unspecified" (getDirective "default-currency" rules)
,"the parse error is: "++customErrorBundlePretty err
,"you may need to "
++"change your amount or currency rules, "
++"or add or change your skip rule"
]
2017-05-14 12:17:56 +03:00
type CsvAmountString = String
2017-05-14 12:17:56 +03:00
-- | Canonicalise the sign in a CSV amount string.
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
-- Such strings can have a minus sign, negating parentheses,
-- or any two of these (which cancels out).
--
-- >>> simplifySign "1"
-- "1"
-- >>> simplifySign "-1"
-- "-1"
-- >>> simplifySign "(1)"
-- "-1"
-- >>> simplifySign "--1"
-- "1"
-- >>> simplifySign "-(1)"
-- "1"
-- >>> simplifySign "(-1)"
-- "1"
-- >>> simplifySign "((1))"
-- "1"
2017-05-14 12:17:56 +03:00
simplifySign :: CsvAmountString -> CsvAmountString
simplifySign ('(':s) | lastMay s == Just ')' = simplifySign $ negateStr $ init s
simplifySign ('-':'(':s) | lastMay s == Just ')' = simplifySign $ init s
2017-05-14 12:17:56 +03:00
simplifySign ('-':'-':s) = s
simplifySign s = s
negateStr :: String -> String
negateStr ('-':s) = s
negateStr s = '-':s
-- | Show a (approximate) recreation of the original CSV record.
showRecord :: CsvRecord -> String
showRecord r = "the CSV record is: "++intercalate "," (map show r)
-- | Given the conversion rules, a CSV record and a journal entry field name, find
-- the template value ultimately assigned to this field, either at top
-- level or in a matching conditional block. Conditional blocks'
-- patterns are matched against an approximation of the original CSV
-- record: all the field values with commas intercalated.
getEffectiveAssignment :: CsvRules -> CsvRecord -> JournalFieldName -> Maybe FieldTemplate
getEffectiveAssignment rules record f = lastMay $ assignmentsFor f
where
assignmentsFor f = map snd $ filter ((==f).fst) $ toplevelassignments ++ conditionalassignments
where
toplevelassignments = rassignments rules
conditionalassignments = concatMap snd $ filter blockMatches $ blocksAssigning f
where
blocksAssigning f = filter (any ((==f).fst) . snd) $ rconditionalblocks rules
blockMatches :: ConditionalBlock -> Bool
blockMatches (matchers,_) = all matcherMatches matchers
where
matcherMatches :: RecordMatcher -> Bool
-- matcherMatches pats = any patternMatches pats
matcherMatches pats = patternMatches $ "(" ++ intercalate "|" pats ++ ")"
where
patternMatches :: RegexpPattern -> Bool
patternMatches pat = regexMatchesCI pat csvline
where
csvline = intercalate "," record
-- | Render a field assigment's template, possibly interpolating referenced
-- CSV field values. Outer whitespace is removed from interpolated values.
renderTemplate :: CsvRules -> CsvRecord -> FieldTemplate -> String
renderTemplate rules record t = regexReplaceBy "%[A-z0-9_-]+" replace t
where
replace ('%':pat) = maybe pat (\i -> strip $ atDef "" record (i-1)) mindex
where
mindex | all isDigit pat = readMay pat
| otherwise = lookup (map toLower pat) $ rcsvfieldindexes rules
replace pat = pat
-- Parse the date string using the specified date-format, or if unspecified try these default formats:
-- YYYY/MM/DD, YYYY-MM-DD, YYYY.MM.DD, MM/DD/YYYY (month and day can be 1 or 2 digits, year must be 4).
parseDateWithFormatOrDefaultFormats :: Maybe DateFormat -> String -> Maybe Day
parseDateWithFormatOrDefaultFormats mformat s = firstJust $ map parsewith formats
where
parsetime =
#if MIN_VERSION_time(1,5,0)
parseTimeM True
#else
parseTime
#endif
parsewith = flip (parsetime defaultTimeLocale) s
formats = maybe
["%Y/%-m/%-d"
,"%Y-%-m-%-d"
,"%Y.%-m.%-d"
-- ,"%-m/%-d/%Y"
-- ,parseTime defaultTimeLocale "%Y/%m/%e" (take 5 s ++ "0" ++ drop 5 s)
-- ,parseTime defaultTimeLocale "%Y-%m-%e" (take 5 s ++ "0" ++ drop 5 s)
-- ,parseTime defaultTimeLocale "%m/%e/%Y" ('0':s)
-- ,parseTime defaultTimeLocale "%m-%e-%Y" ('0':s)
]
(:[])
mformat
--------------------------------------------------------------------------------
-- tests
2018-09-06 23:08:26 +03:00
tests_CsvReader = tests "CsvReader" [
2018-09-04 23:39:21 +03:00
tests "parseCsvRules" [
test"empty file" $
parseCsvRules "unknown" "" @?= Right defrules
2018-09-04 23:39:21 +03:00
]
,tests "rulesp" [
test"trailing comments" $
parseWithState' defrules rulesp "skip\n# \n#\n" @?= Right defrules{rdirectives = [("skip","")]}
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
,test"trailing blank lines" $
parseWithState' defrules rulesp "skip\n\n \n" @?= (Right defrules{rdirectives = [("skip","")]})
csv: merge lucamolteni's cassava/custom separators (squashed) (#829) commit 5ba464de761b298e50d57a8b7d14bc28adb30d5d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:54:12 2018 +0200 Fix CI 2 commit f060ae9449f4b61a915b0ed4629fc1ba9b66fb4a Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:30:08 2018 +0200 Fix CI build commit af0719a33b9b72ad244ae80198d881a1f7145e9d Author: Luca Molteni <volothamp@gmail.com> Date: Fri Sep 7 17:19:01 2018 +0200 Fix rebase commit 1a24ddfa54dfb4ff1326e1a51005ffa82d3dc3c8 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:25:24 2018 +0200 Fixed some GHC warnings commit 1ac43398a359b5925ef71f53347698f1c6c510ef Author: Luca Molteni <volothamp@gmail.com> Date: Fri Aug 10 16:14:49 2018 +0200 Fix .cabal commit 422456b925d8aa4ab3e869f51e98c2b1c3dcde0a Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:56:20 2018 +0200 Removed to-do list commit 1118b762e4fd15c4fe7ba48ba86676706ea3a5a5 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 22:53:28 2018 +0200 Better test commit 1146ed0941655668bf7684f18aa15c5f4b9b20c2 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jul 1 15:32:28 2018 +0200 Fix parsing commit 4fc2374b2b81802990da30c96756aab54d77399c Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 22:11:11 2018 +0200 Parsing of separator commit f7a61737f1ad4460ba20ca9b2e86eb21468abb33 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:29:23 2018 +0200 Almost separator in options commit ac8841cf3b9c80914bc3271ad9b9ff4ae9ba48a7 Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 14:16:59 2018 +0200 Separator in parseCSV commit 92a8b9f6ba77ea4237f769641e03029ac88542ea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 13:30:41 2018 +0200 separator option commit ec417a81ae625647cf35e61776cdf02bdb2c6aea Author: Luca Molteni <volothamp@gmail.com> Date: Thu Jun 21 10:45:26 2018 +0200 Removed one qualified import commit 8b2f386c2f780adcd34cff3de7edceacc1d325a7 Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 14:01:12 2018 +0200 Removed string conversions commit a14d0e099e28a286bb81770cfc9cb8f5c7e5cf1f Author: Luca Molteni <volothamp@gmail.com> Date: Wed Jun 20 10:23:20 2018 +0200 custom delimiter in cassava commit 694d48e2bc1ada0037b90367c017f3082f68ed45 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:51:54 2018 +0200 Use Text.getContents - remove UTF-8 compatibility library commit a7ada2cc60033ebdd796ca34cc2ec69a4f387843 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:49:34 2018 +0200 todo list commit 58ec47d3987909f6bace50e3e647e30dadd5bf03 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 17:45:22 2018 +0200 CSV test now has unicode characters commit b7851e94c3f1683b63ec7250a12bcde3b7bed691 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 16:59:39 2018 +0200 Use decode from Text commit 79f59fd28ccaca08fcd718fcd8d00b1c1d65d7e1 Author: Luca Molteni <volothamp@gmail.com> Date: Sun Jun 10 13:28:57 2018 +0200 Use Text and Lazy Bytestring commit 470c9bcb8dc00669beb4ef0303a1e7d9f7aecc89 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:30:22 2018 +0200 Use megaparsec error commit f978848ba249ef4f67b855bea5d4e549290c205c Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:22:07 2018 +0200 Renamed qualify and remove Parsec commit 152587fde204c43a55798d212e43f37cd3038c2e Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 15:12:36 2018 +0200 Use cassava mega parsec commit cf281577a3d3a071196484a6fc8485f2ea1f7d67 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 14:01:47 2018 +0200 Removed Data.Vector commit 1272e8e758369d8cc5778029a705b277355a5029 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:16:18 2018 +0200 Removed Parsec ParseError commit ae07f043135a19307fd65b281ade37a74c76acb2 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 12:06:14 2018 +0200 Type sinonim for ParsecError commit 8e15b253c11bd1c0c35a7641aeb18aa54e0ba9b0 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:16:08 2018 +0200 Replaced with typeclasses commit 1ed46f9c175603611325f3d377004e4b85f29377 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 11:01:33 2018 +0200 Replaced Text/CSV with Cassava commit 362f4111b5854145703174b976fc7acbd71b8783 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 10:34:37 2018 +0200 Use cassava parsin instead of Text/CSV commit 83e678e371618687cf7c15a4e2cfa67f570b6b64 Author: Luca Molteni <volothamp@gmail.com> Date: Sat Jun 9 08:22:51 2018 +0200 Text CSV error messages commit f922df71d274beeacab9fb2530b16c97f005cc08 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:45:20 2018 +0200 Better types commit edd130781c84790a53bff2283e6041eb8232e7cf Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 21:34:59 2018 +0200 Conversion to Text CSV type commit 0799383214483018ad2d977a3c8022414959c2b2 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 16:06:21 2018 +0200 First function with cassava commit e92aeb151ff527b383ff3d0ced7764e81b71af82 Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:47:34 2018 +0200 Added cassava as dependency commit 5ea005c558a3939af7e5f0cd735a9b4da931228e Author: Luca Molteni <volothamp@gmail.com> Date: Fri Jun 8 13:18:47 2018 +0200 Better .gitignore for multi idea modules
2018-09-07 20:12:13 +03:00
,test"no final newline" $
parseWithState' defrules rulesp "skip" @?= (Right defrules{rdirectives=[("skip","")]})
,test"assignment with empty value" $
parseWithState' defrules rulesp "account1 \nif foo\n account2 foo\n" @?=
(Right defrules{rassignments = [("account1","")], rconditionalblocks = [([["foo"]],[("account2","foo")])]})
]
,tests "conditionalblockp" [
test"space after conditional" $ -- #1120
parseWithState' defrules conditionalblockp "if a\n account2 b\n \n" @?=
(Right ([["a"]],[("account2","b")]))
]
]