script2py package

Submodules

script2py.nodes module

nodes.py

The nodes each represent a block in the script. The currently supported nodes are:

  • the base Node, which all nodes inherit from

  • the Line node, which stores a a line of dialogue from a speaker

  • the Choice node, which gives the player a choice and creates a branching path

  • the Setter node, which changes a system parameter

class script2py.nodes.Choice(choices: Optional[list] = None, **kwargs)

Bases: script2py.nodes.Node

Offers a choice to the player.

Script format:

*** SpeakerName: First Choice
    --> Branch1
*** SpeakerName: Second Choice

JSON format:

{
    "node_id": "0001",
    "type": "choice",
    "choices": [
        {
            "speaker": "SpeakerName",
            "choice": "First Choice",
            "next_id": "0002"
        },
        {
            "speaker": "SpeakerName",
            "choice": "Second Choice",
            "next_id": "0003"
        }
    ],
    "next_id": "0003"
}

If no branch is specified, the next node will be assumed to be the next node in the script.

Parameters
  • choices (list(dict)) – each element is a dictionary with keys “speaker”, “text”, and (optionally) “next_id”.

  • kwargs – keyword arguments passed to the base class Node.

node_type = 'choice'
to_dot()

Formats the node parameters for a dot graph.

Returns

the dot graph for this node.

Return type

str

to_json()

Formats the node paramters for a JSON file.

Returns

the JSON data for this node.

Return type

dict

class script2py.nodes.Line(speaker: str = '', text: str = '', **kwargs)

Bases: script2py.nodes.Node

Displays a line from the specified speaker.

Script format:

SpeakerName: Line of text.
SpeakerName: Another line of text. This one extends to
    more than one line.

JSON format:

{
    "node_id": "0001",
    "type": "line",
    "speaker": "SpeakerName",
    "text": "Line of text.",
    "next_id": "0002"
},
{
    "node_id": "0002",
    "type": "line",
    "speaker": "SpeakerName",
    "text": "Another line of text. This one extends to more than one line.",
    "next_id": null
}
Parameters
  • speaker (str) – name of the character speaking.

  • text (str) – the line of text the character is saying.

  • kwargs – keyword arguments passed to the base class Node.

node_type = 'line'
to_dot()

Formats the node parameters for a dot graph.

Returns

the dot graph for this node.

Return type

str

to_json()

Formats the node paramters for a JSON file.

Returns

the JSON data for this node.

Return type

dict

class script2py.nodes.Node(next_id: Optional[str] = None, section: str = '', next_section: Optional[str] = None, wrap: int = 80)

Bases: object

The base node class.

Each node has the following properties:

  • a node_id, which is created upon instantiation

  • a section, which groups nodes together

  • a next_section, which is only applicable for the last node in a section

Each inherited class should:

  • set the node_type

  • format its displayed text using the _clean() function

  • add to the to_dot() and to_json() functions with its additional parameters

Parameters
  • next_id (str) – the node id of the next node. The last node should be left as None.

  • section (str) – the name of the group this node is in.

  • next_section (str) – the name of the next section. If the next node is in the same section, this should be None.

  • wrap (int) – the maximum number of characters per line of text. Default wrapping width is 80.

node_type = 'node'
to_dot()

Formats the node parameters for a dot graph.

Returns

the dot graph for this node.

Return type

str

to_json()

Formats the node paramters for a JSON file.

Returns

the JSON data for this node.

Return type

dict

class script2py.nodes.Setter(key: str = '', value: str | int | bool = '', **kwargs)

Bases: script2py.nodes.Node

Sets a variable to a value.

Script format:

<<{ variable_name = "variable value" }>>
<<{ variable_name = 10 }>>

JSON format:

{
    "node_id": "0001",
    "type": "setter",
    "setter": {
        "variable_name": "variable value"
    },
    "next_id": "0002"
},
{
    "node_id": "0002",
    "type": "setter",
    "setter": {
        "variable_name": 10
    },
    "next_id": null
}
Parameters
  • key (str) – the variable name.

  • value (str or int or bool) – the value of the variable name.

  • kwargs – keyword arguments passed to the base class Node.

node_type = 'setter'
to_dot()

Formats the node parameters for a dot graph.

Returns

the dot graph for this node.

Return type

str

to_json()

Formats the node paramters for a JSON file.

Returns

the JSON data for this node.

Return type

dict

script2py.script module

script.py

Each script represents a full conversation consisting of multiple nodes (see nodes.py).

  • Each script may use any number of nodes that are grouped into sections, which are determined by the branching paths in the script by using the Choice node

  • The script is responsible for ordering and linking the nodes together

  • The script also contains basic information about the conversation, such as a list of speakers

class script2py.script.Script(filepath: str, last_modified: float = 0, wrap: int = 80)

Bases: object

Converts a script into nodes that are used to create a dot graph and JSON file.

Parameters
  • filepath (str) – the path to the script file.

  • last_modified (float) – Time of the most recent content modification (in seconds).

  • wrap (int) – the maximum number of characters per line of text. Default wrapping width is 80.

to_dot()

Creates the dot graph for the script.

Returns

the dot graph for this script.

Return type

str

to_json()

Creates the JSON data for the script.

Returns

a list of nodes in JSON format.

Return type

list

update()

Loads the script file from disk and writes the updated output to json and dot files.

Returns

True if the script was updated, False otherwise.

Return type

bool

script2py.visualizer module

visualizer.py

The visualizer monitors a script file and creates the JSON file and renders the dot graph whenever the script file changes.

  • Every time the script file changes, the Script object is updated and the file is reanalyzed, so large scripts may take longer to process.

  • Every time the script file is processed, there are three outputs: the JSON file, the dot file, and the PNG file (if GraphViz is installed)

class script2py.visualizer.Visualizer(dirpath, interval: int = 5, wrap: int = 80)

Bases: object

Outputs a file for graphviz to turn into a directed graph.

Parameters
  • filepath (str) – the filepath where the script files (*.s2py) are.

  • interval (int) – the number of seconds between checking if the files in the filepath has been updated. Default value is 5 seconds.

  • wrap (int) – the maximum number of characters per line of text. Default wrapping width is 80.

run()

Runs the visualizer.

The visualizer runs on an infinite loop and continually checks if the script file has been updated by comparing the time the file was last modified.

update()

Updates the scripts from the root directory and its subdirectories.

This method is also called to update the visualizers in the subdirectories.

Returns

True if one of the scripts was updated, False otherwise.

Return type

bool

Module contents