summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Filter/Pyplot/Scripting.hs
diff options
context:
space:
mode:
authorLaurentRDC <>2019-11-05 13:36:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-11-05 13:36:00 (GMT)
commit0896c8b21460ee840bb016aa81192720dfcc1eb4 (patch)
treed6f08c8cf271d24eb3c3f775db6282561cbfc124 /src/Text/Pandoc/Filter/Pyplot/Scripting.hs
parent2ae5738bae724386a9710042cfc7b0e4939dcfc1 (diff)
version 2.2.0.02.2.0.0
Diffstat (limited to 'src/Text/Pandoc/Filter/Pyplot/Scripting.hs')
-rw-r--r--src/Text/Pandoc/Filter/Pyplot/Scripting.hs75
1 files changed, 42 insertions, 33 deletions
diff --git a/src/Text/Pandoc/Filter/Pyplot/Scripting.hs b/src/Text/Pandoc/Filter/Pyplot/Scripting.hs
index 0ed4642..15d251c 100644
--- a/src/Text/Pandoc/Filter/Pyplot/Scripting.hs
+++ b/src/Text/Pandoc/Filter/Pyplot/Scripting.hs
@@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-|
-Module : Text.Pandoc.Filter.Pyplot.Scripting
+Module : $header$
Copyright : (c) Laurent P René de Cotret, 2019
License : MIT
Maintainer : laurent.decotret@outlook.com
@@ -16,58 +16,63 @@ module Text.Pandoc.Filter.Pyplot.Scripting
, runScriptIfNecessary
) where
-import Data.Hashable (hash)
-import Data.List (intersperse)
-import Data.Monoid (Any(..), (<>))
-import qualified Data.Text as T
-import qualified Data.Text.IO as T
+import Control.Monad.IO.Class
+import Control.Monad.Reader.Class
-import System.Directory (createDirectoryIfMissing,
- doesFileExist)
-import System.Exit (ExitCode (..))
-import System.FilePath ((</>), takeDirectory)
-import System.IO.Temp (getCanonicalTemporaryDirectory)
-import System.Process.Typed (runProcess, shell)
+import Data.Hashable (hash)
+import Data.List (intersperse)
+import Data.Monoid (Any (..), (<>))
+import qualified Data.Text as T
+import qualified Data.Text.IO as T
+
+import System.Directory (createDirectoryIfMissing,
+ doesFileExist)
+import System.Exit (ExitCode (..))
+import System.FilePath (takeDirectory, (</>))
+import System.IO.Temp (getCanonicalTemporaryDirectory)
+import System.Process.Typed (runProcess, shell)
-import Text.Pandoc.Filter.Pyplot.Types
import Text.Pandoc.Filter.Pyplot.FigureSpec
+import Text.Pandoc.Filter.Pyplot.Types
-- | Detect the presence of a blocking show call, for example "plt.show()"
checkBlockingShowCall :: PythonScript -> CheckResult
-checkBlockingShowCall script' =
- if hasShowCall
+checkBlockingShowCall script' =
+ if hasShowCall
then CheckFailed "The script has a blocking call to `matplotlib.pyplot.show`. "
- else CheckPassed
+ else CheckPassed
where
scriptLines = T.lines script'
hasShowCall = getAny $ mconcat $ Any <$>
[ "plt.show()" `elem` scriptLines
, "pyplot.show()" `elem` scriptLines
, "matplotlib.pyplot.show()" `elem` scriptLines
+ , "fig.show()" `elem` scriptLines
]
+
-- | List of all script checks
-- This might be overkill right now but extension to other languages will be easier
scriptChecks :: [PythonScript -> CheckResult]
scriptChecks = [checkBlockingShowCall]
+
-- | Take a python script in string form, write it in a temporary directory,
-- then execute it.
-runTempPythonScript :: String -- ^ Interpreter (e.g. "python" or "python35")
- -> [String] -- ^ Command-line flags
- -> PythonScript -- ^ Content of the script
- -> IO ScriptResult -- ^ Result.
-runTempPythonScript interpreter' flags' script' = case checkResult of
+runTempPythonScript :: PythonScript -- ^ Content of the script
+ -> PyplotM ScriptResult -- ^ Result.
+runTempPythonScript script' = case checkResult of
CheckFailed msg -> return $ ScriptChecksFailed msg
CheckPassed -> do
-- We involve the script hash as a temporary filename
-- so that there is never any collision
- scriptPath <- (</> hashedPath) <$> getCanonicalTemporaryDirectory
- T.writeFile scriptPath script'
-
+ scriptPath <- liftIO $ (</> hashedPath) <$> getCanonicalTemporaryDirectory
+ liftIO $ T.writeFile scriptPath script'
+ interpreter' <- asks interpreter
+ flags' <- asks flags
let command = mconcat . intersperse " " $ [interpreter'] <> flags' <> [show scriptPath]
- ec <- runProcess . shell $ command
+ ec <- liftIO $ runProcess . shell $ command
case ec of
ExitSuccess -> return ScriptSuccess
ExitFailure code -> return $ ScriptFailure code
@@ -75,21 +80,25 @@ runTempPythonScript interpreter' flags' script' = case checkResult of
checkResult = mconcat $ scriptChecks <*> [script']
hashedPath = show . hash $ script'
+
-- | Run the Python script. In case the file already exists, we can safely assume
-- there is no need to re-run it.
-runScriptIfNecessary :: Configuration -> FigureSpec -> IO ScriptResult
-runScriptIfNecessary config spec = do
- createDirectoryIfMissing True . takeDirectory $ figurePath spec
+runScriptIfNecessary :: FigureSpec
+ -> PyplotM ScriptResult
+runScriptIfNecessary spec = do
+ liftIO $ createDirectoryIfMissing True . takeDirectory $ figurePath spec
- fileAlreadyExists <- doesFileExist $ figurePath spec
+ fileAlreadyExists <- liftIO . doesFileExist $ figurePath spec
result <- if fileAlreadyExists
then return ScriptSuccess
- else runTempPythonScript (interpreter config) (flags config) scriptWithCapture
-
+ else runTempPythonScript scriptWithCapture
+
case result of
- ScriptSuccess -> T.writeFile (sourceCodePath spec) (script spec) >> return ScriptSuccess
+ ScriptSuccess -> liftIO $ T.writeFile (sourceCodePath spec) (script spec) >> return ScriptSuccess
ScriptFailure code -> return $ ScriptFailure code
ScriptChecksFailed msg -> return $ ScriptChecksFailed msg
where
- scriptWithCapture = addPlotCapture spec \ No newline at end of file
+ scriptWithCapture = addPlotCapture spec
+
+