[Python-checkins] python/dist/src/Mac/Lib aepack.py,1.4,1.5 aetools.py,1.3,1.4 aetypes.py,1.2,1.3

jackjansen@users.sourceforge.net jackjansen@users.sourceforge.net
Wed, 07 Aug 2002 07:49:02 -0700


Update of /cvsroot/python/python/dist/src/Mac/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv13794/d

Modified Files:
	aepack.py aetools.py aetypes.py 
Log Message:
Donovan Preston's patch #538395, with some mods by me.

This patch makes inheritance for OSA classes work. The implementation is a
bit convoluted, but I don't immedeately see a simpler way of doing it.

I added calls to ascii() everywhere we output strings that may contain
non-ascii characters (Python has gotten very picky since the encoding
patch:-).

I also removed Donovan's different way of opening resource files: I don't
seem to need it.


Index: aepack.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Lib/aepack.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** aepack.py	30 Mar 2002 23:44:58 -0000	1.4
--- aepack.py	7 Aug 2002 14:48:59 -0000	1.5
***************
*** 25,28 ****
--- 25,29 ----
  import aetypes
  from aetypes import mkenum, mktype
+ import os
  
  # These ones seem to be missing from AppleEvents
***************
*** 62,65 ****
--- 63,75 ----
  AliasType = macfs.AliasType
  
+ def packkey(ae, key, value):
+ 	if hasattr(key, 'which'):
+ 		keystr = key.which
+ 	elif hasattr(key, 'want'):
+ 		keystr = key.want
+ 	else:
+ 		keystr = key
+ 	ae.AEPutParamDesc(keystr, pack(value))
+ 
  def pack(x, forcetype = None):
  	"""Pack a python object into an AE descriptor"""
***************
*** 100,110 ****
  		record = AE.AECreateList('', 1)
  		for key, value in x.items():
! 			record.AEPutParamDesc(key, pack(value))
  		return record
  	if t == InstanceType and hasattr(x, '__aepack__'):
  		return x.__aepack__()
  	return AE.AECreateDesc('TEXT', repr(x)) # Copout
  
! def unpack(desc):
  	"""Unpack an AE descriptor to a python object"""
  	t = desc.type
--- 110,125 ----
  		record = AE.AECreateList('', 1)
  		for key, value in x.items():
! 			packkey(record, key, value)
! 			#record.AEPutParamDesc(key, pack(value))
  		return record
  	if t == InstanceType and hasattr(x, '__aepack__'):
  		return x.__aepack__()
+ 	if hasattr(x, 'which'):
+ 		return AE.AECreateDesc('TEXT', x.which)
+ 	if hasattr(x, 'want'):
+ 		return AE.AECreateDesc('TEXT', x.want)
  	return AE.AECreateDesc('TEXT', repr(x)) # Copout
  
! def unpack(desc, formodulename=""):
  	"""Unpack an AE descriptor to a python object"""
  	t = desc.type
***************
*** 118,122 ****
  		for i in range(desc.AECountItems()):
  			keyword, item = desc.AEGetNthDesc(i+1, '****')
! 			l.append(unpack(item))
  		return l
  	if t == typeAERecord:
--- 133,137 ----
  		for i in range(desc.AECountItems()):
  			keyword, item = desc.AEGetNthDesc(i+1, '****')
! 			l.append(unpack(item, formodulename))
  		return l
  	if t == typeAERecord:
***************
*** 124,132 ****
  		for i in range(desc.AECountItems()):
  			keyword, item = desc.AEGetNthDesc(i+1, '****')
! 			d[keyword] = unpack(item)
  		return d
  	if t == typeAEText:
  		record = desc.AECoerceDesc('reco')
! 		return mkaetext(unpack(record))
  	if t == typeAlias:
  		return macfs.RawAlias(desc.data)
--- 139,147 ----
  		for i in range(desc.AECountItems()):
  			keyword, item = desc.AEGetNthDesc(i+1, '****')
! 			d[keyword] = unpack(item, formodulename)
  		return d
  	if t == typeAEText:
  		record = desc.AECoerceDesc('reco')
! 		return mkaetext(unpack(record, formodulename))
  	if t == typeAlias:
  		return macfs.RawAlias(desc.data)
