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 (
|
module Hledger.Read (
|
||||||
|
|
||||||
-- * Journal files
|
-- * Journal files
|
||||||
PrefixedFilePath,
|
|
||||||
defaultJournal,
|
defaultJournal,
|
||||||
defaultJournalWith,
|
defaultJournalWith,
|
||||||
defaultJournalSafely,
|
defaultJournalSafely,
|
||||||
@ -227,10 +226,6 @@ defaultJournalPath = do
|
|||||||
home <- fromMaybe "" <$> getHomeSafe
|
home <- fromMaybe "" <$> getHomeSafe
|
||||||
return $ home </> journalDefaultFilename
|
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@
|
-- | @readJournal iopts mfile txt@
|
||||||
--
|
--
|
||||||
-- Read a Journal from some handle, with strict checks if enabled,
|
-- 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
|
--- ** exports
|
||||||
module Hledger.Read.Common (
|
module Hledger.Read.Common (
|
||||||
Reader (..),
|
Reader (..),
|
||||||
|
PrefixedFilePath,
|
||||||
|
isStdin,
|
||||||
InputOpts(..),
|
InputOpts(..),
|
||||||
HasInputOpts(..),
|
HasInputOpts(..),
|
||||||
definputopts,
|
definputopts,
|
||||||
@ -193,6 +195,17 @@ data Reader m = Reader {
|
|||||||
|
|
||||||
instance Show (Reader m) where show r = show (rFormat r) ++ " 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.
|
-- | 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.
|
-- This will fail with a usage error if the forecast period expression cannot be parsed.
|
||||||
rawOptsToInputOpts :: Day -> Bool -> Bool -> RawOpts -> InputOpts
|
rawOptsToInputOpts :: Day -> Bool -> Bool -> RawOpts -> InputOpts
|
||||||
|
|||||||
@ -167,14 +167,6 @@ findReader Nothing (Just path) =
|
|||||||
(prefix,path') = splitReaderPrefix path
|
(prefix,path') = splitReaderPrefix path
|
||||||
ext = map toLower $ drop 1 $ takeExtension 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.
|
-- | Separate a file path and its reader prefix, if any.
|
||||||
--
|
--
|
||||||
-- >>> splitReaderPrefix "csv:-"
|
-- >>> splitReaderPrefix "csv:-"
|
||||||
|
|||||||
@ -241,7 +241,7 @@ withJournalCached defaultJournalOverride cliopts cmd = do
|
|||||||
return (cache, journal)
|
return (cache, journal)
|
||||||
Nothing -> do
|
Nothing -> do
|
||||||
dbg1IO ("readAndCacheJournalFile reading and caching "++fp) iopts
|
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
|
either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal
|
||||||
where
|
where
|
||||||
-- InputOptions contain reportspan_ that is used to calculate forecast period,
|
-- 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
|
-- forecast transactions are never generated before journal end
|
||||||
-- unless specifically requested).
|
-- unless specifically requested).
|
||||||
Just forecastspan -> forecastspan `spanValidDefaultsFrom` reportspan_ iopts
|
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 :: InputOpts -> ExceptT String IO Journal
|
||||||
readStdin = do
|
readStdin = do
|
||||||
stdinContent <- liftIO $ modifyMVar stdinCache $ \cache ->
|
stdinContent <- liftIO $ modifyMVar stdinCache $ \cache ->
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user