make account type autodetection (& hledger-smooth) case insensitive again (#1341)
lib: added case-insensitive variants of the accountNameToRegex functions.
This commit is contained in:
		
							parent
							
								
									a229b658e8
								
							
						
					
					
						commit
						c2929939e4
					
				| @ -66,7 +66,7 @@ main = do | ||||
|       args = words' $ query_ ropts | ||||
|       q = queryFromOpts today ropts | ||||
|       acct = T.pack $ headDef (error' "Please provide an account name argument") args | ||||
|       pr = postingsReport ropts (And [Acct $ accountNameToAccountRegex acct, q]) j | ||||
|       pr = postingsReport ropts (And [Acct $ accountNameToAccountRegexCI acct, q]) j | ||||
| 
 | ||||
|       -- dates of postings to acct (in report) | ||||
|       pdates = map (postingDate . fourth5) (snd pr) | ||||
|  | ||||
| @ -16,7 +16,9 @@ module Hledger.Data.AccountName ( | ||||
|   ,accountNameFromComponents | ||||
|   ,accountNameLevel | ||||
|   ,accountNameToAccountOnlyRegex | ||||
|   ,accountNameToAccountOnlyRegexCI | ||||
|   ,accountNameToAccountRegex | ||||
|   ,accountNameToAccountRegexCI | ||||
|   ,accountNameTreeFrom | ||||
|   ,accountSummarisedName | ||||
|   ,acctsep | ||||
| @ -218,10 +220,20 @@ escapeName = T.unpack . T.concatMap escapeChar | ||||
| accountNameToAccountRegex :: AccountName -> Regexp | ||||
| accountNameToAccountRegex a = toRegex' $ '^' : escapeName a ++ "(:|$)"  -- PARTIAL: Is this safe after escapeName? | ||||
| 
 | ||||
| -- | Convert an account name to a regular expression matching it and its subaccounts, | ||||
| -- case insensitively. | ||||
| accountNameToAccountRegexCI :: AccountName -> Regexp | ||||
| accountNameToAccountRegexCI a = toRegexCI' $ '^' : escapeName a ++ "(:|$)"  -- PARTIAL: Is this safe after escapeName? | ||||
| 
 | ||||
| -- | Convert an account name to a regular expression matching it but not its subaccounts. | ||||
| accountNameToAccountOnlyRegex :: AccountName -> Regexp | ||||
| accountNameToAccountOnlyRegex a = toRegex' $ '^' : escapeName a ++ "$" -- PARTIAL: Is this safe after escapeName? | ||||
| 
 | ||||
| -- | Convert an account name to a regular expression matching it but not its subaccounts, | ||||
| -- case insensitively. | ||||
| accountNameToAccountOnlyRegexCI :: AccountName -> Regexp | ||||
| accountNameToAccountOnlyRegexCI a = toRegexCI' $ '^' : escapeName a ++ "$" -- PARTIAL: Is this safe after escapeName? | ||||
| 
 | ||||
| -- -- | Does this string look like an exact account-matching regular expression ? | ||||
| --isAccountRegex  :: String -> Bool | ||||
| --isAccountRegex s = take 1 s == "^" && take 5 (reverse s) == ")$|:(" | ||||
|  | ||||
| @ -300,7 +300,7 @@ journalAccountNameTree = accountNameTreeFrom . journalAccountNames | ||||
| -- or otherwise for accounts with names matched by the case-insensitive  | ||||
| -- regular expression @^assets?(:|$)@. | ||||
| journalAssetAccountQuery :: Journal -> Query | ||||
| journalAssetAccountQuery = journalAccountTypeQuery [Asset,Cash] (toRegex' "^assets?(:|$)") | ||||
| journalAssetAccountQuery = journalAccountTypeQuery [Asset,Cash] (toRegexCI' "^assets?(:|$)") | ||||
| 
 | ||||
| -- | A query for "Cash" (liquid asset) accounts in this journal, ie accounts | ||||
| -- declared as Cash by account directives, or otherwise with names matched by the  | ||||
| @ -309,7 +309,7 @@ journalAssetAccountQuery = journalAccountTypeQuery [Asset,Cash] (toRegex' "^asse | ||||
| journalCashAccountQuery  :: Journal -> Query | ||||
| journalCashAccountQuery j = | ||||
|   case M.lookup Cash (jdeclaredaccounttypes j) of | ||||
|     Nothing -> And [ journalAssetAccountQuery j, Not . Acct $ toRegex' "(investment|receivable|:A/R|:fixed)" ] | ||||
|     Nothing -> And [ journalAssetAccountQuery j, Not . Acct $ toRegexCI' "(investment|receivable|:A/R|:fixed)" ] | ||||
|     Just _  -> journalAccountTypeQuery [Cash] notused j | ||||
|       where notused = error' "journalCashAccountQuery: this should not have happened!"  -- PARTIAL: | ||||
| 
 | ||||
| @ -318,28 +318,28 @@ journalCashAccountQuery j = | ||||
| -- accounts with names matched by the case-insensitive regular expression | ||||
| -- @^(debts?|liabilit(y|ies))(:|$)@. | ||||
| journalLiabilityAccountQuery :: Journal -> Query | ||||
| journalLiabilityAccountQuery = journalAccountTypeQuery [Liability] (toRegex' "^(debts?|liabilit(y|ies))(:|$)") | ||||
| journalLiabilityAccountQuery = journalAccountTypeQuery [Liability] (toRegexCI' "^(debts?|liabilit(y|ies))(:|$)") | ||||
| 
 | ||||
| -- | A query for accounts in this journal which have been | ||||
| -- declared as Equity by account directives, or otherwise for | ||||
| -- accounts with names matched by the case-insensitive regular expression | ||||
| -- @^equity(:|$)@. | ||||
| journalEquityAccountQuery :: Journal -> Query | ||||
| journalEquityAccountQuery = journalAccountTypeQuery [Equity] (toRegex' "^equity(:|$)") | ||||
| journalEquityAccountQuery = journalAccountTypeQuery [Equity] (toRegexCI' "^equity(:|$)") | ||||
| 
 | ||||
| -- | A query for accounts in this journal which have been | ||||
| -- declared as Revenue by account directives, or otherwise for | ||||
| -- accounts with names matched by the case-insensitive regular expression | ||||
| -- @^(income|revenue)s?(:|$)@. | ||||
| journalRevenueAccountQuery :: Journal -> Query | ||||
| journalRevenueAccountQuery = journalAccountTypeQuery [Revenue] (toRegex' "^(income|revenue)s?(:|$)") | ||||
| journalRevenueAccountQuery = journalAccountTypeQuery [Revenue] (toRegexCI' "^(income|revenue)s?(:|$)") | ||||
| 
 | ||||
| -- | A query for accounts in this journal which have been | ||||
| -- declared as Expense by account directives, or otherwise for | ||||
| -- accounts with names matched by the case-insensitive regular expression | ||||
| -- @^expenses?(:|$)@. | ||||
| journalExpenseAccountQuery  :: Journal -> Query | ||||
| journalExpenseAccountQuery = journalAccountTypeQuery [Expense] (toRegex' "^expenses?(:|$)") | ||||
| journalExpenseAccountQuery = journalAccountTypeQuery [Expense] (toRegexCI' "^expenses?(:|$)") | ||||
| 
 | ||||
| -- | A query for Asset, Liability & Equity accounts in this journal. | ||||
| -- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Balance_Sheet_Accounts>. | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| # 1. trivial balance sheet | ||||
| # 1. A trivial balance sheet. With no accounts of type Asset declared, | ||||
| # a top-level "assets" account is autodetected as an Asset. | ||||
| < | ||||
| 2016/1/1 | ||||
|   assets  1 | ||||
| @ -21,7 +22,30 @@ Balance Sheet 2016-01-01 | ||||
| =============++============ | ||||
|  Net:        ||          1  | ||||
| 
 | ||||
| # 2. monthly balance sheet, normal positive sign | ||||
| # 2. Account type autodetection is case insensitive. | ||||
| < | ||||
| 2016/1/1 | ||||
|   ASSETS  1 | ||||
|   b | ||||
| $ hledger -f - balancesheet | ||||
| Balance Sheet 2016-01-01 | ||||
| 
 | ||||
|              || 2016-01-01  | ||||
| =============++============ | ||||
|  Assets      ||             | ||||
| -------------++------------ | ||||
|  ASSETS      ||          1  | ||||
| -------------++------------ | ||||
|              ||          1  | ||||
| =============++============ | ||||
|  Liabilities ||             | ||||
| -------------++------------ | ||||
| -------------++------------ | ||||
|              ||             | ||||
| =============++============ | ||||
|  Net:        ||          1  | ||||
| 
 | ||||
| # 3. monthly balance sheet, normal positive sign | ||||
| # old (arithmetic sign): | ||||
| #Balance Sheet | ||||
| # | ||||
| @ -64,7 +88,7 @@ Balance Sheet 2008-01-31..2008-12-31 | ||||
| ======================++================================================================================================================================================ | ||||
|  Net:                 ||         $1          $1          $1          $1          $1           0           0           0           0           0           0           0  | ||||
| 
 | ||||
| # 3. monthly balance sheet in tree mode | ||||
| # 4. monthly balance sheet in tree mode | ||||
| # old (arithmetic sign): | ||||
| #              || 2008/01/31  2008/02/29  2008/03/31  2008/04/30  2008/05/31  2008/06/30  2008/07/31  2008/08/31  2008/09/30  2008/10/31  2008/11/30  2008/12/31  | ||||
| #==============++================================================================================================================================================ | ||||
| @ -110,7 +134,7 @@ Balance Sheet 2008-01-31..2008-12-31 | ||||
| ===================++================================================================================================================================================ | ||||
|  Net:              ||         $1          $1          $1          $1          $1           0           0           0           0           0           0           0  | ||||
| 
 | ||||
| # 4. monthly balancesheet with average column and without overall totals row. | ||||
| # 5. monthly balancesheet with average column and without overall totals row. | ||||
| # Total column is requested but not shown because balancesheet is in historical mode | ||||
| # by default (shows ending balances). | ||||
| $ hledger -f sample.journal balancesheet -p 'monthly in 2008' -NAT | ||||
| @ -128,7 +152,7 @@ Balance Sheet 2008-01-31..2008-12-31 | ||||
| ----------------------++--------------------------------------------------------------------------------------------------------------------------------------------------------- | ||||
|  liabilities:debts    ||          0           0           0           0           0           0           0           0           0           0           0         $-1        0  | ||||
| 
 | ||||
| # 5. Tree output still works, #565 | ||||
| # 6. Tree output still works, #565 | ||||
| < | ||||
| 2017/1/1 | ||||
|   (assets:b)  1 | ||||
| @ -150,7 +174,7 @@ Balance Sheet 2017-01-01 | ||||
| =============++============ | ||||
|  Net:        ||          1  | ||||
| 
 | ||||
| # 6. Flat output still works, #552 | ||||
| # 7. Flat output still works, #552 | ||||
| < | ||||
| 2017/1/1 | ||||
|   (assets:b)  1 | ||||
| @ -172,7 +196,7 @@ Balance Sheet 2017-01-01 | ||||
| =============++============ | ||||
|  Net:        ||          1  | ||||
| 
 | ||||
| # 7. An empty section does not disrupt the overall totals, #588 | ||||
| # 8. An empty section does not disrupt the overall totals, #588 | ||||
| < | ||||
| 2017/1/1 | ||||
|   (assets)  $1 | ||||
| @ -194,7 +218,7 @@ Balance Sheet 2017-12-31 | ||||
| =============++===================== | ||||
|  Net:        ||         $1       $1  | ||||
| 
 | ||||
| # 8. --pretty-tables uses unicode chars for borders | ||||
| # 9. --pretty-tables uses unicode chars for borders | ||||
| < | ||||
| 2016/1/1 | ||||
|   assets  1 | ||||
| @ -217,7 +241,7 @@ Balance Sheet 2016-01-31 | ||||
| ═════════════╬════════════ | ||||
|  Net:        ║          1  | ||||
| 
 | ||||
| # 9. Check that accounts brought to zero by subaccount balances | ||||
| # 10. Check that accounts brought to zero by subaccount balances | ||||
| # are not erased from balancesheet | ||||
| < | ||||
| 2018-10-01 | ||||
| @ -255,7 +279,7 @@ Balance Sheet 2018-10-03 | ||||
| =====================================++============ | ||||
|  Net:                                ||       $120  | ||||
| 
 | ||||
| # 10. Check that starting balances are also filtered by subreport query. (See issue #1335) | ||||
| # 11. Check that starting balances are also filtered by subreport query. (See issue #1335) | ||||
| < | ||||
| 2020-03-01 * Rent | ||||
|     assets:a     -$1 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user