apptools.scripting.recorder module

Code to support recording to a readable and executable Python script.

FIXME:
  • Support for dictionaries?

class apptools.scripting.recorder.Recorder[source]

Bases: HasTraits

clear()[source]

Clears all previous recorded state and unregisters all registered objects.

get_code()[source]

Returns the recorded lines as a string of printable code.

get_object_path(object)[source]

Returns the path in the object hierarchy of a registered object. Useful for debugging.

get_script_id(object)[source]

Returns the script_id of a registered object. Useful when you want to manually add a record statement.

is_registered(object)[source]

Returns True if the given object is registered with the recorder.

record(code)[source]

Record a string to be stored to the output file.

Parameters

code (str) – A string of text.

record_function(func, args, kw)[source]

Record a function call given the function and its arguments.

register(object, parent=None, trait_name_on_parent='', ignore=None, known=False, script_id=None)[source]

Register an object with the recorder. This sets up the object for recording.

By default all traits (except those starting and ending with ‘_’) are recorded. For attributes that are themselves recordable, one may mark traits with a ‘record’ metadata as follows:

  • If metadata record=False is set, the nested object will not be recorded.

  • If record=True, then that object is also recorded if it is not None.

If the object is a list or dict that is marked with record=True, the list is itself not listened to for changes but all its contents are registered.

If the object has a trait named recorder then this recorder instance will be set to it if possible.

Parameters
  • object (Instance(HasTraits)) – The object to register in the registry.

  • parent (Instance(HasTraits)) – An optional parent object in which object is contained

  • trait_name_on_parent (str) – An optional trait name of the object in the parent.

  • ignore (list(str)) – An optional list of trait names on the object to be ignored.

  • known (bool) – Optional specification if the object id is known on the interpreter. This is needed if you are manually injecting code to define/create an object.

  • script_id (str) – Optionally specify a script_id to use for this object. It is not guaranteed that this ID will be used since it may already be in use.

save(file)[source]

Save the recorded lines to the given file. It does not close the file.

ui_save()[source]

Save recording to file, pop up a UI dialog to find out where and close the file when done.

unregister(object)[source]

Unregister the given object from the recorder. This inverts the logic of the register(…) method.

write_script_id_in_namespace(script_id)[source]

If a script_id is not known in the current script’s namespace, this sets it using the path of the object or actually instantiating it. If this is not possible (since the script_id matches no existing object), nothing is recorded but the framework is notified that the particular script_id is available in the namespace. This is useful when you want to inject code in the namespace to create a particular object.

exception apptools.scripting.recorder.RecorderError[source]

Bases: Exception