***************
*** 154,158 ****
  	if t == typeInsertionLoc:
  		record = desc.AECoerceDesc('reco')
! 		return mkinsertionloc(unpack(record))
  	# typeInteger equal to typeLongInteger
  	if t == typeIntlText:
--- 169,173 ----
  	if t == typeInsertionLoc:
  		record = desc.AECoerceDesc('reco')
! 		return mkinsertionloc(unpack(record, formodulename))
  	# typeInteger equal to typeLongInteger
  	if t == typeIntlText:
***************
*** 178,182 ****
  	if t == typeObjectSpecifier:
  		record = desc.AECoerceDesc('reco')
! 		return mkobject(unpack(record))
  	# typePict returned as unknown
  	# typePixelMap coerced to typeAERecord
--- 193,201 ----
  	if t == typeObjectSpecifier:
  		record = desc.AECoerceDesc('reco')
! 		# If we have been told the name of the module we are unpacking aedescs for,
! 		# we can attempt to create the right type of python object from that module.
! 		if formodulename:
! 			return mkobjectfrommodule(unpack(record, formodulename), formodulename)
! 		return mkobject(unpack(record, formodulename))
  	# typePict returned as unknown
  	# typePixelMap coerced to typeAERecord
***************
*** 215,225 ****
  	if t == 'rang':
  		record = desc.AECoerceDesc('reco')
! 		return mkrange(unpack(record))
  	if t == 'cmpd':
  		record = desc.AECoerceDesc('reco')
! 		return mkcomparison(unpack(record))
  	if t == 'logi':
  		record = desc.AECoerceDesc('reco')
! 		return mklogical(unpack(record))
  	return mkunknown(desc.type, desc.data)
  	
--- 234,244 ----
  	if t == 'rang':
  		record = desc.AECoerceDesc('reco')
! 		return mkrange(unpack(record, formodulename))
  	if t == 'cmpd':
  		record = desc.AECoerceDesc('reco')
! 		return mkcomparison(unpack(record, formodulename))
  	if t == 'logi':
  		record = desc.AECoerceDesc('reco')
! 		return mklogical(unpack(record, formodulename))
  	return mkunknown(desc.type, desc.data)
  	
***************
*** 311,314 ****
--- 330,347 ----
  		return aetypes.Property(seld.type, fr)
  	return aetypes.ObjectSpecifier(want, form, seld, fr)
+ 
+ # Note by Jack: I'm not 100% sure of the following code. This was
+ # provided by Donovan Preston, but I wonder whether the assignment
+ # to __class__ is safe. Moreover, shouldn't there be a better
+ # initializer for the classes in the suites?
+ def mkobjectfrommodule(dict, modulename):
+ 	want = dict['want'].type
+ 	module = __import__(modulename)
+ 	codenamemapper = module._classdeclarations
+ 	classtype = codenamemapper.get(want, None)
+ 	newobj = mkobject(dict)
+ 	if classtype:
+ 		newobj.__class__ = classtype
+ 	return newobj
  
  def _test():

Index: aetools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Lib/aetools.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** aetools.py	23 Jan 2002 22:46:30 -0000	1.3
--- aetools.py	7 Aug 2002 14:48:59 -0000	1.4
***************
*** 29,33 ****
  
  from aetypes import *
! from aepack import pack, unpack, coerce, AEDescType
  
  Error = 'aetools.Error'
--- 29,33 ----
  
  from aetypes import *
! from aepack import packkey, pack, unpack, coerce, AEDescType
  
  Error = 'aetools.Error'
***************
*** 57,61 ****
  	return desc.data
  
! def unpackevent(ae):
  	parameters = {}
  	try:
--- 57,61 ----
  	return desc.data
  
! def unpackevent(ae, formodulename=""):
  	parameters = {}
  	try:
***************
*** 64,73 ****
  		pass
  	else:
! 		parameters['----'] = unpack(dirobj)
  		del dirobj
  	while 1:
  		key = missed(ae)
  		if not key: break
! 		parameters[key] = unpack(ae.AEGetParamDesc(key, '****'))
  	attributes = {}
  	for key in aekeywords:
--- 64,73 ----
  		pass
  	else:
! 		parameters['----'] = unpack(dirobj, formodulename)
  		del dirobj
  	while 1:
  		key = missed(ae)
  		if not key: break
