From 9fda86d8b94e840361164d48b93b64b8b99af0b7 Mon Sep 17 00:00:00 2001 From: Louis-Francis Ratté-Boulianne Date: Fri, 22 Oct 2010 14:18:00 -0400 Subject: Add function to ensure a handle by its name (create it if not found) Connection managers can't override create_handle and normalize_handle to have more specific behaviours. Extra arguments can be given to create handles. --- src/server/conn.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/server/conn.py b/src/server/conn.py index 1d8ab7d..db2da89 100644 --- a/src/server/conn.py +++ b/src/server/conn.py @@ -158,6 +158,23 @@ class Connection(_Connection, DBusProperties): if (type <= HANDLE_TYPE_NONE or type > LAST_HANDLE_TYPE): raise InvalidArgument('handle type %s not known' % type) + def create_handle(self, type, name, **kwargs): + id = self.get_handle_id() + handle = Handle(id, handle_type, name) + self._handles[handle_type, id] = handle + return handle + + def normalize_handle_name(self, type, name): + return name + + def ensure_handle(self, type, name, **kwargs): + self.check_handle_type(type) + name = self.normalize_handle_name(type, name) + for candidate in self._handles.values(): + if candidate.type == type and candidate.name == name: + return candidate + return self.create_handle(type, name, **kwargs) + def get_handle_id(self): id = self._next_handle_id self._next_handle_id += 1 @@ -251,17 +268,7 @@ class Connection(_Connection, DBusProperties): ret = [] for name in names: - handle = None - for candidate in self._handles.values(): - if candidate.get_name() == name: - handle = candidate - break - - if not handle: - id = self.get_handle_id() - handle = Handle(id, handle_type, name) - self._handles[handle_type, id] = handle - + handle = self.ensure_handle(handle_type, name) self.add_client_handle(handle, sender) ret.append(handle.get_id()) -- cgit v1.2.3