Pberndt V4

Direkt zum Inhalt springen


Quellcode runGraphical.py

Beschreibung

Im WM auf eine Tastenkombination geschaltet, erlaubt es dieses Script, viele Programme leicht auszuführen: Nach dem Programmstart wird ein Tastendruck abgefangen, der sich mit Befehlen belegen lässt.

Sourcecode

#!/bin/env python
# vim:fileencoding=iso-8859-1:ft=python
#
"""
    runGraphical
    Run a programm in X11 using a shortcut

    Configuration file syntax:

    command {
        .. shell script to execute ..
    }

    or

    command shell line to execute

    lines beginning with # are ignored.
"""
import gtk
import os
import sys
import re

# Read configuration
try:
    config = open(os.path.expanduser("~/.runGraphical.config")).read()
    config = re.sub("\r\n?", "\n", config)
    commands = {}
    n = -1
    cmdLevel = 0
    while n < len(config) - 1:
        n += 1
        if config[n].isspace():
            continue
        if config[n] == "#":
            n = config.find("\n", n)
            continue

        command = ""
        while not (config[n].isspace() or n >= len(config)):
            command += config[n]
            n += 1
        while config[n].isspace() and n < len(config) and config[n] != "\n":
            n += 1
        if config[n] == "\n":
            raise Exception("Configuration error")
        execute = ""
        if config[n] != "{":
            while config[n] != "\n" and n < len(config):
                execute += config[n]
                n += 1
        else:
            n += 2
            cmdLevel = 1
            while n < len(config):
                if config[n] == "}":
                    cmdLevel -= 1
                    if cmdLevel == 0:
                        break
                elif config[n] == "{":
                    cmdLevel += 1
                execute += config[n]
                n += 1
            if cmdLevel > 0:
                raise Exception("Configuration error")
        commands[command] = execute.strip()
except:
    print "Configuration error in ~/.runGraphical.config in character %d" % n
    sys.exit(0)

# Show dialog
entry = gtk.Entry()
def check(*x):
    if entry.get_text() in commands:
        gtk.main_quit()
entry.connect("key_release_event", check)

window = gtk.Window()
window.add(entry)
window.connect("hide", lambda *x: gtk.main_quit())
window.set_decorated(gtk.gdk.DECOR_ALL & (not gtk.gdk.DECOR_TITLE))
window.stick()
window.move(0, 0)
window.show_all()
gtk.main()
command = entry.get_text()

del window
del entry
del gtk

# Execute
if command in commands:
    os.system(commands[command] + " &")

Download

Dateiname
runGraphical.py
Größe
1.87kb