Plugin API¶
Writing plugins¶
To add support for a new file format, write a new KurtPlugin subclass:
import kurt
from kurt.plugin import Kurt, KurtPlugin
class MyScratchModPlugin(KurtPlugin):
def load(self, fp):
kurt_project = kurt.Project()
# ... set kurt_project attributes ... #
return kurt_project
def save(self, fp, kurt_project):
# ... save kurt_project attributes to file ...
Kurt.register(MyScratchModPlugin())
Take a look at kurt.scratch20 for a more detailed example.
List available plugins¶
To get a list of the plugins registered with Kurt:
>>> kurt.plugin.Kurt.plugins.keys()
['scratch20', 'scratch14']
You should see your plugin in the output, unless you forgot to register it.
Notes¶
Some things to keep in mind:
- Most Scratch file formats have the stage as the base object – so project attributes, such as the notes and the list of sprites, are stored on the stage object.
- For Scratch, which doesn’t support stage-specific variables, global variables and lists are stored on the Project, not the Stage.
- If your plugin contains obsolete blocks, they should be at the end of the
blockslist. Otherwise things might not work properly.
KurtPlugin¶
-
class
kurt.plugin.KurtPlugin[source]¶ Bases:
objectHandles a specific file format.
Loading and saving converts between a
Project, kurt’s internal representation, and a file of this format.-
name= 'scratch14'¶ Short name of this file format, Python identifier style. Used internally by kurt.
Examples:
"scratch14","scratch20.sprite","byob3","snap"
-
display_name= 'Scratch 2.0 Sprite'¶ Human-readable name of this file format. May be displayed to the user. Should not contain “Project” or “File”.
Examples:
"Scratch 1.4","Scratch 2.0 Sprite","BYOB 3.1"
-
extension= '.sb'¶ The extension used by this format, with leading dot.
Used by
Project.loadto recognise its files.
-
features= []¶ A list of the
Featuresthat the plugin supports.
-
blocks= []¶ The list of
PluginBlockTypeobjects supported by this plugin, in the order they appear in the program’s interface.
-
Kurt¶
-
class
kurt.plugin.Kurt[source]¶ Bases:
objectThe Kurt file format loader.
This class manages the registering and selection of file formats. Used by
Project.-
classmethod
register(plugin)[source]¶ Register a new
KurtPlugin.Once registered, the plugin can be used by
Project, when:Project.loadsees a file with the right extensionProject.convertis called with the format as a parameter
-
classmethod
get_plugin(name=None, **kwargs)[source]¶ Returns the first format plugin whose attributes match kwargs.
For example:
get_plugin(extension="scratch14")
Will return the
KurtPluginwhoseextensionattribute is"scratch14".The
nameis used as theformatparameter toProject.loadandProject.save.Raises: ValueErrorif the format doesn’t exist.Returns: KurtPlugin
-
classmethod