bridge: templates' constants can now be overrided

This commit is contained in:
Goffi 2011-06-06 18:35:30 +02:00
commit 8f6eb3cc41
3 changed files with 29 additions and 5 deletions

View file

@ -39,6 +39,8 @@ MANAGED_PROTOCOLES=['dbus','mediawiki']
DEFAULT_PROTOCOLE='dbus'
FLAGS=['deprecated', 'async']
ENV_OVERRIDE = "SAT_BRIDGE_CONST_" #Prefix used to override a constant
import sys
import os
from os import path
@ -378,6 +380,8 @@ class DbusConstructor(Constructor):
#at this point, signals_part, methods_part and direct_calls should be filled,
#we just have to place them in the right part of the template
core_bridge = []
const_override_pref = filter(lambda env: env.startswith(ENV_OVERRIDE), os.environ)
const_override = [env[len(ENV_OVERRIDE):] for env in const_override_pref]
try:
with open(self.core_template) as core_template:
for line in core_template:
@ -388,6 +392,12 @@ class DbusConstructor(Constructor):
elif line.startswith('##DIRECT_CALLS##'):
core_bridge.extend(direct_calls)
else:
if line.startswith('const_'):
const_name = line[len('const_'):line.find(' = ')]
if const_name in const_override:
print ("const %s overriden" % const_name)
core_bridge.append('const_%s = %s' % (const_name, os.environ[ENV_OVERRIDE+const_name]))
continue
core_bridge.append(line.replace('\n',''))
except IOError:
print ("Can't open template file [%s]" % self.core_template)
@ -430,12 +440,20 @@ class DbusConstructor(Constructor):
#at this point, methods_part should be filled,
#we just have to place it in the right part of the template
frontend_bridge = []
const_override_pref = filter(lambda env: env.startswith(ENV_OVERRIDE), os.environ)
const_override = [env[len(ENV_OVERRIDE):] for env in const_override_pref]
try:
with open(self.frontend_template) as frontend_template:
for line in frontend_template:
if line.startswith('##METHODS_PART##'):
frontend_bridge.extend(methods_part)
else:
if line.startswith('const_'):
const_name = line[len('const_'):line.find(' = ')]
if const_name in const_override:
print ("const %s overriden" % const_name)
frontend_bridge.append('const_%s = %s' % (const_name, os.environ[ENV_OVERRIDE+const_name]))
continue
frontend_bridge.append(line.replace('\n',''))
except IOError:
print ("Can't open template file [%s]" % self.frontend_template)

View file

@ -27,6 +27,7 @@ import dbus.mainloop.glib
from logging import debug, info
const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
const_OBJ_PATH = '/org/goffi/SAT/bridge'
const_COMM_SUFFIX = ".communication"
const_REQ_SUFFIX = ".request"
@ -125,7 +126,7 @@ class DBusBridge(Bridge):
info ("Init DBus...")
self.session_bus = dbus.SessionBus()
self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')
self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)
##DIRECT_CALLS##

View file

@ -23,6 +23,11 @@ from bridge_frontend import BridgeFrontend
import dbus, dbus.glib
from logging import debug
const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
const_OBJ_PATH = '/org/goffi/SAT/bridge'
const_COMM_SUFFIX = ".communication"
const_REQ_SUFFIX = ".request"
class BridgeExceptionNoService(Exception):
pass
@ -30,12 +35,12 @@ class DBusBridgeFrontend(BridgeFrontend):
def __init__(self):
try:
self.sessions_bus = dbus.SessionBus()
self.db_object = self.sessions_bus.get_object('org.goffi.SAT',
'/org/goffi/SAT/bridge')
self.db_object = self.sessions_bus.get_object(const_INT_PREFIX,
const_OBJ_PATH)
self.db_comm_iface = dbus.Interface(self.db_object,
dbus_interface='org.goffi.SAT.communication')
dbus_interface=const_INT_PREFIX + const_COMM_SUFFIX)
self.db_req_iface = dbus.Interface(self.db_object,
dbus_interface='org.goffi.SAT.request')
dbus_interface=const_INT_PREFIX + const_REQ_SUFFIX)
except dbus.exceptions.DBusException,e:
if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown':
raise BridgeExceptionNoService