piatok 5. novembra 2010

simple yakuake bindings

I was looking for something which I could better describe with example:

import pyakuake

pyakuake.add_tab('egg')
pyakuake.run_in_tab('egg', "'vi test'") # unfortunaltely string in string (see note below)

pyakuake.run_in_tab('egg', "'isome text'")
pyakuake.run_in_tab('egg', "'\x1b'")
pyakuake.run_in_tab('egg', "':wq'")
pyakuake.run_in_tab('egg', "'cat test'")

Because I didnt find it I wrote this simple modul ->

# -*- coding: utf-8 -*-
"""
    Several (probably) useful dbus bindings for yakuake 
"""
import commands

def __dbus(first, second, param=''):
    """ dbus interface """
    return commands.getstatusoutput('qdbus org.kde.yakuake /yakuake'
+first+' '+second+' '+param)

def get_tab_title(tab_id):
""" return tab title for tab_id'th tab """
return __dbus('/tabs', 'tabTitle', str(tab_id))[1]

def get_tab_names():
""" return list of tab names """
sessions_ids = __dbus('/sessions', 'sessionIdList')[1].split(',')
return [get_tab_title(i) for i in sessions_ids ]

def add_tab(tab_name):
""" create tab with name "name" """
old_list = __dbus('/sessions', 'sessionIdList')[1].split(',')
__dbus('/sessions', 'addSession')[1]
new_list = __dbus('/sessions', 'sessionIdList')[1].split(',')
last_terminal = [i for i in new_list if i not in old_list][-1]
__dbus('/tabs', 'setTabTitle', last_terminal+' '+tab_name)

def run_in_term(command, term_id):
""" run command in terminal (dont change terminal and tab) """
__dbus('/sessions', 'runCommandInTerminal', term_id+' '+command)

def run_in_tab(tab_name, command, term_id=-1):
""" run by default in last terminal of tab """
for i in __dbus('/sessions', 'sessionIdList')[1].split(','):
if tab_name == get_tab_title(i):
tids = __dbus('/sessions', 'terminalIdsForSessionId' , i)
run_in_term(command, tids[1].split(',')[term_id])
return

def termids_in_session(s_id):
""" return list of terminals id in session """
return __dbus('/sessions', 'terminalIdsForSessionId' , s_id)[1].split(',')

def terms_in_tab(tab_name):
""" return list of terminals in tab """
for i in __dbus('/sessions', 'sessionIdList')[1].split(','):
if tab_name == get_tab_title(i):
return termids_in_session(i)
return []

def raise_tab(tab_name):
""" raise (show) tab """
for i in __dbus('/sessions', 'org.kde.yakuake.sessionIdList')[1].split(','):
#print i, name, get_tab_title(i)
if tab_name == get_tab_title(i):
return __dbus('/sessions',
'org.kde.yakuake.raiseSession' , i)

def start(config):
""" useful function for create predefined environment

for example
config=[
['user@machine1',
['"ssh user@machine1"',
'"cd somewhere"',
'"hg pull -u"',
'"make something"',]],
['user@machine2',
['"ssh user@machine2"',
'"cd elsewhere"',
'"ls -la"',]],
]

creates 2 new tabs and run several commands on it (if tabs with same names dont exist yet)
"""
t_n = get_tab_names()
for i in config:
if i[0] not in t_n:
add_tab( i[0])
if len(i)>1:
for cmd in i[1]:
run_in_tab(i[0], cmd)

Žiadne komentáre:

Zverejnenie komentára