{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module      :  ELynx.Tools.InputOutput
-- Copyright   :  2021 Dominik Schrempf
-- License     :  GPL-3.0-or-later
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Creation date: Thu Feb 14 13:30:37 2019.
--
-- Tools involving input, output, and parsing.
module ELynx.Tools.InputOutput
  ( -- * Execution Mode
    ExecutionMode (..),
    openFileWithExecutionMode,

    -- * Input, output
    readGZFile,
    writeGZFile,

    -- * Parsing
    runParserOnFile,
    parseFileWith,
    parseIOWith,
    parseFileOrIOWith,
    parseStringWith,
    parseByteStringWith,
  )
where

import Codec.Compression.GZip
import Data.Aeson hiding (String)
import Data.Attoparsec.ByteString.Lazy hiding (Fail)
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.List (isSuffixOf)
import GHC.Generics
import System.Directory
import System.IO

-- | Overwrite existing output files or fail if output files exist.
data ExecutionMode = Overwrite | Fail
  deriving (ExecutionMode -> ExecutionMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ExecutionMode -> ExecutionMode -> Bool
$c/= :: ExecutionMode -> ExecutionMode -> Bool
== :: ExecutionMode -> ExecutionMode -> Bool
$c== :: ExecutionMode -> ExecutionMode -> Bool
Eq, Int -> ExecutionMode -> ShowS
[ExecutionMode] -> ShowS
ExecutionMode -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [ExecutionMode] -> ShowS
$cshowList :: [ExecutionMode] -> ShowS
show :: ExecutionMode -> [Char]
$cshow :: ExecutionMode -> [Char]
showsPrec :: Int -> ExecutionMode -> ShowS
$cshowsPrec :: Int -> ExecutionMode -> ShowS
Show, forall x. Rep ExecutionMode x -> ExecutionMode
forall x. ExecutionMode -> Rep ExecutionMode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ExecutionMode x -> ExecutionMode
$cfrom :: forall x. ExecutionMode -> Rep ExecutionMode x
Generic)

instance FromJSON ExecutionMode

instance ToJSON ExecutionMode

checkFile :: ExecutionMode -> FilePath -> IO ()
checkFile :: ExecutionMode -> [Char] -> IO ()
checkFile ExecutionMode
Overwrite [Char]
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkFile ExecutionMode
Fail [Char]
fp =
  [Char] -> IO Bool
doesFileExist [Char]
fp forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Bool
True ->
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
        [Char]
"File exists: "
          forall a. Semigroup a => a -> a -> a
<> [Char]
fp
          forall a. Semigroup a => a -> a -> a
<> [Char]
"."
          forall a. Semigroup a => a -> a -> a
<> [Char]
"\n"
          forall a. Semigroup a => a -> a -> a
<> [Char]
"Please use --force to overwrite results of a previous analysis."
    Bool
False -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Open existing files only if 'ExecutionMode' is 'Overwrite'.
openFileWithExecutionMode :: ExecutionMode -> FilePath -> IO Handle
openFileWithExecutionMode :: ExecutionMode -> [Char] -> IO Handle
openFileWithExecutionMode ExecutionMode
em [Char]
fp = ExecutionMode -> [Char] -> IO ()
checkFile ExecutionMode
em [Char]
fp forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> IOMode -> IO Handle
openFile [Char]
fp IOMode
WriteMode

-- | Read file. If file path ends with ".gz", assume gzipped file and decompress
-- before read.
readGZFile :: FilePath -> IO BL.ByteString
readGZFile :: [Char] -> IO ByteString
readGZFile [Char]
f
  | [Char]
".gz" forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` [Char]
f = ByteString -> ByteString
decompress forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO ByteString
BL.readFile [Char]
f
  | Bool
otherwise = [Char] -> IO ByteString
BL.readFile [Char]
f

-- | Write file. If file path ends with ".gz", assume gzipped file and compress
-- before write.
writeGZFile :: ExecutionMode -> FilePath -> BL.ByteString -> IO ()
writeGZFile :: ExecutionMode -> [Char] -> ByteString -> IO ()
writeGZFile ExecutionMode
frc [Char]
f ByteString
r
  | [Char]
".gz" forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` [Char]
f = ExecutionMode -> [Char] -> IO ()
checkFile ExecutionMode
frc [Char]
f forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> ByteString -> IO ()
BL.writeFile [Char]
f (ByteString -> ByteString
compress ByteString
r)
  | Bool
otherwise = ExecutionMode -> [Char] -> IO ()
checkFile ExecutionMode
frc [Char]
f forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> ByteString -> IO ()
BL.writeFile [Char]
f ByteString
r

-- | Parse a possibly gzipped file.
runParserOnFile :: Parser a -> FilePath -> IO (Either String a)
runParserOnFile :: forall a. Parser a -> [Char] -> IO (Either [Char] a)
runParserOnFile Parser a
p [Char]
f = forall r. Result r -> Either [Char] r
eitherResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> ByteString -> Result a
parse Parser a
p forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO ByteString
readGZFile [Char]
f

-- | Parse a possibly gzipped file and extract the result.
parseFileWith :: Parser a -> FilePath -> IO a
parseFileWith :: forall a. Parser a -> [Char] -> IO a
parseFileWith Parser a
p [Char]
f = forall a. Parser a -> Maybe [Char] -> IO a
parseFileOrIOWith Parser a
p (forall a. a -> Maybe a
Just [Char]
f)

-- | Parse standard input.
parseIOWith :: Parser a -> IO a
parseIOWith :: forall a. Parser a -> IO a
parseIOWith Parser a
p = forall a. Parser a -> Maybe [Char] -> IO a
parseFileOrIOWith Parser a
p forall a. Maybe a
Nothing

-- | Parse a possibly gzipped file, or standard input, and extract the result.
parseFileOrIOWith :: Parser a -> Maybe FilePath -> IO a
parseFileOrIOWith :: forall a. Parser a -> Maybe [Char] -> IO a
parseFileOrIOWith Parser a
p Maybe [Char]
mf = do
  ByteString
s <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe IO ByteString
BL.getContents [Char] -> IO ByteString
readGZFile Maybe [Char]
mf
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. Parser a -> ByteString -> a
parseByteStringWith Parser a
p ByteString
s

-- | Parse a 'String' and extract the result.
parseStringWith :: Parser a -> String -> a
parseStringWith :: forall a. Parser a -> [Char] -> a
parseStringWith Parser a
p [Char]
x = forall a. Parser a -> ByteString -> a
parseByteStringWith Parser a
p ([Char] -> ByteString
BL.pack [Char]
x)

-- | Parse a 'BL.ByteString' and extract the result.
parseByteStringWith :: Parser a -> BL.ByteString -> a
parseByteStringWith :: forall a. Parser a -> ByteString -> a
parseByteStringWith Parser a
p ByteString
x = case forall r. Result r -> Either [Char] r
eitherResult forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> ByteString -> Result a
parse Parser a
p ByteString
x of
  Left [Char]
err -> forall a. HasCallStack => [Char] -> a
error [Char]
err
  Right a
val -> a
val