Skip to content

Commit

Permalink
ENH Make write() return the filename used
Browse files Browse the repository at this point in the history
Also, internally refactor the code by adding a __builtin__ module as in
Python, thus making the code more regular (there is no special
`builtinFunctions` list anymore, just a special module that gets handled
like all the other modules)
  • Loading branch information
luispedro committed Mar 19, 2021
1 parent c4b0b23 commit 66a08e5
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 23 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Unreleased
* write() returns the filename used

Version 1.3.0 2021-01-28 by luispedro
* Validate count() headers on --validate-only
* Better error message if the user attempts to use the non-existent <\>
Expand Down
6 changes: 4 additions & 2 deletions Execs/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import Utils.Batch (getNcpus)
import Utils.Suggestion
import CWL (writeCWL)

import BuiltinFunctions (builtinModule)
import qualified BuiltinModules.Argv as ModArgv
import qualified BuiltinModules.Assemble as ModAssemble
import qualified BuiltinModules.Checks as Checks
Expand Down Expand Up @@ -144,7 +145,7 @@ runNGLessIO context (NGLessIO act) = try (runResourceT act) >>= \case
exitFailure
Right v -> return v

-- | Load both automatically imported modules are user-requested one
-- | Load both automatically imported modules and user-requested ones
loadModules :: NGLVersion -> [ModInfo] -> NGLessIO [Module]
loadModules av mods = do
mA <- ModAsReads.loadModule ("" :: T.Text)
Expand All @@ -157,7 +158,8 @@ loadModules av mods = do
mStats <- ModQCStats.loadModule ("" :: T.Text)
mOrfFind <- ModORFFind.loadModule ("0.6" :: T.Text)
imported <- loadStdlibModules mods
let loaded = [mOrfFind | av >= NGLVersion 0 6]
let loaded = [builtinModule av]
++ [mOrfFind | av >= NGLVersion 0 6]
++ [mLoadDirectory | av >= NGLVersion 1 2]
++ [mReadlines, mArgv, mAssemble, mA, mChecks, mRemove, mStats] ++ imported
forM_ loaded registerModule
Expand Down
23 changes: 18 additions & 5 deletions NGLess/BuiltinFunctions.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{- Copyright 2013-2020 NGLess Authors
{- Copyright 2013-2021 NGLess Authors
- License: MIT
-}
module BuiltinFunctions
( MethodName(..)
, MethodInfo(..)
, builtinFunctions
, builtinModule
, builtinMethods
, findFunction
) where

import Data.List (find)
import Data.Default (def)
import qualified Data.Text as T

import NGLess.NGLEnvironment (NGLVersion(..))
import Modules
import Language

Expand All @@ -24,9 +28,17 @@ data MethodInfo = MethodInfo
} deriving (Eq, Show)

findFunction :: [Module] -> FuncName -> Maybe Function
findFunction mods fn = find ((==fn) . funcName) $ builtinFunctions ++ concat (modFunctions <$> mods)
-- findFunction mods fn = trace (show mods) $ trace (show fn) $ find ((==fn) . funcName) $ concat (modFunctions <$> mods)
findFunction mods fn = find ((==fn) . funcName) $ concat (modFunctions <$> mods)

builtinModule :: NGLVersion -> Module
builtinModule ver@(NGLVersion majV minV) = def
{ modInfo = ModInfo "__builtin__" (T.pack $ show majV ++ "." ++ show minV)
, modPath = ""
, modFunctions = builtinFunctions ver
}

builtinFunctions =
builtinFunctions ver =
[Function (FuncName "fastq") (Just NGLString) [ArgCheckFileReadable] NGLReadSet fastqArgs False []
,Function (FuncName "paired") (Just NGLString) [ArgCheckFileReadable] NGLReadSet pairedArgs False []
,Function (FuncName "group") (Just (NGList NGLReadSet)) [] NGLReadSet groupArgs False []
Expand All @@ -42,7 +54,7 @@ builtinFunctions =
,Function (FuncName "count") (Just NGLMappedReadSet) [] NGLCounts countArgs False [FunctionCheckReturnAssigned]
,Function (FuncName "__check_count") (Just NGLMappedReadSet) [] NGLCounts countCheckArgs False []
,Function (FuncName "countfile") (Just NGLString) [ArgCheckFileReadable] NGLCounts [] False [FunctionCheckReturnAssigned]
,Function (FuncName "write") (Just NGLAny) [] NGLVoid writeArgs False []
,Function (FuncName "write") (Just NGLAny) [] (if ver >= NGLVersion 1 4 then NGLString else NGLVoid) writeArgs False []

,Function (FuncName "print") (Just NGLAny) [] NGLVoid [] False []

Expand Down Expand Up @@ -151,6 +163,7 @@ smoothtrimArgs =
,ArgInformation "window" True NGLInteger []
]

