filter by metadata tag
This commit is contained in:
parent
b6b5c5ffaa
commit
dafa764a07
@ -123,6 +123,7 @@ nullfilterspec = FilterSpec {
|
|||||||
,acctpats=[]
|
,acctpats=[]
|
||||||
,descpats=[]
|
,descpats=[]
|
||||||
,depth=Nothing
|
,depth=Nothing
|
||||||
|
,metadata=[]
|
||||||
}
|
}
|
||||||
|
|
||||||
journalFilePath :: Journal -> FilePath
|
journalFilePath :: Journal -> FilePath
|
||||||
@ -234,10 +235,12 @@ filterJournalTransactions FilterSpec{datespan=datespan
|
|||||||
,acctpats=apats
|
,acctpats=apats
|
||||||
,descpats=dpats
|
,descpats=dpats
|
||||||
,depth=depth
|
,depth=depth
|
||||||
|
,metadata=md
|
||||||
} =
|
} =
|
||||||
filterJournalTransactionsByClearedStatus cleared .
|
filterJournalTransactionsByClearedStatus cleared .
|
||||||
filterJournalPostingsByDepth depth .
|
filterJournalPostingsByDepth depth .
|
||||||
filterJournalTransactionsByAccount apats .
|
filterJournalTransactionsByAccount apats .
|
||||||
|
filterJournalTransactionsByMetadata md .
|
||||||
filterJournalTransactionsByDescription dpats .
|
filterJournalTransactionsByDescription dpats .
|
||||||
filterJournalTransactionsByDate datespan
|
filterJournalTransactionsByDate datespan
|
||||||
|
|
||||||
@ -251,15 +254,22 @@ filterJournalPostings FilterSpec{datespan=datespan
|
|||||||
,acctpats=apats
|
,acctpats=apats
|
||||||
,descpats=dpats
|
,descpats=dpats
|
||||||
,depth=depth
|
,depth=depth
|
||||||
|
,metadata=md
|
||||||
} =
|
} =
|
||||||
filterJournalPostingsByRealness real .
|
filterJournalPostingsByRealness real .
|
||||||
filterJournalPostingsByClearedStatus cleared .
|
filterJournalPostingsByClearedStatus cleared .
|
||||||
filterJournalPostingsByEmpty empty .
|
filterJournalPostingsByEmpty empty .
|
||||||
filterJournalPostingsByDepth depth .
|
filterJournalPostingsByDepth depth .
|
||||||
filterJournalPostingsByAccount apats .
|
filterJournalPostingsByAccount apats .
|
||||||
|
filterJournalTransactionsByMetadata md .
|
||||||
filterJournalTransactionsByDescription dpats .
|
filterJournalTransactionsByDescription dpats .
|
||||||
filterJournalTransactionsByDate datespan
|
filterJournalTransactionsByDate datespan
|
||||||
|
|
||||||
|
-- | Keep only transactions whose metadata matches all metadata specifications.
|
||||||
|
filterJournalTransactionsByMetadata :: [(String,String)] -> Journal -> Journal
|
||||||
|
filterJournalTransactionsByMetadata pats j@Journal{jtxns=ts} = j{jtxns=filter matchmd ts}
|
||||||
|
where matchmd t = all (`elem` tmetadata t) pats
|
||||||
|
|
||||||
-- | Keep only transactions whose description matches the description patterns.
|
-- | Keep only transactions whose description matches the description patterns.
|
||||||
filterJournalTransactionsByDescription :: [String] -> Journal -> Journal
|
filterJournalTransactionsByDescription :: [String] -> Journal -> Journal
|
||||||
filterJournalTransactionsByDescription pats j@Journal{jtxns=ts} = j{jtxns=filter matchdesc ts}
|
filterJournalTransactionsByDescription pats j@Journal{jtxns=ts} = j{jtxns=filter matchdesc ts}
|
||||||
|
|||||||
@ -263,5 +263,6 @@ data FilterSpec = FilterSpec {
|
|||||||
,acctpats :: [String] -- ^ only include if matching these account patterns
|
,acctpats :: [String] -- ^ only include if matching these account patterns
|
||||||
,descpats :: [String] -- ^ only include if matching these description patterns
|
,descpats :: [String] -- ^ only include if matching these description patterns
|
||||||
,depth :: Maybe Int
|
,depth :: Maybe Int
|
||||||
|
,metadata :: [(String,String)] -- ^ only include if matching these metadata
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
|
|||||||
@ -177,8 +177,9 @@ filterSpecFromOpts opts@ReportOpts{..} d = FilterSpec {
|
|||||||
,acctpats=apats
|
,acctpats=apats
|
||||||
,descpats=dpats
|
,descpats=dpats
|
||||||
,depth = depth_
|
,depth = depth_
|
||||||
|
,metadata = mds
|
||||||
}
|
}
|
||||||
where (apats,dpats) = parsePatternArgs patterns_
|
where (apats,dpats,mds) = parsePatternArgs patterns_
|
||||||
|
|
||||||
-- | Convert report options to a (new) query.
|
-- | Convert report options to a (new) query.
|
||||||
queryFromOpts :: ReportOpts -> Day -> Query
|
queryFromOpts :: ReportOpts -> Day -> Query
|
||||||
@ -200,12 +201,21 @@ queryFromOpts opts@ReportOpts{..} d = -- strace $
|
|||||||
-- follows: those prefixed with "desc:" are description patterns, all
|
-- follows: those prefixed with "desc:" are description patterns, all
|
||||||
-- others are account patterns; also patterns prefixed with "not:" are
|
-- others are account patterns; also patterns prefixed with "not:" are
|
||||||
-- negated. not: should come after desc: if both are used.
|
-- negated. not: should come after desc: if both are used.
|
||||||
parsePatternArgs :: [String] -> ([String],[String])
|
-- pattern "tag" means the word after it should be interpreted as metadata
|
||||||
parsePatternArgs args = (as, ds')
|
-- constraint.
|
||||||
|
parsePatternArgs :: [String] -> ([String],[String],[(String,String)])
|
||||||
|
parsePatternArgs args = (as, ds', mds)
|
||||||
where
|
where
|
||||||
|
(tags, args') = filterOutTags False [] [] args
|
||||||
descprefix = "desc:"
|
descprefix = "desc:"
|
||||||
(ds, as) = partition (descprefix `isPrefixOf`) args
|
(ds, as) = partition (descprefix `isPrefixOf`) args'
|
||||||
ds' = map (drop (length descprefix)) ds
|
ds' = map (drop (length descprefix)) ds
|
||||||
|
mds = map (\(a,b)->(a,tail b)) $ map (\t->span (/='=') t) tags
|
||||||
|
|
||||||
|
filterOutTags _ tags args' [] = (reverse tags, reverse args')
|
||||||
|
filterOutTags False tags args' ("tag":xs) = filterOutTags True tags args' xs
|
||||||
|
filterOutTags False tags args' (x:xs) = filterOutTags False tags (x:args') xs
|
||||||
|
filterOutTags True tags args' (x:xs) = filterOutTags False (x:tags) args' xs
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@ -18,3 +18,28 @@ bin/hledger -f - print
|
|||||||
|
|
||||||
>>>2
|
>>>2
|
||||||
>>>=0
|
>>>=0
|
||||||
|
|
||||||
|
bin/hledger -f - print tag key1=value
|
||||||
|
<<<
|
||||||
|
2010/01/01
|
||||||
|
; key:value
|
||||||
|
a 1
|
||||||
|
b -1
|
||||||
|
>>>
|
||||||
|
>>>2
|
||||||
|
>>>=0
|
||||||
|
|
||||||
|
bin/hledger -f - print tag key=value
|
||||||
|
<<<
|
||||||
|
2010/01/01
|
||||||
|
; key:value
|
||||||
|
a 1
|
||||||
|
b -1
|
||||||
|
>>>
|
||||||
|
2010/01/01
|
||||||
|
; key:value
|
||||||
|
a 1
|
||||||
|
b -1
|
||||||
|
|
||||||
|
>>>2
|
||||||
|
>>>=0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user