Scripts

Revision 7 as of 2012-05-01 10:30:31

Clear message

The Guide to Creating Your Accomplishments

Creating accomplishment scripts

<-- Previous page (Creating .accomplishment files) - - - - - Next page (blah) --->

This page is under construction. This guide is not yet complete!

Every single accomplishment requires a script, that is executed to determine whether the user has completed this accomplishment. Usually these scripts are not larger then the .accomplishment file, so no great programming skills are needed to prepare one.

Currently only Python scripts are supported. We hope to allow developers to prepare scripts in other languages too, but that's not our priority, while writing them in Python is simple and quick.

Returning values

The Accomplishments System will run scripts on it's own. The only thing the script has to do is to check if the accomplishment is complete, and report result by returning a number.

The exit codes are:

  • 0 - The accomplishment has been completed.

  • 1 - The accomplishment has not been completed.

  • 2 - There was an error while checking for completion.

  • 4 - There was an error while getting extra-information.

The easiest way to return such value is to use sys.exit(n).


While this should be enough for you to create scripts, there are some tips and examples for you, to make everything clearer.

Also, if your script has to use some extra-information, it can use Accomplishments Daemon API to get it - details below.

Everything your script prints will land in daemon's log - which is available at ~/.cache/accomplishments/daemon.log

Remember, that if your accomplishment collection uses scripts that import some unusual libraries, you will need to setup appropriate dependencies, when publishing a .deb package.

Local accomplishment scripts

Example

##!/usr/bin/python
import traceback, sys
try:
    try:
        #open the highscores file...
        highscoresfile = open("/var/games/gnomine.Small.scores")
    except IOError as e:
        # it seems that the file does not exist
        sys.exit(1)
    bestscore = highscoresfile.readline()
    if not bestscore:
        #the file is empty
        sys.exit(1)
    #the file is not empty
    sys.exit(0)
except:
    traceback.print_exc()
    sys.exit(2)

This is a working script for Begginer Minesweeper from previous chapter. It is intended to check if the user has ever completed a Small game in Gnomine. This is done by reading the highscores file, and checking whether it contains at least one entry. If so, we return 0, otherwise 1. All that is within a global try block, to react in case of unhandled errors (that does not make much sense here, but may be useful in your scripts, so it's just as an example).

Global accomplishment scripts

Scripts for global accomplishments are slightly more complicated. Also, they may use Accomplishments Daemon API to get extra-information easily.

Example

##!/usr/bin/python
import traceback, sys

from accomplishments.daemon import dbusapi

try:
    import sys, os, pwd, subprocess
    from launchpadlib.launchpad import Launchpad

    # Connect to the daemon to get extra-information.
    api = dbusapi.Accomplishments()
    f = api.get_extra_information("ubuntu-community", "launchpad-email")
    if bool(f[0]["launchpad-email"]) == False:
        sys.exit(4)
    else:
        email = f[0]["launchpad-email"]

    # Get count of bugs reported by user from Launchpad, using email to identify user
    l = Launchpad.login_anonymously(
        'ubuntu-community accomplishments', 'production')
    me = l.people.getByEmail(email=email)
    if me == None:
        # User of such e-mail does not exist...
        sys.exit(1)
    else:
        ubuntu=l.projects['ubuntu']
        # Search for bugs of any status reported by this user in Ubuntu...
        bugs_reported = ubuntu.searchTasks(bug_reporter=me, 
            status=['New', 'Incomplete', 'Invalid', 'Confirmed', 'Triaged', 
                    'In Progress', 'Fix Committed', 'Fix Released', 'Opinion',
                    "Won't Fix"])
        if len(bugs_reported) > 0:
            # Success! Accomplishment completed!
            sys.exit(0)
        else:
            # No such bugs were found...
            sys.exit(1)
except SystemExit, e:
    sys.exit(e.code)
except:
    traceback.print_exc()
    sys.exit(2)

This example is an actual code for Filed First Bug accomplishment from previous chapter. It uses launchpadlib to connect to Launchpad, and check bugs that given person reported in Ubuntu. There are few lines of special interest, that are worth explaining:

    from accomplishments.daemon import dbusapi
    api = dbusapi.Accomplishments()

The above used to connect to Accomplishments Daemon.