#!/usr/bin/env python2.5
##
## @system_bus_emulator.py
##
## Copyright (C) 2008 Nokia. All rights reserved.
##
## Create session bus and emulate real session or system bus 
##
## Requires python2.5-gobject and python2.5-dbus
##
##
import signal
import sys
import os
import re
import getopt
from time import sleep

SYSTEM = 6
SESSION = 5

def parse (pattern, string):
    result = re.match(pattern, string)
    if result != None:
        return result
    else:
        raise Exception(pattern+" not found in "+string+" quit dbus emulator")
        sys.exit(1)

def createBus(type,configPath):
    output = None
    if ((os.getenv("DBUS_SYSTEM_BUS_ADDRESS") == None and type == SYSTEM) or type == SESSION):
        if os.path.isdir(configPath):
            output = os.popen("dbus-launch --config-file=" + configPath + os.path.sep + "session.conf").readlines()
        else:
            output = os.popen("dbus-launch").readlines()
        if len(output) >= 2:
            result = parse("(DBUS_SESSION_BUS_ADDRESS)=(.*)",output[0]) , parse("(DBUS_SESSION_BUS_PID)=(.*)",output[1])
            if type == SESSION:
                os.environ['DBUS_SESSION_BUS_ADDRESS'] = result[0].group(2)
                os.environ['DBUS_SESSION_BUS_PID'] = result[1].group(2)
            
            if type == SYSTEM: 
                os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = result[0].group(2)
                os.environ['DBUS_SYSTEM_BUS_PID'] = result[1].group(2)
    else:
        if type == SYSTEM:
            print "Instance of system bus already exists"
        else:
            print "Instance of session bus already exists"
       
def deleteBus(type):
    if type == SYSTEM:
        os.kill(int(os.getenv("DBUS_SYSTEM_BUS_PID")),15)
    elif type == SESSION:
        os.kill(int(os.getenv("DBUS_SESSION_BUS_PID")),15)
    
def usage(prog="dbus_emulator"):
    print """
    dbus_emulator : create session bus which will emulate real session or system
    bus
    
    dbus_emulator [-S] [-s] [-h]
    
    -h                   print this message
    
    -S                   create system bus instance 
    
    -s                   create session bus instance

 """
    
# Main stuff
def main():

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hSs")
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    for o, a in opts:
        if o == "-S":
            createBus(SYSTEM,"")
        elif o == "-s":
            createBus(SESSION,"")
        elif o == "-h":
            usage();
            sys.exit(0)
        else:
            assert False, "unhandled option"

if __name__ == "__main__":
    main()
            

@@ -305,16 +305,36 @@
 rt sleep
+%0Aimport ConfigParser
 %0A%0ASYSTEM
@@ -351,16 +351,514 @@
 ON = 5%0A%0A
+def readConfig(configFilePath):%0A    config = ConfigParser.RawConfigParser()%0A    config.read(configFilePath)%0A    sections = config.sections()%0A    return config.items(sections%5B0%5D)%0A        %0Adef writeConfig(section, keyValuePairs, configFilePath):%0A    config = ConfigParser.RawConfigParser()%0A    %0A    config.add_section(section)%0A    for keyValuePair in keyValuePairs:%0A        config.set(section, keyValuePair%5B0%5D, keyValuePair%5B1%5D)%0A    %0A    f = open(configFilePath,%22ab%22)%0A    config.write(f)%0A    f.close%0A%0A
 def pars
@@ -1100,24 +1100,29 @@
 onfigPath):%0A
+    %0A
     output =
@@ -1226,24 +1226,25 @@
 N):%0A        
+#
 if os.path.i
@@ -1261,32 +1261,33 @@
 gPath):%0A        
+#
     output = os.
@@ -1339,21 +1339,16 @@
 th + os.
-path.
 sep + %22s
@@ -1377,34 +1377,31 @@
 s()%0A        
+#
 else:%0A
-    
         outp
@@ -1787,186 +1787,694 @@
     
-%0A            if type == SYSTEM: %0A                os.environ%5B'DBUS_SYSTEM_BUS_ADDRESS'%5D = result%5B0%5D.group(2)%0A                os.environ%5B'DBUS_SYSTEM_BUS_PID'%5D = result%5B1%5D.group(2)
+    keyPairs = %5B('DBUS_SESSION_BUS_ADDRESS',result%5B0%5D.group(2)),('DBUS_SESSION_BUS_PID',result%5B1%5D.group(2))%5D%0A                writeConfig(%22Session%22, keyPairs, %22/tmp/dbus%22)%0A                                %0A            if type == SYSTEM: %0A                os.environ%5B'DBUS_SYSTEM_BUS_ADDRESS'%5D = result%5B0%5D.group(2)%0A                os.environ%5B'DBUS_SYSTEM_BUS_PID'%5D = result%5B1%5D.group(2)%0A                keyPairs = %5B('DBUS_SYSTEM_BUS_ADDRESS',result%5B0%5D.group(2)),('DBUS_SYSTEM_BUS_PID',result%5B1%5D.group(2))%5D%0A                writeConfig(%22Session%22, keyPairs, %22/tmp/dbus%22)%0A                %0A        #print os.environ%5B'DBUS_SYSTEM_BUS_ADDRESS'%5D%0A        #print os.environ%5B'DBUS_SESSION_BUS_ADDRESS'%5D
 %0A   
@@ -2638,23 +2638,16 @@
 exists%22%0A
-       
 %0Adef del
@@ -2826,24 +2826,49 @@
 %22)),15)%0A    
+#os.remove(%22/tmp/dbus%22)%0A 
 %0Adef usage(p

add config file