dev: refactor PrefixedFilePath
This commit is contained in:
parent
a8eb2ff92d
commit
bfbef4bcbb
@ -87,7 +87,6 @@ This is used by the import command.
|
||||
module Hledger.Read (
|
||||
|
||||
-- * Journal files
|
||||
PrefixedFilePath,
|
||||
defaultJournal,
|
||||
defaultJournalWith,
|
||||
defaultJournalSafely,
|
||||
@ -227,10 +226,6 @@ defaultJournalPath = do
|
||||
home <- fromMaybe "" <$> getHomeSafe
|
||||
return $ home </> journalDefaultFilename
|
||||
|
||||
-- | A file path optionally prefixed by a reader name and colon
|
||||
-- (journal:, csv:, timedot:, etc.).
|
||||
type PrefixedFilePath = FilePath
|
||||
|
||||
-- | @readJournal iopts mfile txt@
|
||||
--
|
||||
-- Read a Journal from some handle, with strict checks if enabled,
|
||||
|
||||
@ -30,6 +30,8 @@ Some of these might belong in Hledger.Read.JournalReader or Hledger.Read.
|
||||
--- ** exports
|
||||
module Hledger.Read.Common (
|
||||
Reader (..),
|
||||
PrefixedFilePath,
|
||||
isStdin,
|
||||
InputOpts(..),
|
||||
HasInputOpts(..),
|
||||
definputopts,
|
||||
@ -193,6 +195,17 @@ data Reader m = Reader {
|
||||
|
||||
instance Show (Reader m) where show r = show (rFormat r) ++ " reader"
|
||||
|
||||
-- | A file path optionally prefixed by a reader name and colon (journal:, csv:, timedot:, etc.).
|
||||
-- The file path part can also be - meaning standard input.
|
||||
type PrefixedFilePath = FilePath
|
||||
|
||||
-- | Is this the special file path meaning standard input ? (-, possibly prefixed)
|
||||
isStdin :: PrefixedFilePath -> Bool
|
||||
isStdin f = case splitAtElement ':' f of
|
||||
[_,"-"] -> True
|
||||
["-"] -> True
|
||||
_ -> False
|
||||
|
||||
-- | Parse an InputOpts from a RawOpts and a provided date.
|
||||
-- This will fail with a usage error if the forecast period expression cannot be parsed.
|
||||
rawOptsToInputOpts :: Day -> Bool -> Bool -> RawOpts -> InputOpts
|
||||
|
||||
@ -167,14 +167,6 @@ findReader Nothing (Just path) =
|
||||
(prefix,path') = splitReaderPrefix path
|
||||
ext = map toLower $ drop 1 $ takeExtension path'
|
||||
|
||||
-- | A prefix used to specify a particular reader to be used for a file path,
|
||||
-- overriding the file extension. It is a valid reader name followed by a colon.
|
||||
-- Eg journal:, csv:, timeclock:, timedot:.
|
||||
-- type ReaderPrefix = String
|
||||
|
||||
-- | A file path with an optional reader prefix.
|
||||
type PrefixedFilePath = FilePath
|
||||
|
||||
-- | Separate a file path and its reader prefix, if any.
|
||||
--
|
||||
-- >>> splitReaderPrefix "csv:-"
|
||||
|
||||
@ -241,7 +241,7 @@ withJournalCached defaultJournalOverride cliopts cmd = do
|
||||
return (cache, journal)
|
||||
Nothing -> do
|
||||
dbg1IO ("readAndCacheJournalFile reading and caching "++fp) iopts
|
||||
journal <- runExceptT $ if snd (splitReaderPrefix fp) == "-" then readStdin else readJournalFile iopts fp
|
||||
journal <- runExceptT $ if isStdin fp then readStdin else readJournalFile iopts fp
|
||||
either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal
|
||||
where
|
||||
-- InputOptions contain reportspan_ that is used to calculate forecast period,
|
||||
@ -257,7 +257,7 @@ withJournalCached defaultJournalOverride cliopts cmd = do
|
||||
-- forecast transactions are never generated before journal end
|
||||
-- unless specifically requested).
|
||||
Just forecastspan -> forecastspan `spanValidDefaultsFrom` reportspan_ iopts
|
||||
-- Read stdin, or if we read it alread, use a cache
|
||||
-- Read stdin, or if we read it already, use a cache
|
||||
-- readStdin :: InputOpts -> ExceptT String IO Journal
|
||||
readStdin = do
|
||||
stdinContent <- liftIO $ modifyMVar stdinCache $ \cache ->
|
||||
|
||||
Loading…
Reference in New Issue
Block a user