[Twisted-Python] Unruly Callback Code

Justin Johnson justinjohnson at fastmail.fm
Tue Aug 5 10:02:31 EDT 2003


I have some code that is getting a bit unruly due to the number of
callbacks I am doing.  I've defined all of this in a single method
because it is part of a command line tool with multiple sub-commands,
each with its own options, and I made the entire program (i.e. the
command line tool) the class, and the sub-commands are methods on that
class.  However, I'm beginning to think I need to rearrange this so it is
more maintainable, like putting each sub-command in its own class so the
callbacks can be methods on the class instead of inline functions.

Would someone mind looking at the code below and suggesting ways to clean
this up?

Thanks.
-Justin


	# Subcommand: mkvob
	def cmd_mkvob(self):
		"""Usage: mkvob -v vob-tag[,vob-tag,...] -o site [-g group] [-s site,site,...]
		"""
		options = self.config.subOptions
		original_site = options["original"]
		group = options["group"]
		vobs = options["vobs"].split(",")
		sites = []
		if options["sites"] != None:
			sites = options["sites"].split(",")

		def gotObject(object):
			self.perspectives[original_site] = object
			print "Successfully connected to %s (%s)." % (original_site, config.siteToServer[original_site])
			print "Calling remote mkvob ..."
			d = object.callRemote("mkvob", vobs, group)
			return d
		def gotNoObject(object):
			print "Failed to connect:", object
			return object

		def renameReplica(results, vobs, original_site):
			# Do I have to connect again to do this?
			obj = self.perspectives[original_site]
			obj.callRemote("renameReplica", vobs, "original", original_site)
			return obj

		def mkRepExport(results, vobs, sites, original_site):
			obj = self.perspectives[original_site]
			d = obj.callRemote("mkReplicaExport", vobs, sites)
			return d

		def _stop(*args):
			print "The mkvob operation completed successfully"
			reactor.stop()
			return args

		def mkRepImport(results, vobs, sites):
			if len(vobs) > 0 and len(sites) > 0:
				deferreds = []
				for site in sites:
					def onSuccess(object, site):
						self.perspectives[site] = object
						d = object.callRemote("mkReplicaImport", vobs)
						return d
					def applyTriggers(results, vobs, site):
						obj = self.perspectives[site]
						d = obj.callRemote("applyTriggers", vobs)
						return d
					d = self.connectToServer(config.siteToServer[site])
					d.addCallback(onSuccess, site).addErrback(log.err)
					d.addCallback(applyTriggers, vobs, site).addErrback(log.err)
					deferreds.append(d)
				return defer.DeferredList(deferreds)
			return results
				
		d = self.connectToServer(config.siteToServer[original_site])
		d.addCallbacks(gotObject, gotNoObject)
		d.addCallback(renameReplica, vobs, original_site).addErrback(log.err)
		d.addCallback(mkRepExport, vobs, sites, original_site).addErrback(log.err)
		d.addCallback(mkRepImport, vobs, sites).addErrback(log.err)
		d.addBoth(_stop)
		#log.startLogging(sys.stdout)
		reactor.run()




More information about the Twisted-Python mailing list