[Deskbar] New extension: basic unit converter and calculator



I wrote this mainly to scratch an itch. It uses the "GNU units" backend.

I am hoping that somebody else will take it up and continue to develop it.

To install it, just drop it into ~/.gnome2/deskbar-applet/handlers as
units.py, and then remove and re-add deskbar and switch on the
extension from Preferences. You need "/usr/bin/units" which, on
Debian/Ubuntu, is in the "units" package in universe.

Usage examples:

Input: "'5 cm' in" (what is 5 cm in inches? note the quotes)
Output: "Copy result 1.9685039 to clipboard"

Input: "'10 gallons' '5 dm^3'" (how many groups of 5 decimetres cubed
are there in 10 gallons?)

Input: "m cm" (what is 1 metre in centimetres?)

Input: '"399 USD" GBP' (how many £ to buy a top iPod?)

Input: '"5 tsp" tbsp' (these units are localised to america or the UK)

Input: 'acre' (define an acre)
Output: "Copy result usacre = 10 surveychain^2 = 4046.8726 m^2 to clipboard"

Input 'acre m2' (1 acre in square metres)

Input 'tempC(100) tempF' (boiling point of water in Frarenheit)

In other words:
Quotes are *required* (single or double). If you don't say how many of
something you want, you want one. Plurals are handled.

"Units" is a fantastic program and I recommend reading the man page.
(You will need to read it if you want to convert temperatures
correctly!)

PS It also handles math with some constants such as pi, e, and c.
from gettext import gettext as _

import gtk
import deskbar
from deskbar.Handler import Handler
from deskbar.Match import Match
import os

#import traceback

def _more_info(dialog):
	deskbar.Utils.more_information_dialog(dialog, _("Setting Up Units"), _("Use 'sudo apt-get install units' to install the backend for the units calculator."))

def _check_requirements():
	if os.access('/usr/bin/units', os.F_OK):
		return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
	else:
		return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, _("You need the \"units\" package."), _more_info)

HANDLERS = {
        "UnitHandler" : {
                "name": _("Units"),
                "description": _("Units!"),
		"requirements": _check_requirements
        }
}

class UnitMatch(Match):
        def __init__(self, backend, **args):
                deskbar.Match.Match.__init__(self, backend, **args)
                
        def action(self, text=None):
                for clipboard in ('CLIPBOARD', 'PRIMARY'):
                        gtk.clipboard_get(clipboard).set_text(self.name)

        def get_category(self):
                return "actions"

        def get_verb(self):
                return _("Copy result <b>%(name)s</b> to clipboard")
        
        def get_hash(self, text=None):
                return self.name

class UnitHandler(Handler):
        def __init__(self):
                deskbar.Handler.Handler.__init__(self, "gnome-calculator")

        def query(self, query):
                try:
			answer = os.popen("units -t '" + query + "' 'one'").read().replace('Definition:','').strip()
			isanswer = answer.replace('.','').isdigit()
			answer2 = os.popen("units -t " + query).read().replace('Definition:','').strip()
			isanswer2 = ((answer2.find('error') == -1) & (answer2.find('Error') == -1) & (answer2.find('Unknown') == -1) & (answer2 != ''))
                        if isanswer:
				return [UnitMatch(self, name=answer)]
			elif isanswer2:
				return [UnitMatch(self, name=answer2)]
			else:
				return []
                except:
                        #print traceback.format_exc()
                        return []








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]