! 		parameters[key] = unpack(ae.AEGetParamDesc(key, '****'), formodulename)
  	attributes = {}
  	for key in aekeywords:
***************
*** 78,89 ****
  				raise sys.exc_type, sys.exc_value
  			continue
! 		attributes[key] = unpack(desc)
  	return parameters, attributes
  
  def packevent(ae, parameters = {}, attributes = {}):
  	for key, value in parameters.items():
! 		ae.AEPutParamDesc(key, pack(value))
  	for key, value in attributes.items():
! 		ae.AEPutAttributeDesc(key, pack(value))
  
  #
--- 78,89 ----
  				raise sys.exc_type, sys.exc_value
  			continue
! 		attributes[key] = unpack(desc, formodulename)
  	return parameters, attributes
  
  def packevent(ae, parameters = {}, attributes = {}):
  	for key, value in parameters.items():
! 		packkey(ae, key, value)
  	for key, value in attributes.items():
! 		packkey(ae, key, value)
  
  #
***************
*** 131,134 ****
--- 131,135 ----
  	"""An AE connection to an application"""
  	_signature = None	# Can be overridden by subclasses
+ 	_moduleName = None # Can be overridden by subclasses
  	
  	def __init__(self, signature=None, start=0, timeout=0):
***************
*** 184,188 ****
  		reply = event.AESend(self.send_flags, self.send_priority,
  		                          self.send_timeout)
! 		parameters, attributes = unpackevent(reply)
  		return reply, parameters, attributes
  		
--- 185,189 ----
  		reply = event.AESend(self.send_flags, self.send_priority,
  		                          self.send_timeout)
! 		parameters, attributes = unpackevent(reply, self._moduleName)
  		return reply, parameters, attributes
  		
***************
*** 211,214 ****
--- 212,238 ----
  		if as:
  			_arguments['rtyp'] = mktype(as)
+ 
+ 		_reply, _arguments, _attributes = self.send(_code, _subcode,
+ 				_arguments, _attributes)
+ 		if _arguments.has_key('errn'):
+ 			raise Error, decodeerror(_arguments)
+ 
+ 		if _arguments.has_key('----'):
+ 			return _arguments['----']
+ 			if as:
+ 				item.__class__ = as
+ 			return item
+ 
+ 	def _set(self, _object, _arguments = {}, _attributes = {}):
+ 		""" _set: set data for an object
+ 		Required argument: the object
+ 		Keyword argument _parameters: Parameter dictionary for the set operation
+ 		Keyword argument _attributes: AppleEvent attribute dictionary
+ 		Returns: the data
+ 		"""
+ 		_code = 'core'
+ 		_subcode = 'setd'
+ 		
+ 		_arguments['----'] = _object
  
  		_reply, _arguments, _attributes = self.send(_code, _subcode,

Index: aetypes.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Lib/aetypes.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** aetypes.py	25 Aug 2001 12:01:13 -0000	1.2
--- aetypes.py	7 Aug 2002 14:48:59 -0000	1.3
***************
*** 10,16 ****
  # aetools_convert.
  #
! def pack(*args):
  	from aepack import pack
! 	return apply(pack, args)
  	
  def IsSubclass(cls, base):
--- 10,16 ----
  # aetools_convert.
  #
! def pack(*args, **kwargs):
  	from aepack import pack
! 	return apply(pack, args, kwargs)
  	
  def IsSubclass(cls, base):
***************
*** 69,72 ****
--- 69,92 ----
  	if IsEnum(enum): return enum
  	return Enum(enum)
+ 
+ # Jack changed the way this is done
+ class InsertionLoc:
+ 	def __init__(self, of, pos):
+ 		self.of = of
+ 		self.pos = pos
+ 	
+ 	def __repr__(self):
+ 		return "InsertionLoc(%s, %s)" % (`self.of`, `self.pos`)
+ 		
+ 	def __aepack__(self):
+ 		rec = {'kobj': self.of, 'kpos': self.pos}
+ 		return pack(rec, forcetype='insl')
+ 		
+ # Convenience functions for dsp:
+ def beginning(of):
+ 	return InsertionLoc(of, Enum('bgng'))
+ 	
+ def end(of):
+ 	return InsertionLoc(of, Enum('end '))
  
  class Boolean: