Class PyArgParser
java.lang.Object
com.inductiveautomation.ignition.common.script.PyArgParser
A utility class for converting Jython's keyword argument signature (
PyObject[], String[]
) into more useful
Java base classes.
Argument names specified in camelCase in Java code will be accepted in either camelCase or snake_case from Jython.-
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(String keyword) Retrieve the argument specified bykeyword
as a plain list.Retrieve the argument specified bykeyword
as a typed list ofclazz
.Retrieve the argument specified bykeyword
as an untyped map.Retrieve the argument specified bykeyword
as a typed map.getBoolean
(String keyword) Getskeyword
, if and only if it was supplied as a boolean or number.Getskeyword
, if and only if it was supplied as a Date.Getskeyword
, if and only if it was supplied as a double.Getskeyword
, if and only if it was supplied as a float.<T> Optional<T>
getIndexed
(int index, Class<T> clazz) Retrieve the positional argument (type-safe) atindex
or throw a TypeError.getIndexedAsList
(int index, Class<T> clazz) Retrieve the positional argument atindex
as a typed list ofclazz
.getInteger
(String keyword) Getskeyword
, if and only if it was supplied as an integer.Getskeyword
, if and only if it was supplied as a long.getNullableBoolean
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Boolean or number.getNullableDate
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Date.getNullableDouble
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Double.getNullableFloat
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Float.getNullableInteger
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Integer.getNullableLong
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a Long.getNullableString
(String keyword) Retrieve argumentkeyword
, if and only if it was supplied as a String.<T> T
getNullSafe
(String keyword, Class<T> clazz) Optional<org.python.core.PyObject>
getPyObject
(int index) Gets positional argument atindex
, directly as a PyObject; i.e., without doing any casting.Optional<org.python.core.PyObject>
getPyObject
(String keyword) Getskeyword
, directly as a PyObject; i.e., without doing any casting.Getskeyword
, if and only if it was supplied as a String.<T> Optional<T>
Ifkeyword
was supplied, return it (type-safe) or throw a TypeError.static PyArgParser
parseArgs
(org.python.core.PyObject[] args, String[] keywords, Class<?> callingClass, String methodName) Parses the provided arguments and keywords into a PyArgParser instance, which can then be used to retrieve type-safe arguments.static PyArgParser
parseArgs
(org.python.core.PyObject[] args, String[] keywords, String[] expectedKeywords, Class<?>[] expectedTypes, String functionName) Parses the provided arguments and keywords into a PyArgParser instance, which can then be used to retrieve type-safe arguments.boolean
requireBoolean
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a boolean or number.requireDate
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a Date.double
requireDouble
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a double.float
requireFloat
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a float.int
requireInteger
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as an integer.requireList
(String keyword) Retrieve required argumentkeyword
if and only if it was supplied as a list.<T> List<T>
requireList
(String keyword, Class<T> clazz) Retrieve required argumentkeyword
if and only if it was supplied as a list ofclazz
.long
requireLong
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a long.org.python.core.PyObject
requirePyObject
(String keyword) Retrievekeyword
, directly as a PyObject; i.e., without doing any casting.requireString
(String keyword) Retrieve required argumentkeyword
, if and only if it was supplied as a String.<T> T
requireTyped
(String keyword, Class<T> clazz) Retrieve required argumentkeyword
, if and only if the supplied value can be coerced toclazz
.
-
Method Details
-
parseArgs
public static PyArgParser parseArgs(org.python.core.PyObject[] args, String[] keywords, String[] expectedKeywords, Class<?>[] expectedTypes, String functionName) Parses the provided arguments and keywords into a PyArgParser instance, which can then be used to retrieve type-safe arguments. The expected keywords and types are provided as arguments.- Parameters:
args
- args from a function callkeywords
- keyword names from a function callexpectedKeywords
- expected keyword argumentsexpectedTypes
- expected argument typesfunctionName
- the name of the function; used for better contextual error messages- Returns:
- a PyArgParser instance
- Throws:
AssertionError
- ifkeywords
is shorter thanargs
, or the list of expected keywords doesn't match the list of expected types.
-
parseArgs
public static PyArgParser parseArgs(org.python.core.PyObject[] args, String[] keywords, Class<?> callingClass, String methodName) Parses the provided arguments and keywords into a PyArgParser instance, which can then be used to retrieve type-safe arguments. The expected keywords and types are retrieved by looking for a method on the supplied class with theKeywordArgs
annotation.- Parameters:
args
- args from a function callkeywords
- keyword names from a function callcallingClass
- the class the method is contained in.methodName
- the name of the method. Used to look up annotations reflectively, and for thrown errors.- Returns:
- a PyArgParser instance
- Throws:
IllegalArgumentException
- if the provided method cannot be found, or is missing the KeywordArgs annotation.AssertionError
- ifkeywords
is shorter thanargs
, or the list of expected keywords doesn't match the list of expected types.org.python.core.PyException
- TypeError if more arguments are provided than expected argument names- See Also:
-
getIndexed
Retrieve the positional argument (type-safe) atindex
or throw a TypeError. If the index is out of range, returns an empty optional.- Throws:
org.python.core.PyException
- TypeError if the supplied value cannot be coerced toclazz
.
-
getTyped
Ifkeyword
was supplied, return it (type-safe) or throw a TypeError. If not specified or None was supplied, returns an empty optional.- Throws:
org.python.core.PyException
- TypeError if the supplied value cannot be coerced toclazz
.IllegalArgumentException
- if the provided keyword is not an expected argument for the function
-
requireTyped
Retrieve required argumentkeyword
, if and only if the supplied value can be coerced toclazz
.- Returns:
- the retrieved value of
keyword
coerced toclazz
. - Throws:
org.python.core.PyException
- TypeError if the supplied value cannot be coerced toclazz
.IllegalArgumentException
- if the provided keyword is not an expected argument for the function.
-
getNullSafe
-
getBoolean
Getskeyword
, if and only if it was supplied as a boolean or number.- Returns:
- the boolean value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a boolean.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireBoolean
Retrieve required argumentkeyword
, if and only if it was supplied as a boolean or number.- Returns:
- the boolean value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a boolean, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableBoolean
Retrieve argumentkeyword
, if and only if it was supplied as a Boolean or number. UnlikerequireBoolean(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Boolean value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a BooleanIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getInteger
Getskeyword
, if and only if it was supplied as an integer.- Returns:
- the integer value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a integer.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireInteger
Retrieve required argumentkeyword
, if and only if it was supplied as an integer.- Returns:
- the integer value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a integer, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableInteger
Retrieve argumentkeyword
, if and only if it was supplied as a Integer. UnlikerequireInteger(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Integer value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a IntegerIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getLong
Getskeyword
, if and only if it was supplied as a long.- Returns:
- the long value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a long.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireLong
Retrieve required argumentkeyword
, if and only if it was supplied as a long.- Returns:
- the long value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a long, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableLong
Retrieve argumentkeyword
, if and only if it was supplied as a Long. UnlikerequireLong(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Long value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a LongIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getFloat
Getskeyword
, if and only if it was supplied as a float.- Returns:
- the float value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a float.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireFloat
Retrieve required argumentkeyword
, if and only if it was supplied as a float.- Returns:
- the float value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a float, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableFloat
Retrieve argumentkeyword
, if and only if it was supplied as a Float. UnlikerequireFloat(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Float value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a FloatIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getDouble
Getskeyword
, if and only if it was supplied as a double.- Returns:
- the double value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a double.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireDouble
Retrieve required argumentkeyword
, if and only if it was supplied as a double.- Returns:
- the double value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a double, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableDouble
Retrieve argumentkeyword
, if and only if it was supplied as a Double. UnlikerequireDouble(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Double value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a DoubleIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getDate
Getskeyword
, if and only if it was supplied as a Date.- Returns:
- the Date value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a Date.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireDate
Retrieve required argumentkeyword
, if and only if it was supplied as a Date.- Returns:
- the Date value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a Date, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableDate
Retrieve argumentkeyword
, if and only if it was supplied as a Date. UnlikerequireDate(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the Date value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a DateIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getString
Getskeyword
, if and only if it was supplied as a String.- Returns:
- the String value of
keyword
, if supplied, or an empty optional. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a String.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requireString
Retrieve required argumentkeyword
, if and only if it was supplied as a String.- Returns:
- the String value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not a String, or was not specified.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getNullableString
Retrieve argumentkeyword
, if and only if it was supplied as a String. UnlikerequireString(String)
, will accept a supplied value ofPyNone
and return null.- Returns:
- the String value of
keyword
, or null. - Throws:
org.python.core.PyException
- TypeError if the supplied value was not a StringIllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
getPyObject
Gets positional argument atindex
, directly as a PyObject; i.e., without doing any casting.- Returns:
- the PyObject value at
index
- Throws:
IllegalArgumentException
- if the provided index is negative
-
getPyObject
Getskeyword
, directly as a PyObject; i.e., without doing any casting.- Returns:
- the PyObject value of
keyword
, if supplied, or an empty optional. Can bePy.None
if None was supplied. - Throws:
IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
requirePyObject
Retrievekeyword
, directly as a PyObject; i.e., without doing any casting.- Returns:
- the PyObject value of
keyword
- Throws:
org.python.core.PyException
- TypeError if the supplied value was not provided.IllegalArgumentException
- if the provided keyword is not an expected argument for the function- See Also:
-
containsKey
-
getIndexedAsList
Retrieve the positional argument atindex
as a typed list ofclazz
. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if any elements of the PyList supplied as an argument can't be coerced to
clazz
- TypeError if the supplied PyObject is not iterable
- TypeError if any elements of the PyList supplied as an argument can't be coerced to
-
getAsList
Retrieve the argument specified bykeyword
as a typed list ofclazz
. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if any elements of the PyList supplied as an argument can't be coerced to
clazz
- TypeError if the supplied PyObject is not iterable
- TypeError if any elements of the PyList supplied as an argument can't be coerced to
-
requireList
Retrieve required argumentkeyword
if and only if it was supplied as a list ofclazz
. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if any elements of the PyList supplied as an argument can't be coerced to
clazz
- TypeError if the supplied PyObject is not iterable
- TypeError if any elements of the PyList supplied as an argument can't be coerced to
IllegalArgumentException
- if the provided keyword is not an expected argument for the function
-
getAsList
Retrieve the argument specified bykeyword
as a plain list. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if the supplied PyObject is not iterable
-
requireList
Retrieve required argumentkeyword
if and only if it was supplied as a list. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if the supplied PyObject is not iterable
IllegalArgumentException
- if the provided keyword is not an expected argument for the function
-
getAsMap
Retrieve the argument specified bykeyword
as a typed map. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if any elements of the PyDictionary supplied as an argument can't be coerced to the right class
- TypeError if the supplied PyObject is not iterable
-
getAsMap
Retrieve the argument specified bykeyword
as an untyped map. Will be a shallow copy.- Throws:
org.python.core.PyException
-- TypeError if the supplied PyObject is not iterable
-