gntools.parsers module¶
This module contains classes that help parse arguments for GEONIS menu or form-based Python scripts.
-
class
gntools.parsers.
FormArgParser
(*param_names)¶ Bases:
gntools.parsers._BaseArgParser
Data class that reads and stores the Python arguments for GEONIS form scripts. Simply instantiate this class in a form-based Python script to get easy access to all arguments passed to it.
Script argument values are cleaned before they are returned, which means that excessive single or double quotes will be removed. Furthermore, any “#” values (representing NoData placeholders) are replaced by
None
and trailing NoData values are removed.Using the GEONIS form definition (XML), a user can pass additional parameters to the script. These parameters do not have a name, but the
FormArgParser
can name them for you if it is initialized with one or more parameter names, so you can access them as a namedtuple.Example:
>>> params = FormArgParser('arg1', 'arg2') >>> params.arguments.arg1 'This value has been set in the GEONIS form XML' >>> params.arguments.arg2 'This is another argument'
>>> # Note that you can still get the arguments by index or unpack as a tuple >>> params.arguments[1] 'This is another argument' >>> params.arguments == 'This value has been set in the GEONIS form XML', 'This is another argument' True
If you don’t specify any parameter names, the
arguments
property returns a regulartuple
.Params:
param_names:
Optional parameter names to set on the parsed form arguments.
-
arguments
¶ Returns the arguments passed to the script (if defined in the GEONIS XML script configuration).
Returns: A namedtuple or tuple with additional script arguments (if any).
-
field_value
¶ The value of the table key field name for the feature/row to which the form script applies.
Return type: str
-
key_field
¶ Field name that holds the key value (ID) for the feature/row to which the form script applies.
Return type: str
-
project_vars
¶ Any optional GEONIS project variables passed to the script (often not used).
Return type: dict
-
script
¶ The full path to the script that was called by the Python interpreter.
Return type: str
-
table
¶ Table or feature class name to which the form script applies.
Return type: str
-
workspace
¶ Returns a
gpf.paths.Workspace
instance for the Esri workspace (and optionally a qualifier) specified in the script arguments.Return type: gpf.paths.Workspace
-
class
gntools.parsers.
MenuArgParser
(*param_names)¶ Bases:
gntools.parsers._BaseArgParser
Data class that reads and stores the Python arguments for GEONIS menu scripts. Simply instantiate this class in a menu-based Python script to get easy access to all arguments passed to it.
Script argument values are cleaned before they are returned, which means that excessive single or double quotes will be removed. Furthermore, any “#” values (representing NoData placeholders) are replaced by
None
and trailing NoData values are removed.Using the GEONIS menu definition (XML), a user can pass additional parameters to the script. These parameters do not have a name, but the
MenuArgParser
can name them for you if it is initialized with one or more parameter names, so you can access them as a namedtuple.Example:
>>> params = MenuArgParser('arg1', 'arg2') >>> params.arguments.arg1 'This value has been set in the GEONIS menu XML' >>> params.arguments.arg2 'This is another argument'
>>> # Note that you can still get the arguments by index or unpack as a tuple >>> params.arguments[1] 'This is another argument' >>> params.arguments == 'This value has been set in the GEONIS menu XML', 'This is another argument' True
If you don’t specify any parameter names, the
arguments
property returns a regulartuple
.Params:
param_names:
Optional parameter names to set on the parsed menu arguments.
-
arguments
¶ Returns the arguments passed to the script (if defined in the GEONIS XML script configuration).
Returns: A namedtuple or tuple with additional script arguments (if any).
-
project_vars
¶ Any optional GEONIS project variables passed to the script (often not used).
Return type: dict
-
script
¶ The full path to the script that was called by the Python interpreter.
Return type: str
-
workspace
¶ Returns a
gpf.paths.Workspace
instance for the Esri workspace (and optionally a qualifier) specified in the script arguments.Return type: gpf.paths.Workspace
-
exception
gntools.parsers.
ParameterWarning
¶ Bases:
exceptions.UserWarning
Warning that is displayed when a (minor) issue occurred while parsing the script parameters.
-
args
¶
-
message
¶
-
-
gntools.parsers.
clean_arg
(value, default)¶ Strips all trailing quotes and NoData (#) values from text.
Parameters: - value – The argument value that should be evaluated. If this is not a string, it will be passed as-is.
- default – The default value that should be returned if the value is a NoData string (#).
-
gntools.parsers.
eval_arg
(value, default)¶ Evaluates a literal string as a Python object in a safe manner.
Parameters: - value (str, unicode) – The string that should be evaluated. If it is not a string, the default value is returned.
- default – The default value to return if evaluation failed.
Raises: TypeError – When the evaluated object does not have the same type as the default value. This error can only be raised when default is not
None
.