module App.Behaviours.FileOps where
import App.EventBus
import Control.Applicative
import qualified Data.ByteString.Char8 as SB
import qualified Data.ByteString.Lazy.Char8 as LB
import Data.Binary
import qualified Control.Exception as Ex
readFileBehaviour :: Read a => String -> EData a -> Behaviour [EData a]
readFileBehaviour n d b = consumeNamedEventsWith b n readFileCatch
where readFileCatch e = Ex.catch (readFile0 e) (emitException e)
readFile0 e = (rFile0 d . eventdata $ e) >>= produce n "ReadSource" (filename e) Persistent >>= return . (:[])
emitException e ex = produce "Exception" "readFileBehaviour" n once (EString (show ex):eventdata e) >>= return . (:[])
filename = (\(EString x) -> x) . head . eventdata
readFileBehaviourNR :: String -> EData a -> Behaviour [EData a]
readFileBehaviourNR n d b = consumeNamedEventsWith b n readFileCatch
where readFileCatch e = Ex.catch (readFile0 e) (emitException e)
readFile0 e = (rFile d . eventdata $ e) >>= produce n "ReadSource" (filename e) Persistent >>= return . (:[])
emitException e ex = produce "Exception" "readFileBehaviour" n once (EString (show ex):eventdata e) >>= return . (:[])
filename = (\(EString x) -> x) . head . eventdata
decodeFileBehaviour :: Binary a => String -> EData a -> Behaviour [EData a]
decodeFileBehaviour n d b = consumeNamedEventsWith b n decodeFileCatch
where decodeFileCatch e = Ex.catch (decodeFile0 e) (emitException e)
decodeFile0 e = (dFile0 d . eventdata $ e) >>= produce n "ReadSource" (filename e) Persistent >>= return . (:[])
emitException e ex = produce "Exception" "decodeFileBehaviour" n once (EString (show ex):eventdata e) >>= return . (:[])
filename = (\(EString x) -> x) . head . eventdata
decodeFileBehaviourNB :: String -> EData a -> Behaviour [EData a]
decodeFileBehaviourNB n d b = consumeNamedEventsWith b n decodeFileCatch
where decodeFileCatch e = Ex.catch (decodeFile0 e) (emitException e)
decodeFile0 e = (dFile d . eventdata $ e) >>= (produce n "ReadSource" (filename e) Persistent) >>= return . (:[])
emitException e ex = produce "Exception" "decodeFileBehaviourNB" n once (EString (show ex):eventdata e) >>= return . (:[])
filename = (\(EString x) -> x) . head . eventdata
writeFileBehaviourNS :: Behaviour [EData a]
writeFileBehaviourNS b = consumeNamedEventsWith b "WriteFile" $ \e -> Ex.catch
(wFile . eventdata $ e)
(\ex -> produce "Exception" "writeFileBehaviourNS" "WriteFile" once (EString (show ex):eventdata e) >>= return . (:[]))
writeFileBehaviour :: Show a => Behaviour [EData a]
writeFileBehaviour b = consumeNamedEventsWith b "WriteFile" $ \e -> Ex.catch
(wFile0 . eventdata $ e)
(\ex -> produce "Exception" "writeFileBehaviour" "WriteFile" once (EString (show ex):eventdata e) >>= return . (:[]))
encodeFileBehaviourNB :: Behaviour [EData a]
encodeFileBehaviourNB b = consumeNamedEventsWith b "WriteBinary" $ \e -> Ex.catch
(wBinary . eventdata $ e)
(\ex -> produce "Exception" "encodeFileBehaviourNB" "WriteFile" once (EString (show ex):eventdata e) >>= return . (:[]))
encodeFileBehaviour :: Binary a => Behaviour [EData a]
encodeFileBehaviour b = consumeNamedEventsWith b "WriteBinary" $ \e -> Ex.catch
(wBinary0 . eventdata $ e)
(\ex -> produce "Exception" "encodeFileBehaviour" "WriteFile" once (EString (show ex):eventdata e) >>= return . (:[]))
wFile [EString filepath, EString contents] = [] <$ writeFile filepath contents
wFile [EString filepath, EStringL contents] = [] <$ (writeFile filepath . unlines $ contents)
wFile [EString filepath, ELByteString contents] = [] <$ (LB.writeFile filepath contents)
wFile [EString filepath, ELByteStringL contents] = [] <$ (LB.writeFile filepath . LB.unlines $ contents)
wFile [EString filepath, EByteString contents] = [] <$ SB.writeFile filepath contents
wFile [EString filepath, EByteStringL contents] = [] <$ (SB.writeFile filepath . SB.unlines $ contents)
wFile [EString filepath, EInt contents] = [] <$ (writeFile filepath . show $ contents)
wFile [EString filepath, EIntL contents] = [] <$ (writeFile filepath . show $ contents)
wFile [EString filepath, EDouble contents] = [] <$ (writeFile filepath . show $ contents)
wFile [EString filepath, EDoubleL contents] = [] <$ (writeFile filepath . show $ contents)
wFile [EString filepath, EBool contents] = [] <$ (writeFile filepath . show $ contents)
wFile [EString filepath, EBoolL contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EString contents] = [] <$ writeFile filepath contents
wFile0 [EString filepath, EStringL contents] = [] <$ (writeFile filepath . unlines $ contents)
wFile0 [EString filepath, ELByteString contents] = [] <$ (LB.writeFile filepath contents)
wFile0 [EString filepath, ELByteStringL contents] = [] <$ (LB.writeFile filepath . LB.unlines $ contents)
wFile0 [EString filepath, EByteString contents] = [] <$ SB.writeFile filepath contents
wFile0 [EString filepath, EByteStringL contents] = [] <$ (SB.writeFile filepath . SB.unlines $ contents)
wFile0 [EString filepath, EInt contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EIntL contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EDouble contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EDoubleL contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EBool contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EBoolL contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EOther contents] = [] <$ (writeFile filepath . show $ contents)
wFile0 [EString filepath, EOtherL contents] = [] <$ (writeFile filepath . show $ contents)
wBinary [EString filepath, EString contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EStringL contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EByteString contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EByteStringL contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, ELByteString contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, ELByteStringL contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EInt contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EIntL contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EDouble contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EDoubleL contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EBool contents] = [] <$ (encodeFile filepath contents)
wBinary [EString filepath, EBoolL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EString contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EStringL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EByteString contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EByteStringL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, ELByteString contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, ELByteStringL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EInt contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EIntL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EDouble contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EDoubleL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EBool contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EBoolL contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EOther contents] = [] <$ (encodeFile filepath contents)
wBinary0 [EString filepath, EOtherL contents] = [] <$ (encodeFile filepath contents)
rFile (EString _) [EString filepath] = (:[]) . EString . LB.unpack <$> LB.readFile filepath
rFile (EStringL _) [EString filepath] = (:[]) . EStringL . read . LB.unpack <$> LB.readFile filepath
rFile (EByteString _) [EString filepath] = (:[]) . EByteString <$> SB.readFile filepath
rFile (EByteStringL _) [EString filepath] = (:[]) . EByteStringL . SB.lines <$> SB.readFile filepath
rFile (ELByteString _) [EString filepath] = (:[]) . ELByteString <$> LB.readFile filepath
rFile (ELByteStringL _) [EString filepath] = (:[]) . ELByteStringL . LB.lines <$> LB.readFile filepath
rFile (EInt _) [EString filepath] = (:[]) . EInt . read . LB.unpack <$> LB.readFile filepath
rFile (EIntL _) [EString filepath] = (:[]) . EIntL . read . LB.unpack <$> LB.readFile filepath
rFile (EDouble _) [EString filepath] = (:[]) . EDouble . read . LB.unpack <$> LB.readFile filepath
rFile (EDoubleL _) [EString filepath] = (:[]) . EDoubleL . read . LB.unpack <$> LB.readFile filepath
rFile (EBool _) [EString filepath] = (:[]) . EBool . read . LB.unpack <$> LB.readFile filepath
rFile (EBoolL _) [EString filepath] = (:[]) . EBoolL . read . LB.unpack <$> LB.readFile filepath
dFile (EString _) [EString filepath] = (:[]) .EString <$> decodeFile filepath
dFile (EStringL _) [EString filepath] = (:[]) .EStringL <$> decodeFile filepath
dFile (EByteString _) [EString filepath] = (:[]) .EByteString <$> decodeFile filepath
dFile (EByteStringL _) [EString filepath] = (:[]) .EByteStringL <$> decodeFile filepath
dFile (ELByteString _) [EString filepath] = (:[]) .ELByteString <$> decodeFile filepath
dFile (ELByteStringL _) [EString filepath] = (:[]) .ELByteStringL <$> decodeFile filepath
dFile (EInt _) [EString filepath] =(:[]) . EInt <$> decodeFile filepath
dFile (EIntL _) [EString filepath] =(:[]) . EIntL <$> decodeFile filepath
dFile (EDouble _) [EString filepath] =(:[]) . EDouble <$> decodeFile filepath
dFile (EDoubleL _) [EString filepath] =(:[]) . EDoubleL <$> decodeFile filepath
dFile (EBool _) [EString filepath] =(:[]) . EBool <$> decodeFile filepath
dFile (EBoolL _) [EString filepath] =(:[]) . EBoolL <$> decodeFile filepath
rFile0 (EString _) [EString filepath] =(:[]) . EString . LB.unpack <$> LB.readFile filepath
rFile0 (EStringL _) [EString filepath] =(:[]) . EStringL . read . LB.unpack <$> LB.readFile filepath
rFile0 (EByteString _) [EString filepath] =(:[]) . EByteString <$> SB.readFile filepath
rFile0 (EByteStringL _) [EString filepath] =(:[]) . EByteStringL . SB.lines <$> SB.readFile filepath
rFile0 (ELByteString _) [EString filepath] =(:[]) . ELByteString <$> LB.readFile filepath
rFile0 (ELByteStringL _) [EString filepath] =(:[]) . ELByteStringL . LB.lines <$> LB.readFile filepath
rFile0 (EInt _) [EString filepath] =(:[]) . EInt . read . LB.unpack <$> LB.readFile filepath
rFile0 (EIntL _) [EString filepath] =(:[]) . EIntL . read . LB.unpack <$> LB.readFile filepath
rFile0 (EDouble _) [EString filepath] = (:[]) . EDouble . read . LB.unpack <$> LB.readFile filepath
rFile0 (EDoubleL _) [EString filepath] = (:[]) . EDoubleL . read . LB.unpack <$> LB.readFile filepath
rFile0 (EBool _) [EString filepath] =(:[]) . EBool . read . LB.unpack <$> LB.readFile filepath
rFile0 (EBoolL _) [EString filepath] =(:[]) . EBoolL . read . LB.unpack <$> LB.readFile filepath
rFile0 (EOther _) [EString filepath] =(:[]) . EOther . read . LB.unpack <$> LB.readFile filepath
rFile0 (EOtherL _) [EString filepath] =(:[]) . EOtherL . read . LB.unpack <$> LB.readFile filepath
dFile0 (EString _) [EString filepath] = (:[]) . EString <$> decodeFile filepath
dFile0 (EStringL _) [EString filepath] =(:[]) . EStringL <$> decodeFile filepath
dFile0 (EByteString _) [EString filepath] =(:[]) . EByteString <$> decodeFile filepath
dFile0 (EByteStringL _) [EString filepath] = (:[]) . EByteStringL <$> decodeFile filepath
dFile0 (ELByteString _) [EString filepath] = (:[]) . ELByteString <$> decodeFile filepath
dFile0 (ELByteStringL _) [EString filepath] = (:[]) . ELByteStringL <$> decodeFile filepath
dFile0 (EInt _) [EString filepath] = (:[]) . EInt <$> decodeFile filepath
dFile0 (EIntL _) [EString filepath] = (:[]) . EIntL <$> decodeFile filepath
dFile0 (EDouble _) [EString filepath] = (:[]) . EDouble <$> decodeFile filepath
dFile0 (EDoubleL _) [EString filepath] = (:[]) . EDoubleL <$> decodeFile filepath
dFile0 (EBool _) [EString filepath] = (:[]) . EBool <$> decodeFile filepath
dFile0 (EBoolL _) [EString filepath] = (:[]) . EBoolL <$> decodeFile filepath
dFile0 (EOther _) [EString filepath] = (:[]) . EOther <$> decodeFile filepath
dFile0 (EOtherL _) [EString filepath] = (:[]) . EOtherL <$> decodeFile filepath