Add serialize/deserialize functions for Service objects
This commit is contained in:
		
							parent
							
								
									afc587212e
								
							
						
					
					
						commit
						9f1c77a55a
					
				@ -1,6 +1,7 @@
 | 
				
			|||||||
import avahi
 | 
					import avahi
 | 
				
			||||||
from sugar import util
 | 
					from sugar import util
 | 
				
			||||||
import string
 | 
					import string
 | 
				
			||||||
 | 
					import dbus
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _txt_to_dict(txt):
 | 
					def _txt_to_dict(txt):
 | 
				
			||||||
	"""Convert an avahi-returned TXT record formatted
 | 
						"""Convert an avahi-returned TXT record formatted
 | 
				
			||||||
@ -56,6 +57,25 @@ def is_multicast_address(address):
 | 
				
			|||||||
		return True
 | 
							return True
 | 
				
			||||||
	return False
 | 
						return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def deserialize(sdict):
 | 
				
			||||||
 | 
						try:
 | 
				
			||||||
 | 
							name = sdict['name']
 | 
				
			||||||
 | 
							full_stype = sdict['full_stype']
 | 
				
			||||||
 | 
							activity_stype = sdict['activity_stype']
 | 
				
			||||||
 | 
							domain = sdict['domain']
 | 
				
			||||||
 | 
							port = sdict['port']
 | 
				
			||||||
 | 
							properties = sdict['properties']
 | 
				
			||||||
 | 
						except KeyError, exc:
 | 
				
			||||||
 | 
							raise ValueError("Serialized service object was not valid.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						address = None
 | 
				
			||||||
 | 
						try:
 | 
				
			||||||
 | 
							address = sdict['address']
 | 
				
			||||||
 | 
						except KeyError:
 | 
				
			||||||
 | 
							pass
 | 
				
			||||||
 | 
						return Service(name, full_stype, domain, address=address,
 | 
				
			||||||
 | 
							port=port, properties=properties)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_ACTIVITY_UID_TAG = "ActivityUID"
 | 
					_ACTIVITY_UID_TAG = "ActivityUID"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -113,6 +133,18 @@ class Service(object):
 | 
				
			|||||||
		if uid and not self._properties.has_key(_ACTIVITY_UID_TAG):
 | 
							if uid and not self._properties.has_key(_ACTIVITY_UID_TAG):
 | 
				
			||||||
			self._properties[_ACTIVITY_UID_TAG] = uid
 | 
								self._properties[_ACTIVITY_UID_TAG] = uid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						def serialize(self):
 | 
				
			||||||
 | 
							sdict = {}
 | 
				
			||||||
 | 
							sdict['name'] = dbus.Variant(self._name)
 | 
				
			||||||
 | 
							sdict['full_stype'] = dbus.Variant(self._full_stype)
 | 
				
			||||||
 | 
							sdict['activity_stype'] = dbus.Variant(self._activity_stype)
 | 
				
			||||||
 | 
							sdict['domain'] = dbus.Variant(self._domain)
 | 
				
			||||||
 | 
							if self._address:
 | 
				
			||||||
 | 
								sdict['address'] = dbus.Variant(self._address)
 | 
				
			||||||
 | 
							sdict['port'] = dbus.Variant(self._port)
 | 
				
			||||||
 | 
							sdict['properties'] = dbus.Variant(self._properties)
 | 
				
			||||||
 | 
							return sdict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	def get_name(self):
 | 
						def get_name(self):
 | 
				
			||||||
		"""Return the service's name, usually that of the
 | 
							"""Return the service's name, usually that of the
 | 
				
			||||||
		buddy who provides it."""
 | 
							buddy who provides it."""
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user