Develop of plugins in python for education software TurtleArt

../images/turtle_art/valentin1.png
Autor:

Valentín Basel

Bio:

I'm computer system analyst and working in technology of Information, as manager of the computer area in the CEA-CONICET. I began in GNU/LINUX in 2000 when installed a SUSE 6.0 and decided to dedicate to the world the free-software I am currently an ambassador for Fedora in Argentina and my contribution for the comunity GNU about free educational robotics with the project ICARO, an environment of software and hardware of low cost for development of educational robots in primary schools and high schools.

I have a few experience in python (a couple of years) previously I always programmed in C / C ++, but the facility to create code, excellent documentation and other made that I program each time in python as main language

Email:

valentinbasel@gmail.com

Web:

TurtleArt (http://wiki.laptop.org/go/Turtle_Art) is a programming environment chart based on the Logo language (http://en.wikipedia.org/wiki/Logo_(programming_language)), in this you make lite programs and designers with a turtle of form graphics.

Since version 106, supports plugins that allow TurtleArt improve and add new "primitive" to our software. This article explains the creation of a custom plugin for TurtleArt. This article requires some knowledge in programming focused on objects, Python and how to work Sugar (http://en.wikipedia.org/wiki/Sugar_(desktop_environment)).

Preparing the environment

First we need to install in our computer a Sugar environment (not exclusive but it is always recommendable) In GNU/Linux Fedora distributions is very simple do it:

$ su -c 'yum install sugar*'

or

su -c 'yum groupinstall sugar'

With this command Linux System will install all Sugar environment with your activities and sugar emulator (very easy for testing)

Then download the last version of TurtleArt from this page:

http://activities.sugarlabs.org/en-US/sugar/downloads/latest/4027/addon-4027-latest.xo?src=addondetail

Also you can download at the following link a version TutleArt with plugin “tortucaro” (Icaro + TurtleArt) for easy studing. (Versión 106 at the time of issue of this magazine).

http://valentinbasel.fedorapeople.org/TurtleArt.tar

Attention: this version is not the same as that discussed in this article.

After downloading, unpackage the file .tar in our home and proceed to see the directory structure.

The files that we care edit are within the plugins folder.

The idea of this version of TurtleArt is to enable the development of new pallets by developers without modifying the code original source of the software (as in previous versions). Thus, before version 107, to make our plugin basically had to create a file with the following format:

<NAME>_plugin.py

Where <NAME> is a name, we give to our plugin. For example: "test_plugin.py".

Since version 107, the system was improved to accommodate all Plugin files directly into a folder inside the plugins directory.

Developing a plugin

You create a folder with name is test and inside create one file test/test.py.

In our file you create a python class with the same name of file and only put the first letter in capitals letter: "Test". (you have to respect that format).

Now we adding the following code to file:

# need to import all these modules to work with
# TurtleArt

import gst
import gtk
from fcntl import ioctl
import os
from gettext import gettext as _
from plugins.plugin import Plugin
from TurtleArt.tapalette import make_palette
from TurtleArt.talogo import media_blocks_dictionary, primitive_dictionary
from TurtleArt.tautils import get_path, debug_output
import logging

_logger = logging.getLogger('TurtleArt-activity prueba plugin')

class Test(Plugin):

    def __init__(self,parent):
        self._parent = parent
        self._status = False

The first part is essential to import all internal modules of TurtleArt; for length, will not explain in detail that treats each module.

With our class created and initialized (def __init__), proceed to put setup method

# Inside the Test Class

def setup(self):
    palette = make_palette('test',
                           colors=["#006060", "#A00000"],
                           help_string=_('This is a proof'))

Here declareted a new palett of name is test; the field test represent the two colors with those who make the gradient button TurtleArt (to differentiate them from other "primitive" system). The field help_string is very easy, basically is about the infomation of the pallet (but not about the buttons in this pallet, how see soon.)

# inside of the class Test and the setup method

#[1]
primitive_dictionary['boton'] = self._test_botton

#[2]
palette.add_block('boton',
             style='basic-style-1arg',
             label=_('boton'),
             prim_name='boton',
             help_string=_('proof button'))

#[3]
self._parent.lc.def_prim('boton', 1, lambda self, valor: primitive_dictionary['boton'](valor))

This is our first button, here we define its behavior, style, name and function pointed to.

In [1] we define a "primitive" for the diccionary. When put this, the system will call _test_button (the function where is the code action).

For [2] Palette.add_block create our button, which will be within our palette test; the first filed is button Nama. With style defined type of button, if it will or not arguments, if in place of sending information is received or if a special style. To know what types of styles you can use the file needs to be reviewed “tapalette.py” inside of sub directory “TurtleArt”, in our main directory where extract the .tar (We will only use the basic style with an argument basic-style-1arg). The label field is the name that show the button in your body. The prim_name field write as in the first field (boton) and then the help_string help text will show when we move the TurtleArt mouse over the button.

At line [3] of code is where the system links with the function button we define. Lamdba is an interesting syntax for defining minimal functions implemented by python, what we do is pass the function _test_button by primitive_dictionary['button'] variable value. As a an argument basic button, lets you put a box and store it in value that variable.

Finally, the only thing we lack is the function _test_button where we whole logic of the button itself.

# inside the Test class
def _test_button(self,valor):
    print "The button value is: ", value

This function is very modest. The only thing that it does is to show a bit of code to illustratethe functioning of a plugin.

You see, plugins TurtleArt program is not the most complex. while are poorly documented and most are in English, with a little patience can build a lot of custom palettes for the most varied activities.

With our file "test.py" finished and all we need is create the folder test/icons, leaving the directory tree follows:

::
~/TurtleArt/
plugins/
test/
icons/

In side to folder icons you put two files .svg to 55x55 pixeles with the name teston.svg and testoff.svg (the same name of plugin).

That icon should appear testoff.svg within the bar TurtleArt tools.

¡TurtleArt with our button of prueba working!

../images/turtle_art/ventana-turtle-art.png

Details armed within the palette button.

../images/turtle_art/boton-turtleart.png

Finally I present the complete file test.py

import gst
import gtk
from fcntl import ioctl
import os
from gettext import gettext as _
from plugins.plugin import Plugin
from TurtleArt.tapalette import make_palette
from TurtleArt.talogo import media_blocks_dictionary, primitive_dictionary
from TurtleArt.tautils import get_path, debug_output
import logging

_logger = logging.getLogger('TurtleArt-activity prueba plugin')

class Test(Plugin):

    def __init__(self, parent):
        self._parent = parent
        self._status = False

    def setup(self):
        palette = make_palette('test',
                               colors=["#006060", "#A00000"],
                               help_string=_('This is a proof'))
        primitive_dictionary['boton'] = self._test_button
        palette.add_block('boton',
                     style='basic-style-1arg',
                     label=_('Buton'),
                     prim_name='boton',
                     help_string=_('Proof Button'))
        self._parent.lc.def_prim('boton', 1,
                                 lambda self, valor: primitive_dictionary['boton'](valor))

    def _test_button(self,valor):
        print "The button value is: ", value

Help PET: Donate

blog comments powered by Disqus

Last Change: Thu Sep 22 08:54:01 2011.  -  This magazine is under a Creative Commons license