fix a silly failure to open ledger file paths containing ~
And drop a misconception: there's no need to tilde-expand paths in command line arguments. Just the hard-coded ones.
This commit is contained in:
parent
485ab2b4d5
commit
dc3be35eef
41
Ledger/IO.hs
41
Ledger/IO.hs
@ -17,12 +17,13 @@ import System.Environment (getEnv)
|
|||||||
import System.IO
|
import System.IO
|
||||||
import Text.ParserCombinators.Parsec
|
import Text.ParserCombinators.Parsec
|
||||||
import qualified Data.Map as Map (lookup)
|
import qualified Data.Map as Map (lookup)
|
||||||
|
import System.FilePath ((</>))
|
||||||
|
|
||||||
|
|
||||||
ledgerdefaultpath = "~/.ledger"
|
ledgerenvvar = "LEDGER"
|
||||||
timelogdefaultpath = "~/.timelog"
|
timelogenvvar = "TIMELOG"
|
||||||
ledgerenvvar = "LEDGER"
|
ledgerdefaultfilename = ".ledger"
|
||||||
timelogenvvar = "TIMELOG"
|
timelogdefaultfilename = ".timelog"
|
||||||
|
|
||||||
-- | A tuple of arguments specifying how to filter a raw ledger file:
|
-- | A tuple of arguments specifying how to filter a raw ledger file:
|
||||||
--
|
--
|
||||||
@ -51,12 +52,18 @@ noioargs = (DateSpan Nothing Nothing, Nothing, False, False, [], [])
|
|||||||
-- | Get the user's default ledger file path.
|
-- | Get the user's default ledger file path.
|
||||||
myLedgerPath :: IO String
|
myLedgerPath :: IO String
|
||||||
myLedgerPath =
|
myLedgerPath =
|
||||||
getEnv ledgerenvvar `catch` \_ -> return ledgerdefaultpath
|
getEnv ledgerenvvar `catch`
|
||||||
|
(\_ -> do
|
||||||
|
home <- getHomeDirectory
|
||||||
|
return $ home </> ledgerdefaultfilename)
|
||||||
|
|
||||||
-- | Get the user's default timelog file path.
|
-- | Get the user's default timelog file path.
|
||||||
myTimelogPath :: IO String
|
myTimelogPath :: IO String
|
||||||
myTimelogPath =
|
myTimelogPath =
|
||||||
getEnv timelogenvvar `catch` \_ -> return timelogdefaultpath
|
getEnv timelogenvvar `catch`
|
||||||
|
(\_ -> do
|
||||||
|
home <- getHomeDirectory
|
||||||
|
return $ home </> timelogdefaultfilename)
|
||||||
|
|
||||||
-- | Read the user's default ledger file, or give an error.
|
-- | Read the user's default ledger file, or give an error.
|
||||||
myLedger :: IO Ledger
|
myLedger :: IO Ledger
|
||||||
@ -68,7 +75,7 @@ myTimelog = myTimelogPath >>= readLedger
|
|||||||
|
|
||||||
-- | Read a ledger from this file, with no filtering, or give an error.
|
-- | Read a ledger from this file, with no filtering, or give an error.
|
||||||
readLedger :: FilePath -> IO Ledger
|
readLedger :: FilePath -> IO Ledger
|
||||||
readLedger f = tildeExpand f >>= readLedgerWithIOArgs noioargs
|
readLedger = readLedgerWithIOArgs noioargs
|
||||||
|
|
||||||
-- | Read a ledger from this file, filtering according to the io args,
|
-- | Read a ledger from this file, filtering according to the io args,
|
||||||
-- | or give an error.
|
-- | or give an error.
|
||||||
@ -94,14 +101,14 @@ filterAndCacheLedger (span,cleared,real,costbasis,apats,dpats) rawtext rl =
|
|||||||
$ canonicaliseAmounts costbasis rl
|
$ canonicaliseAmounts costbasis rl
|
||||||
){rawledgertext=rawtext}
|
){rawledgertext=rawtext}
|
||||||
|
|
||||||
-- | Expand ~ in a file path (does not handle ~name).
|
-- -- | Expand ~ in a file path (does not handle ~name).
|
||||||
tildeExpand :: FilePath -> IO FilePath
|
-- tildeExpand :: FilePath -> IO FilePath
|
||||||
tildeExpand ('~':[]) = getHomeDirectory
|
-- tildeExpand ('~':[]) = getHomeDirectory
|
||||||
tildeExpand ('~':'/':xs) = getHomeDirectory >>= return . (++ ('/':xs))
|
-- tildeExpand ('~':'/':xs) = getHomeDirectory >>= return . (++ ('/':xs))
|
||||||
--handle ~name, requires -fvia-C or ghc 6.8:
|
-- --handle ~name, requires -fvia-C or ghc 6.8:
|
||||||
--import System.Posix.User
|
-- --import System.Posix.User
|
||||||
-- tildeExpand ('~':xs) = do let (user, path) = span (/= '/') xs
|
-- -- tildeExpand ('~':xs) = do let (user, path) = span (/= '/') xs
|
||||||
-- pw <- getUserEntryForName user
|
-- -- pw <- getUserEntryForName user
|
||||||
-- return (homeDirectory pw ++ path)
|
-- -- return (homeDirectory pw ++ path)
|
||||||
tildeExpand xs = return xs
|
-- tildeExpand xs = return xs
|
||||||
|
|
||||||
|
|||||||
@ -12,8 +12,8 @@ import Text.Printf
|
|||||||
import Text.RegexPR (gsubRegexPRBy)
|
import Text.RegexPR (gsubRegexPRBy)
|
||||||
import Data.Char (toLower)
|
import Data.Char (toLower)
|
||||||
import Ledger.IO (IOArgs,
|
import Ledger.IO (IOArgs,
|
||||||
ledgerenvvar,ledgerdefaultpath,myLedgerPath,
|
ledgerenvvar,myLedgerPath,
|
||||||
timelogenvvar,timelogdefaultpath,myTimelogPath)
|
timelogenvvar,myTimelogPath)
|
||||||
import Ledger.Parse
|
import Ledger.Parse
|
||||||
import Ledger.Utils
|
import Ledger.Utils
|
||||||
import Ledger.Types
|
import Ledger.Types
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user