builtinMethods :: [MethodInfo]
builtinMethods =
[
-- NGLMappedReadSet
Expand Down
2 changes: 1 addition & 1 deletion NGLess/Transform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Output (outputListLno', OutputType(..))
import NGLess
import Utils.Utils (uniq, secondM)
import NGLess.NGLEnvironment
import BuiltinFunctions
import BuiltinFunctions (findFunction)


{-| NOTE
Expand Down
4 changes: 1 addition & 3 deletions NGLess/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,9 @@ checkindexable expr = do
errorInLineC ["List expected. Type ", show e , " provided."]
return $ Just NGLVoid

allFunctions = (builtinFunctions ++) <$> moduleFunctions
moduleFunctions = asks (concatMap modFunctions)

funcInfo fn = do
fs <- allFunctions
fs <- asks (concatMap modFunctions)
let matched = filter ((==fn) . funcName) fs
case matched of
[fi] -> return fi
Expand Down
2 changes: 1 addition & 1 deletion NGLess/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Data.Foldable (asum)
import Language
import Modules
import NGLess.NGError
import BuiltinFunctions
import BuiltinFunctions (MethodInfo(..), builtinMethods, findFunction)
import Utils.Suggestion


Expand Down
2 changes: 1 addition & 1 deletion NGLess/ValidationIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import NGLess
import Output
import Modules
import Language
import BuiltinFunctions
import BuiltinFunctions (findFunction)
import FileManagement
import Utils.Suggestion
import ReferenceDatabases
Expand Down
15 changes: 10 additions & 5 deletions Tests-Src/Tests/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ import qualified Data.Text as T
import Tests.Utils
import Types
import Language
import BuiltinFunctions (builtinModule)
import NGLess.NGLEnvironment (NGLVersion(..))

tgroup_Types = $(testGroupGenerator)


mods = [builtinModule (NGLVersion 1 3)]

isOkTypes :: Script -> IO ()
isOkTypes script = case checktypes [] script of
isOkTypes script = case checktypes mods script of
(Right _) -> return ()
(Left err) -> assertFailure ("Type error on good code (error was '"++show err++"'")

isOkTypesText scriptText = case parsetest scriptText >>= checktypes [] of
isOkTypesText scriptText = case parsetest scriptText >>= checktypes mods of
(Right _) -> return ()
(Left err) -> assertFailure ("Type error on good code (error was '"++show err++"') for script: '"++T.unpack scriptText++"'")

isErrorText scriptText = isError $ parsetest scriptText>>= checktypes []
isErrorText scriptText = isError $ parsetest scriptText>>= checktypes mods


case_bad_type_fastq = isError $ checktypes [] (Script Nothing [(0,FunctionCall (FuncName "fastq") (ConstInt 3) [] Nothing)])
case_bad_type_fastq = isError $ checktypes mods (Script Nothing [(0,FunctionCall (FuncName "fastq") (ConstInt 3) [] Nothing)])
case_good_type_fastq = isOkTypes (Script Nothing [(0,FunctionCall (FuncName "fastq") (ConstStr "fastq.fq") [] Nothing)])

case_type_complete = isOkTypesText
Expand All @@ -49,7 +54,7 @@ case_indent_empty_line = isOkTypesText
\ discard\n"


case_invalid_func_fastq = isError $ checktypes [] $
case_invalid_func_fastq = isError $ checktypes mods $
Script Nothing [(0,FunctionCall (FuncName "fastq") (ConstStr "fastq.fq") [(Variable "fname", ConstInt 10)] Nothing)]

-- unique
Expand Down
12 changes: 8 additions & 4 deletions Tests-Src/Tests/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import Tests.Utils
import Validation
import ValidationIO
import Utils.Here
import BuiltinFunctions (builtinModule)
import NGLess.NGLEnvironment (NGLVersion(..))

tgroup_Validation = $(testGroupGenerator)

-- Pure Validation

isValidateOk ftext = case parsetest ftext >>= validate [] of
mods = [builtinModule (NGLVersion 1 3)]

isValidateOk ftext = case parsetest ftext >>= validate mods of
Right _ -> return ()
Left err -> assertFailure ("Validation should have passed for script "++T.unpack ftext++"; instead picked up error: '"++show err++"'")
isValidateError ftext = isErrorMsg ("Validation should have picked error for script '"++T.unpack ftext++"'") (parsetest ftext >>= validate [])
isValidateError ftext = isErrorMsg ("Validation should have picked error for script '"++T.unpack ftext++"'") (parsetest ftext >>= validate mods)

case_bad_function_attr_count = isValidateError
"ngless '0.0'\n\
Expand All @@ -49,13 +53,13 @@ write(
-- Validate IO

validateIO_Ok script = do
err <- testNGLessIO $ validateIO [] (fromRight . parsetest $ script)
err <- testNGLessIO $ validateIO mods (fromRight . parsetest $ script)
case err of
Nothing -> assertBool "" True
Just errmsg -> assertFailure (concat ["Expected no errors in validation, got ", show errmsg, ".\nScript was::\n\n", show script])

validateIO_error script = do
err <- testNGLessIO $ validateIO [] (fromRight . parsetest $ script)
err <- testNGLessIO $ validateIO mods (fromRight . parsetest $ script)
case err of
Nothing -> assertFailure (concat ["ValidateIO should have detected an error on the script ", show script])
Just _ -> return ()
Expand Down
5 changes: 4 additions & 1 deletion docs/sources/Functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,10 @@ Any
Return:
~~~~~~~

Void
.. versionadded:: NGLess 1.4
Prior to version 1.4, ``write()`` returned nothing

String: the file name used

Arguments by value:
~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 66a08e5

Please sign in to comment.