{-# LANGUAGE OverloadedStrings #-}
module Hledger.Read.TimeclockReader (
reader,
timeclockfilep,
)
where
import Control.Monad
import Control.Monad.Except (ExceptT, liftEither)
import Control.Monad.State.Strict
import Data.Char (isSpace)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Text.Megaparsec hiding (parse)
import Hledger.Data
import Hledger.Read.Common
import Hledger.Utils
import Data.Text as T (strip)
import Data.Functor ((<&>))
reader :: MonadIO m => Reader m
reader :: forall (m :: * -> *). MonadIO m => Reader m
reader = Reader
{rFormat :: StorageFormat
rFormat = StorageFormat
Timeclock
,rExtensions :: [String]
rExtensions = [String
"timeclock"]
,rReadFn :: InputOpts -> String -> Handle -> ExceptT String IO Journal
rReadFn = (InputOpts -> String -> Text -> ExceptT String IO Journal)
-> InputOpts -> String -> Handle -> ExceptT String IO Journal
handleReadFnToTextReadFn InputOpts -> String -> Text -> ExceptT String IO Journal
parse
,rParser :: MonadIO m => InputOpts -> ErroringJournalParser m Journal
rParser = MonadIO m => InputOpts -> ErroringJournalParser m Journal
InputOpts -> ErroringJournalParser m Journal
forall (m :: * -> *).
MonadIO m =>
InputOpts -> JournalParser m Journal
timeclockfilep
}
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
parse :: InputOpts -> String -> Text -> ExceptT String IO Journal
parse InputOpts
iopts String
fp Text
t = ErroringJournalParser IO Journal
-> InputOpts -> String -> Text -> ExceptT String IO Journal
initialiseAndParseJournal (InputOpts -> ErroringJournalParser IO Journal
forall (m :: * -> *).
MonadIO m =>
InputOpts -> JournalParser m Journal
timeclockfilep InputOpts
iopts) InputOpts
iopts String
fp Text
t
ExceptT String IO Journal
-> (Journal -> ExceptT String IO Journal)
-> ExceptT String IO Journal
forall a b.
ExceptT String IO a
-> (a -> ExceptT String IO b) -> ExceptT String IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either String Journal -> ExceptT String IO Journal
forall e (m :: * -> *) a. MonadError e m => Either e a -> m a
liftEither (Either String Journal -> ExceptT String IO Journal)
-> (Journal -> Either String Journal)
-> Journal
-> ExceptT String IO Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AccountAlias] -> Journal -> Either String Journal
journalApplyAliases (InputOpts -> [AccountAlias]
aliasesFromOpts InputOpts
iopts)
ExceptT String IO Journal
-> (Journal -> ExceptT String IO Journal)
-> ExceptT String IO Journal
forall a b.
ExceptT String IO a
-> (a -> ExceptT String IO b) -> ExceptT String IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= InputOpts -> String -> Text -> Journal -> ExceptT String IO Journal
journalFinalise InputOpts
iopts String
fp Text
t
timeclockfilep :: MonadIO m => InputOpts -> JournalParser m ParsedJournal
timeclockfilep :: forall (m :: * -> *).
MonadIO m =>
InputOpts -> JournalParser m Journal
timeclockfilep InputOpts
iopts = do
StateT Journal (ParsecT HledgerParseErrorData Text m) ()
-> StateT Journal (ParsecT HledgerParseErrorData Text m) [()]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall {m :: * -> *}.
StateT Journal (ParsecT HledgerParseErrorData Text m) ()
timeclockitemp
StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall e s (m :: * -> *). MonadParsec e s m => m ()
eof
j@Journal{jparsetimeclockentries=es} <- StateT Journal (ParsecT HledgerParseErrorData Text m) Journal
forall s (m :: * -> *). MonadState s m => m s
get
now <- liftIO getCurrentLocalTime
let
j' = if InputOpts -> Bool
_oldtimeclock InputOpts
iopts
then
Journal
j{jtxns = reverse $ timeclockToTransactionsOld now $ reverse es, jparsetimeclockentries = []}
else
Journal
j{jtxns = reverse $ timeclockToTransactions now es, jparsetimeclockentries = []}
return j'
where
timeclockitemp :: StateT Journal (ParsecT HledgerParseErrorData Text m) ()
timeclockitemp = [StateT Journal (ParsecT HledgerParseErrorData Text m) ()]
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [
StateT Journal (ParsecT HledgerParseErrorData Text m) ()
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT HledgerParseErrorData Text m ()
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall (m :: * -> *) a. Monad m => m a -> StateT Journal m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ParsecT HledgerParseErrorData Text m ()
forall (m :: * -> *). TextParser m ()
emptyorcommentlinep)
,JournalParser m TimeclockEntry
forall {m :: * -> *}. JournalParser m TimeclockEntry
entryp JournalParser m TimeclockEntry
-> (TimeclockEntry
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ())
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall a b.
StateT Journal (ParsecT HledgerParseErrorData Text m) a
-> (a -> StateT Journal (ParsecT HledgerParseErrorData Text m) b)
-> StateT Journal (ParsecT HledgerParseErrorData Text m) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \TimeclockEntry
e -> (Journal -> Journal)
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (\Journal
j -> Journal
j{jparsetimeclockentries = e : jparsetimeclockentries j})
] StateT Journal (ParsecT HledgerParseErrorData Text m) ()
-> String
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"timeclock entry, comment line, or empty line"
where entryp :: JournalParser m TimeclockEntry
entryp = if InputOpts -> Bool
_oldtimeclock InputOpts
iopts then JournalParser m TimeclockEntry
forall {m :: * -> *}. JournalParser m TimeclockEntry
oldtimeclockentryp else JournalParser m TimeclockEntry
forall {m :: * -> *}. JournalParser m TimeclockEntry
timeclockentryp
oldtimeclockentryp :: JournalParser m TimeclockEntry
oldtimeclockentryp :: forall {m :: * -> *}. JournalParser m TimeclockEntry
oldtimeclockentryp = do
pos <- StateT Journal (ParsecT HledgerParseErrorData Text m) SourcePos
forall s e (m :: * -> *).
(TraversableStream s, MonadParsec e s m) =>
m SourcePos
getSourcePos
code <- oneOf ("bhioO" :: [Char])
lift skipNonNewlineSpaces1
datetime <- datetimep
account <- fmap (fromMaybe "") $ optional $ lift skipNonNewlineSpaces1 >> modifiedaccountnamep True
description <- fmap (maybe "" T.strip) $ optional $ lift $ skipNonNewlineSpaces1 >> descriptionp
(comment, tags) <- lift transactioncommentp
return $ TimeclockEntry pos (read [code]) datetime account description comment tags
timeclockentryp :: JournalParser m TimeclockEntry
timeclockentryp :: forall {m :: * -> *}. JournalParser m TimeclockEntry
timeclockentryp = do
pos <- StateT Journal (ParsecT HledgerParseErrorData Text m) SourcePos
forall s e (m :: * -> *).
(TraversableStream s, MonadParsec e s m) =>
m SourcePos
getSourcePos
code <- oneOf ("iobhO" :: [Char])
lift skipNonNewlineSpaces1
datetime <- datetimep
(account, description) <- case code of
Char
'i' -> do
ParsecT HledgerParseErrorData Text m ()
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall (m :: * -> *) a. Monad m => m a -> StateT Journal m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ParsecT HledgerParseErrorData Text m ()
forall s (m :: * -> *).
(Stream s, Token s ~ Char) =>
ParsecT HledgerParseErrorData s m ()
skipNonNewlineSpaces1
a <- Bool -> JournalParser m Text
forall (m :: * -> *). Bool -> JournalParser m Text
modifiedaccountnamep Bool
False
d <- optional (lift $ skipNonNewlineSpaces1 >> descriptionp) <&> maybe "" T.strip
return (a, d)
Char
'o' -> do
a <- JournalParser m Text
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Maybe Text)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (StateT Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
forall a.
StateT Journal (ParsecT HledgerParseErrorData Text m) a
-> StateT Journal (ParsecT HledgerParseErrorData Text m) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (ParsecT HledgerParseErrorData Text m ()
-> StateT Journal (ParsecT HledgerParseErrorData Text m) ()
forall (m :: * -> *) a. Monad m => m a -> StateT Journal m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ParsecT HledgerParseErrorData Text m ()
forall s (m :: * -> *).
(Stream s, Token s ~ Char) =>
ParsecT HledgerParseErrorData s m ()
skipNonNewlineSpaces1 StateT Journal (ParsecT HledgerParseErrorData Text m) ()
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
forall a b.
StateT Journal (ParsecT HledgerParseErrorData Text m) a
-> StateT Journal (ParsecT HledgerParseErrorData Text m) b
-> StateT Journal (ParsecT HledgerParseErrorData Text m) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> StateT Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
forall a.
StateT Journal (ParsecT HledgerParseErrorData Text m) a
-> StateT Journal (ParsecT HledgerParseErrorData Text m) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
lookAhead ((Token Text -> Bool)
-> StateT
Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (\Token Text
c -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Char -> Bool
isSpace Char
Token Text
c Bool -> Bool -> Bool
|| Char -> Bool
isSameLineCommentStart Char
Token Text
c)))
StateT Journal (ParsecT HledgerParseErrorData Text m) (Token Text)
-> JournalParser m Text -> JournalParser m Text
forall a b.
StateT Journal (ParsecT HledgerParseErrorData Text m) a
-> StateT Journal (ParsecT HledgerParseErrorData Text m) b
-> StateT Journal (ParsecT HledgerParseErrorData Text m) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> JournalParser m Text
forall (m :: * -> *). Bool -> JournalParser m Text
modifiedaccountnamep Bool
False) StateT Journal (ParsecT HledgerParseErrorData Text m) (Maybe Text)
-> (Maybe Text -> Text) -> JournalParser m Text
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
""
return (a, "")
Char
_ -> Tag -> StateT Journal (ParsecT HledgerParseErrorData Text m) Tag
forall a.
a -> StateT Journal (ParsecT HledgerParseErrorData Text m) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
"", Text
"")
lift skipNonNewlineSpaces
(comment, tags) <- lift $ optional transactioncommentp <&> fromMaybe ("",[])
return $ TimeclockEntry pos (read [code]) datetime account description comment tags