Remove --separator command line argument
This commit is contained in:
parent
93c27891f2
commit
8df4e1ed83
@ -50,7 +50,6 @@ module Hledger.Read.Common (
|
|||||||
getAccountAliases,
|
getAccountAliases,
|
||||||
clearAccountAliases,
|
clearAccountAliases,
|
||||||
journalAddFile,
|
journalAddFile,
|
||||||
getSpecialSeparators,
|
|
||||||
|
|
||||||
-- * parsers
|
-- * parsers
|
||||||
-- ** transaction bits
|
-- ** transaction bits
|
||||||
@ -163,7 +162,6 @@ data InputOpts = InputOpts {
|
|||||||
mformat_ :: Maybe StorageFormat -- ^ a file/storage format to try, unless overridden
|
mformat_ :: Maybe StorageFormat -- ^ a file/storage format to try, unless overridden
|
||||||
-- by a filename prefix. Nothing means try all.
|
-- by a filename prefix. Nothing means try all.
|
||||||
,mrules_file_ :: Maybe FilePath -- ^ a conversion rules file to use (when reading CSV)
|
,mrules_file_ :: Maybe FilePath -- ^ a conversion rules file to use (when reading CSV)
|
||||||
,separator_ :: Maybe Char -- ^ the separator to use (when reading CSV)
|
|
||||||
,aliases_ :: [String] -- ^ account name aliases to apply
|
,aliases_ :: [String] -- ^ account name aliases to apply
|
||||||
,anon_ :: Bool -- ^ do light anonymisation/obfuscation of the data
|
,anon_ :: Bool -- ^ do light anonymisation/obfuscation of the data
|
||||||
,ignore_assertions_ :: Bool -- ^ don't check balance assertions
|
,ignore_assertions_ :: Bool -- ^ don't check balance assertions
|
||||||
@ -176,14 +174,13 @@ data InputOpts = InputOpts {
|
|||||||
instance Default InputOpts where def = definputopts
|
instance Default InputOpts where def = definputopts
|
||||||
|
|
||||||
definputopts :: InputOpts
|
definputopts :: InputOpts
|
||||||
definputopts = InputOpts def def def def def def def True def def
|
definputopts = InputOpts def def def def def def True def def
|
||||||
|
|
||||||
rawOptsToInputOpts :: RawOpts -> InputOpts
|
rawOptsToInputOpts :: RawOpts -> InputOpts
|
||||||
rawOptsToInputOpts rawopts = InputOpts{
|
rawOptsToInputOpts rawopts = InputOpts{
|
||||||
-- files_ = listofstringopt "file" rawopts
|
-- files_ = listofstringopt "file" rawopts
|
||||||
mformat_ = Nothing
|
mformat_ = Nothing
|
||||||
,mrules_file_ = maybestringopt "rules-file" rawopts
|
,mrules_file_ = maybestringopt "rules-file" rawopts
|
||||||
,separator_ = maybestringopt "separator" rawopts >>= getSpecialSeparators
|
|
||||||
,aliases_ = listofstringopt "alias" rawopts
|
,aliases_ = listofstringopt "alias" rawopts
|
||||||
,anon_ = boolopt "anon" rawopts
|
,anon_ = boolopt "anon" rawopts
|
||||||
,ignore_assertions_ = boolopt "ignore-assertions" rawopts
|
,ignore_assertions_ = boolopt "ignore-assertions" rawopts
|
||||||
@ -195,14 +192,6 @@ rawOptsToInputOpts rawopts = InputOpts{
|
|||||||
|
|
||||||
--- * parsing utilities
|
--- * parsing utilities
|
||||||
|
|
||||||
-- | Parse special separator names TAB and SPACE, or return the first
|
|
||||||
-- character. Return Nothing on empty string
|
|
||||||
getSpecialSeparators :: String -> Maybe Char
|
|
||||||
getSpecialSeparators "SPACE" = Just ' '
|
|
||||||
getSpecialSeparators "TAB" = Just '\t'
|
|
||||||
getSpecialSeparators (x:_) = Just x
|
|
||||||
getSpecialSeparators [] = Nothing
|
|
||||||
|
|
||||||
-- | Run a text parser in the identity monad. See also: parseWithState.
|
-- | Run a text parser in the identity monad. See also: parseWithState.
|
||||||
runTextParser, rtp
|
runTextParser, rtp
|
||||||
:: TextParser Identity a -> Text -> Either (ParseErrorBundle Text CustomErr) a
|
:: TextParser Identity a -> Text -> Either (ParseErrorBundle Text CustomErr) a
|
||||||
|
|||||||
@ -71,7 +71,7 @@ import Text.Printf (printf)
|
|||||||
|
|
||||||
import Hledger.Data
|
import Hledger.Data
|
||||||
import Hledger.Utils
|
import Hledger.Utils
|
||||||
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos, getSpecialSeparators, finaliseJournal)
|
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos, finaliseJournal)
|
||||||
|
|
||||||
type CSV = [Record]
|
type CSV = [Record]
|
||||||
|
|
||||||
@ -93,8 +93,7 @@ reader = Reader
|
|||||||
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
|
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
|
||||||
parse iopts f t = do
|
parse iopts f t = do
|
||||||
let rulesfile = mrules_file_ iopts
|
let rulesfile = mrules_file_ iopts
|
||||||
let separator = separator_ iopts
|
r <- liftIO $ readJournalFromCsv rulesfile f t
|
||||||
r <- liftIO $ readJournalFromCsv separator rulesfile f t
|
|
||||||
case r of Left e -> throwError e
|
case r of Left e -> throwError e
|
||||||
Right pj -> finaliseJournal iopts{ignore_assertions_=True} f t pj'
|
Right pj -> finaliseJournal iopts{ignore_assertions_=True} f t pj'
|
||||||
where
|
where
|
||||||
@ -104,12 +103,19 @@ parse iopts f t = do
|
|||||||
-- better preemptively reverse them once more. XXX inefficient
|
-- better preemptively reverse them once more. XXX inefficient
|
||||||
pj' = journalReverse pj
|
pj' = journalReverse pj
|
||||||
|
|
||||||
|
-- | Parse special separator names TAB and SPACE, or return the first
|
||||||
|
-- character. Return Nothing on empty string
|
||||||
|
getSpecialSeparators :: String -> Maybe Char
|
||||||
|
getSpecialSeparators "SPACE" = Just ' '
|
||||||
|
getSpecialSeparators "TAB" = Just '\t'
|
||||||
|
getSpecialSeparators (x:_) = Just x
|
||||||
|
getSpecialSeparators [] = Nothing
|
||||||
|
|
||||||
-- | Decide which separator to get.
|
-- | Decide which separator to get.
|
||||||
-- If the external separator is provided, take it. Otherwise, look at the rules. Finally, return ','.
|
-- If the external separator is provided, take it. Otherwise, look at the rules. Finally, return ','.
|
||||||
getSeparator :: Maybe Char -> CsvRules -> Char
|
getSeparator :: CsvRules -> Char
|
||||||
getSeparator externalSeparator rules = head $
|
getSeparator rules = head $
|
||||||
catMaybes [ externalSeparator
|
catMaybes [ getDirective "separator" rules >>= getSpecialSeparators
|
||||||
, getDirective "separator" rules >>= getSpecialSeparators
|
|
||||||
, Just ',']
|
, Just ',']
|
||||||
|
|
||||||
-- | Read a Journal from the given CSV data (and filename, used for error
|
-- | Read a Journal from the given CSV data (and filename, used for error
|
||||||
@ -123,9 +129,9 @@ getSeparator externalSeparator rules = head $
|
|||||||
-- 4. if the rules file didn't exist, create it with the default rules and filename
|
-- 4. if the rules file didn't exist, create it with the default rules and filename
|
||||||
-- 5. return the transactions as a Journal
|
-- 5. return the transactions as a Journal
|
||||||
-- @
|
-- @
|
||||||
readJournalFromCsv :: Maybe Char -> Maybe FilePath -> FilePath -> Text -> IO (Either String Journal)
|
readJournalFromCsv :: Maybe FilePath -> FilePath -> Text -> IO (Either String Journal)
|
||||||
readJournalFromCsv _ Nothing "-" _ = return $ Left "please use --rules-file when reading CSV from stdin"
|
readJournalFromCsv Nothing "-" _ = return $ Left "please use --rules-file when reading CSV from stdin"
|
||||||
readJournalFromCsv commandLineSeparator mrulesfile csvfile csvdata =
|
readJournalFromCsv mrulesfile csvfile csvdata =
|
||||||
handle (\(e::IOException) -> return $ Left $ show e) $ do
|
handle (\(e::IOException) -> return $ Left $ show e) $ do
|
||||||
|
|
||||||
-- make and throw an IO exception.. which we catch and convert to an Either above ?
|
-- make and throw an IO exception.. which we catch and convert to an Either above ?
|
||||||
@ -156,7 +162,7 @@ readJournalFromCsv commandLineSeparator mrulesfile csvfile csvdata =
|
|||||||
records <- (either throwerr id .
|
records <- (either throwerr id .
|
||||||
dbg2 "validateCsv" . validateCsv rules skiplines .
|
dbg2 "validateCsv" . validateCsv rules skiplines .
|
||||||
dbg2 "parseCsv")
|
dbg2 "parseCsv")
|
||||||
`fmap` parseCsv (getSeparator commandLineSeparator rules) parsecfilename csvdata
|
`fmap` parseCsv (getSeparator rules) parsecfilename csvdata
|
||||||
dbg1IO "first 3 csv records" $ take 3 records
|
dbg1IO "first 3 csv records" $ take 3 records
|
||||||
|
|
||||||
-- identify header lines
|
-- identify header lines
|
||||||
|
|||||||
@ -122,7 +122,6 @@ inputflags :: [Flag RawOpts]
|
|||||||
inputflags = [
|
inputflags = [
|
||||||
flagReq ["file","f"] (\s opts -> Right $ setopt "file" s opts) "FILE" "use a different input file. For stdin, use - (default: $LEDGER_FILE or $HOME/.hledger.journal)"
|
flagReq ["file","f"] (\s opts -> Right $ setopt "file" s opts) "FILE" "use a different input file. For stdin, use - (default: $LEDGER_FILE or $HOME/.hledger.journal)"
|
||||||
,flagReq ["rules-file"] (\s opts -> Right $ setopt "rules-file" s opts) "RFILE" "CSV conversion rules file (default: FILE.rules)"
|
,flagReq ["rules-file"] (\s opts -> Right $ setopt "rules-file" s opts) "RFILE" "CSV conversion rules file (default: FILE.rules)"
|
||||||
,flagReq ["separator"] (\s opts -> Right $ setopt "separator" s opts) "SEP" "CSV separator (default: ,)"
|
|
||||||
,flagReq ["alias"] (\s opts -> Right $ setopt "alias" s opts) "OLD=NEW" "rename accounts named OLD to NEW"
|
,flagReq ["alias"] (\s opts -> Right $ setopt "alias" s opts) "OLD=NEW" "rename accounts named OLD to NEW"
|
||||||
,flagNone ["anon"] (setboolopt "anon") "anonymize accounts and payees"
|
,flagNone ["anon"] (setboolopt "anon") "anonymize accounts and payees"
|
||||||
,flagReq ["pivot"] (\s opts -> Right $ setopt "pivot" s opts) "TAGNAME" "use some other field/tag for account names"
|
,flagReq ["pivot"] (\s opts -> Right $ setopt "pivot" s opts) "TAGNAME" "use some other field/tag for account names"
|
||||||
|
|||||||
@ -152,13 +152,14 @@ $ ./hledger-csv
|
|||||||
RULES
|
RULES
|
||||||
account1 Assets:MyAccount
|
account1 Assets:MyAccount
|
||||||
date %1
|
date %1
|
||||||
|
separator ;
|
||||||
date-format %d/%Y/%m
|
date-format %d/%Y/%m
|
||||||
description %2
|
description %2
|
||||||
amount-in %3
|
amount-in %3
|
||||||
amount-out %4
|
amount-out %4
|
||||||
currency $
|
currency $
|
||||||
|
|
||||||
$ ./hledger-csv --separator ';'
|
$ ./hledger-csv
|
||||||
2009/09/10 Flubber Co🎅
|
2009/09/10 Flubber Co🎅
|
||||||
Assets:MyAccount $50
|
Assets:MyAccount $50
|
||||||
income:unknown $-50
|
income:unknown $-50
|
||||||
@ -527,7 +528,7 @@ $ ./hledger-csv
|
|||||||
|
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
# 25. specify alternative whitespace delimiter in rules
|
# 25. specify reserved word whitespace separator in rules
|
||||||
<
|
<
|
||||||
2009/10/01 Flubber Co 50 123
|
2009/10/01 Flubber Co 50 123
|
||||||
|
|
||||||
@ -543,39 +544,7 @@ $ ./hledger-csv
|
|||||||
|
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
# 26. specify char delimiter in rules
|
## 26. A single unbalanced posting with number other than 1 also should not generate a balancing posting.
|
||||||
<
|
|
||||||
2009/10/01;Flubber Co;50;123
|
|
||||||
|
|
||||||
RULES
|
|
||||||
fields date, description, amount, balance
|
|
||||||
currency $
|
|
||||||
account1 (assets:myacct)
|
|
||||||
separator ;
|
|
||||||
|
|
||||||
$ ./hledger-csv
|
|
||||||
2009/10/01 Flubber Co
|
|
||||||
(assets:myacct) $50 = $123
|
|
||||||
|
|
||||||
>=0
|
|
||||||
|
|
||||||
# 27. command line delimiter overrides configuration file
|
|
||||||
<
|
|
||||||
2009/10/01 Flubber Co 50 123
|
|
||||||
|
|
||||||
RULES
|
|
||||||
fields date, description, amount, balance
|
|
||||||
currency $
|
|
||||||
account1 (assets:myacct)
|
|
||||||
separator ;
|
|
||||||
|
|
||||||
$ ./hledger-csv --separator 'TAB'
|
|
||||||
2009/10/01 Flubber Co
|
|
||||||
(assets:myacct) $50 = $123
|
|
||||||
|
|
||||||
>=0
|
|
||||||
|
|
||||||
## 28. A single unbalanced posting with number other than 1 also should not generate a balancing posting.
|
|
||||||
#<
|
#<
|
||||||
#2019-01-01,1
|
#2019-01-01,1
|
||||||
#
|
#
|
||||||
@ -589,7 +558,7 @@ $ ./hledger-csv --separator 'TAB'
|
|||||||
#
|
#
|
||||||
#>=0
|
#>=0
|
||||||
#
|
#
|
||||||
## 29. A single posting that's zero also should not generate a balancing posting.
|
## 27. A single posting that's zero also should not generate a balancing posting.
|
||||||
#<
|
#<
|
||||||
#2019-01-01,0
|
#2019-01-01,0
|
||||||
#
|
#
|
||||||
@ -603,7 +572,7 @@ $ ./hledger-csv --separator 'TAB'
|
|||||||
#
|
#
|
||||||
#>=0
|
#>=0
|
||||||
|
|
||||||
## 30. With a bracketed account name, the auto-generated second posting should also be bracketed.
|
## 28. With a bracketed account name, the auto-generated second posting should also be bracketed.
|
||||||
#<
|
#<
|
||||||
#2019-01-01,1
|
#2019-01-01,1
|
||||||
#
|
#
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user