nxt.nxt_io module

Methods for file input and output of nxt file data.

register_reference_path_expander(expander)[source]

Register a method to expand reference paths. An expander is a callable that takes exactly 1 argument, a filepath.

Parameters

expander (callable) – A python callable that takes a single argument, and returns an expanded filepath.

expand_reference_path(filepath, layer_dir=None)[source]

Where a reference points, resolved the way loading resolves it.

A reference is stored as written, which is often partial, so anything that wants to show a person where it actually lands has to ask the same machinery that loading asks. A path that is absolute once variables and “~” are expanded is taken as it is. Anything else is tried against every registered expander, which is what walks NXT_FILE_ROOTS, and then against the referring layer’s own directory, so a root wins over a file beside the layer, as it does on load. Without a layer directory the working directory stands in for it.

Parameters
  • filepath (str) – the reference as stored on the layer

  • layer_dir (str | None) – directory of the layer holding the reference, which is how a path relative to its own file resolves

Returns

(resolved path, whether a file is there). The path is returned even when nothing is there, because where it looked is the useful thing to show.

Return type

tuple

is_absolute(filepath)[source]

Whether a path is absolute once its variables and “~” are expanded.

Parameters

filepath (str) – path as written

Return type

bool

stored_reference_path(path, layer_dir)[source]

How a file is written into a layer as a reference.

Relative to the layer when it sits alongside it, which keeps a graph portable. Anything else is stored whole: making a path relative across drives or out of the layer’s folder would be worse than being explicit, and would get in the way of NXT_FILE_ROOTS. Symlinks are resolved on both sides first, so a file reached by another name for the same folder still counts as beside it.

Parameters
  • path (str) – the file to reference

  • layer_dir (str | None) – folder of the layer that holds the reference, or None when it has never been saved

Returns

forward slashed path, relative when the file is beside the layer

Return type

str

load_file_data(filepath, layer_dir=None)[source]

Given a file path this function determines if its a known nxt save format and attempts to open it. If the file is out of date it is passed to the legacy converter for conversion.

A relative path is looked for under each registered expander first, which is what walks NXT_FILE_ROOTS, and then relative to layer_dir, or to the working directory when no layer_dir is given.

Parameters
  • filepath (str) – string of save file filepath

  • layer_dir (str | None) – directory of the layer referencing this file, which a relative path is resolved against

Returns

dict of file data

save_file_data(save_data, filepath=None, file_format='.nxt')[source]

Saves the given file data to the given file path in the given file format. If no filepath is provided a temp file is generated. The default file format is ASCII.

Parameters
  • save_data – dict of save data

  • filepath – string of desired output file path or None

  • file_format – FILE_FORMAT constant

Returns

string of output file path.

generate_temp_file(suffix='.nxt')[source]

Safely generates a temp file and returns a Windows safe path

Parameters

suffix – Optional suffix for the file, default is .nxt

Returns

String filepath

generate_temp_dir(prefix='nxt_tmp_')[source]

Safely generates a temp dir and returns a Windows safe path

Parameters

prefix – Optional prefix to the dir name, default is nxt_tmp_

Returns

String dir path