Pete Ryland

 Home  Projects  Words  Photos  Information  Themes  News  Links  Blog

Pete Ryland's Web Log

I didn't want a blog but they made me do it.

Wed, 18 Oct 2006

EGG! Prototype 2! [] (10:30)

And here it is: egg20061018.tar.gz complete with two example applications as described below.

A week on from last week's release and it has really moved on in leaps and bounds. The following is a list of the recent improvements:

  • The variety of decorators has been replaced with one, egg.eggify, which intelligently decides how to wrap the method
  • Toolbar support has been added
  • Statusbar support has been added

Internally, the biggest change is that the wrapper now replaces the callback method with a (callable) object of type Egg. This turns out to be much more efficient than simply adding attributes to the existing function, although there is a tradeoff with the time it takes for the getwidget() et al calls.

Anyway, on with an example:

#!/usr/bin/python

import egg, gtk

class EGGFileView(egg.App):
  """A file viewer example application for EGG."""

  # Set up filename as a context since some functions require an open file.
  # When set to None, 0 or False, it will disable widgets needing this context.
  filename = egg.context(None)

  # The egg.eggify() decorator wraps all our callback methods in order to
  # provide necessary widgets automatically
  @egg.eggify()
  def open(self, filename):
    # Note that method docstrings are in the imperative
    """Open a file (read-only)"""
    # Pretty rough code: just read the *whole* file into a gtk TextBuffer
    file = open(filename)
    mytext = "".join(file.readlines())
    file.close()
    self.textbuffer.set_text(mytext)
    self.textbuffer.apply_tag(self.mytexttag, self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter())
    # Tell the user what we've done
    self.feedback(filename + " opened.")
    # Provide the filename context, enabling appropriate widgets
    self.filename = filename

  # We provide the filename as our context to the eggify decorator function,
  # since we can't perform a close() without a non-None filename
  @egg.eggify(context=filename)
  def close(self):
    """Close the current file"""
    self.textbuffer.set_text("")
    # Tell the user what we've done
    self.feedback(self.filename + " closed.")
    # Remove the filename context, disabling functions requiring an open file
    self.filename = None

  def __init__(self):
    egg.App.__init__(self,
                     menu=[["_File", self.open, self.close, None, self.quit],
                           ["_Help", self.about]],
                     tools=[self.open, self.close, self.quit],
                     version="1.0",
                     authors=["Pete Ryland "],
                     copyright="Copyright (c) 2006 Pete Ryland",
                     license=egg.GPL
                    )
    # The below is standard gtk-type code.  This gives you an idea of what
    # we're saving ourselves from by using EGG.  Of course, EGG will eventually
    # provide TextView setup stuff somehow.  Please let me know if you have any
    # good ideas about this or other unprovided stuff like layouts.
    self.mytexttag = gtk.TextTag()
    self.mytexttag.props.family = "Monospace"
    self.textbuffer = gtk.TextBuffer()
    self.textbuffer.get_tag_table().add(self.mytexttag)
    textview = gtk.TextView(self.textbuffer)
    scrolltextview = gtk.ScrolledWindow()
    scrolltextview.add(textview)
    scrolltextview.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    self.set_contents(scrolltextview)

if __name__ == "__main__":
  a=EGGFileView()
  a()

Here's a few things to note:

  • The menu and toolbar are set up in the egg.App.__init__ call with lists of methods. The About information is set there too.
  • The methods are called open and close which happen to resemble the names of gtk stock items, and so those items along with their standard shortcuts and pretty icons are used.
  • Because open takes a single argument called filename, eggify automatically knows to launch a file selection dialog first, and send it the filename. The method name and its argument are used to determine whether the file selection with be an "open" or "save" one, work on directories, and/or allow multiple selections.
  • The inherited feedback method sets the statusbar message, but it's still very early days for this part of the API.

Well, please do try it out, the tarball is linked at the top. And don't hesitate to send me an email with any feedback you may have (whether positive or negative!).

>

Request served by muriel.pdr.me.uk located in London, UK.

Trademarks owned by their respective owners.

Original content Copyright (c) 1991-2005 Pete Ryland (gpg key).