Python Scripting

Automic Automation allows you to seamlessly integrate Python code into your Automic Automation system, thus allowing you to leverage Python language to solve automation challenges. This topic explains how to use Python scripting

This page includes the following:

Working with Automic Automation Variables Using Python Scripting

Automic Automation provides a special Python dictionary named _automic_variables that contains all Automic Automation variables available to the current Python Job at runtime (for example object variables, script variables, predefined variables and so on). This lets you access and process AE variables directly in Python instead of using the AE scripting language. For example, to access a specific value (the internal Job number) through the Python Job object (_automic_job), you can read it as an attribute using the following statement:

:print (_automic_job.uc_py_jobnr

You can also define your own variables in Python and attach them to _automic_job. For example, 

_automic_job.new_variable = 'new value'

creates a new job attribute that Automic Automation publishes back to the system when the Python Job finishes.

All variables provided through the Python integration, as well as any variables you add or update on _automic_job, are written back to Automic Automation after Job execution. They are then visible in the Job Report (REP) and can be reused by subsequent tasks, just like variables defined with standard AEscripting. See Job Reports.

Examples

The following script statement examples allow you to:

  • Assign a value to a variable named "foo" within the Automic Automation environment.

    :set &foo# = "some value"

  • Access the "foo" variable's value directly as an attribute of the Job object in Python.

    :print (_automic_job.foo)

  • Define arrays.

    For example, the following arrays in AE scripting language

    :DEFINE &xy#string, 5

    :set &xy#[1] = 10

    :set &xy#[2] = 20

    can be accessed as follows in Python:

    • To print the entire array

      print(_automic_job.xy)

    • To print just one element in position 1

      print(_automic_job.xy[1])

  • Assign a value to the test_value attribute of the Job object in Python. This value will be automatically transmitted to the Automic Automation once the Python Job is completed.

    _automic_job.test_value = "test01"

  • Remove the value associated with the 'foo' variable

    Note: This operation only removes the value; the "foo" key remains in the variable set.

    del _automic_job.foo

  • Iterate through all Automic Automation variables available in the Python dictionary (_automic_variables) included in the offering and print their key-value pairs:

    for keyvalues in _automic_variables.items():
        print(f"{key} : {values}")

Registering a Variable

Use the script statement automic_register_variable to register the name and the value of a variable in the AE system. You can do so in the Process pages of Python Jobs. The registered variables are stored as object variables, see Defining the Variables Page.

This is equivalent to the :REGISTER_VARIABLE script command in the AE scripting language, see :REGISTER_VARIABLE.

Syntax

automic_register_variable(Variable name, Variable value) to register a variable

or

automic_register_variable('test#', [1,2,3,4,5]) to register a variable with an array.

  • Variable name

    Name of the variable

    Format: Name of the variable without a leading ampersand (&) character. End with a hashtag (#).

  • Variable value

    Either the value of the relevant variable or, in the case of arrays, the list type.

    Format: String, list

Example

The following script statement allows you to register a new variable named "var_name_02" with the value "test02" in the Automic Automation system:

automic_register_variable ("var_name_02""test02")

Registering an Output File

Use the automic_register_outputfile Python script statement on the Process page of Python Jobs to register a file as an external Job output. The specified file must be stored on the agent's computer on which the Job is executed or must be accessible from there. It makes sense only to define files that are generated by the Job. After the Job has been executed, the file is listed in the Directory tab and the report dialog of the default Job output. There, you can open or store the file directly via the Automic Web Interface. For more information, see Registered Job Output.

Note: Using the Output page, you can also register external files as Job output. The difference lies in the registration time: The files of the Output page are registered immediately when the Job is executed, regardless of whether the Job could create the file. If you use this script statement, the specified file is only registered when it is called. For more information, see Registering External Output Files.

This is equivalent to the :REGISTER_OUTPUTFILE script command in the AE scripting language, see :REGISTER_OUTPUTFILE.

Syntax

automic_register_outputfile(File, User login)

Parameters

  • File

    Fully qualified path and name of the file that should be registered as a Job output. Wildcard characters are not allowed. You must always specify the absolute path.

    Format: script literal

  • User login

    Defines whether the user's login data should be used

    Allowed values: Y and N

    Format: script literal

Example

The following sample script allows you to register the file C:\temp\test.txt as an output file in the Automic Automation system:

automic_register_outputfile("C:\\temp\\test.txt""N")

The system then verifies whether the command could successfully be executed. If so, this file is registered as Job output. Otherwise, the Job aborts.

See also: