📄 stafmarshalling.py
字号:
#############################################################################
# Software Testing Automation Framework (STAF) #
# (C) Copyright IBM Corp. 2004, 2005 #
# #
# This software is licensed under the Common Public License (CPL) V1.0. #
#############################################################################
# Marshalling constants and imports
import types
UNMARSHALLING_DEFAULTS = 0
IGNORE_INDIRECT_OBJECTS = 1
MARSHALLED_DATA_MARKER = '@SDT/'
NONE_MARKER = '@SDT/$0:0:'
SCALAR_MARKER = '@SDT/$'
SCALAR_STRING_MARKER = '@SDT/$S'
LIST_MARKER = '@SDT/['
MAP_MARKER = '@SDT/{'
MC_INSTANCE_MARKER = '@SDT/%'
CONTEXT_MARKER = '@SDT/*'
# Formatting constants and imports
import os
NONE_STRING = '<None>'
DISPLAY_NAME_KEY = 'display-name'
MAP_CLASS_MAP_KEY = 'map-class-map'
MAP_CLASS_NAME_KEY = 'staf-map-class-name'
ENTRY_SEPARATOR = ''
INDENT_DELTA = 2
# 80 spaces
SPACES = (' ' +
' ')
# STAFMapClassDefinitionClass
class STAFMapClassDefinition:
# Constructors
def __init__(self, name = None, mapClassDef = None):
if (mapClassDef is None) and (name is None):
self.mapClassDef = { 'name': '', 'keys': [] }
elif (name is not None):
self.mapClassDef = { 'name': name, 'keys': [] }
else:
self.mapClassDef = mapClassDef
def createInstance(self):
return { 'staf-map-class-name' : self.mapClassDef['name'] }
def addKey(self, keyName, displayName = None):
theKey = { 'key': keyName }
if displayName is not None:
theKey['display-name'] = displayName
self.mapClassDef['keys'].append(theKey)
def setKeyProperty(self, keyName, property, value):
for key in self.mapClassDef['keys']:
if key['key'] == keyName:
key[property] = value
def keys(self):
return self.mapClassDef['keys']
def name(self):
return self.mapClassDef['name']
def getMapClassDefinitionObject(self):
return self.mapClassDef
def __str__(self):
return formatObject(self.mapClassDef)
def __repr__(self):
return formatObject(self.mapClassDef)
# STAFMarshallingContext class
class STAFMarshallingContext:
def isMarshalledData(self, someData):
return someData.startswith('@SDT/')
def __init__(self, obj = None, mapClassMap = None):
if mapClassMap is None:
self.mapClassMap = {}
else:
self.mapClassMap = mapClassMap
self.rootObject = obj
def setMapClassDefinition(self, mapClassDef):
self.mapClassMap[mapClassDef.name()] = mapClassDef.getMapClassDefinitionObject()
def getMapClassDefinition(self, mapClassName):
return STAFMapClassDefinition(mapClassDef =
self.mapClassMap.get(mapClassName, None))
def hasMapClassDefinition(self, mapClassName):
return self.mapClassMap.has_key(mapClassName)
def getMapClassMap(self):
return self.mapClassMap
def mapClassDefinitionIterator(self):
return self.mapClassMap.keys()
def setRootObject(self, rootObject):
self.rootObject = rootObject
def getRootObject(self):
return self.rootObject
def getPrimaryObject(self):
if len(self.mapClassMap.keys()) == 0:
return self.rootObject
else:
return self
def marshall(self):
return marshall(self, self)
def __str__(self):
return formatObject(self.rootObject, self)
# XXX: Change to show the key map class in addition?
def __repr__(self):
return formatObject(self.rootObject, self)
# Function that tests if a string is marshalled data
def isMarshalledData(someData):
return someData.startswith('@SDT/')
# General marshalling function
def marshall(object, context = None):
if object is None:
return NONE_MARKER
if type(object) == types.ListType:
# Build a list of strings and join them for performance reasons
listDataList = []
for item in object:
listDataList.append(marshall(item, context))
listData = ''.join(listDataList)
return "%s%s:%s:%s" % (LIST_MARKER, len(object), len(listData), listData)
if type(object) == types.DictType:
# If a staf-map-class-name key exists in the map, make sure that
# it's map class definition is provided in the marshalling context.
# If it's not, then treat the map as a plain map object.
isMapClass = 0
mapClassName = ''
if ((context is not None) and
(isinstance(context, STAFMarshallingContext)) and
(object.has_key('staf-map-class-name'))):
mapClassName = object['staf-map-class-name']
if context.hasMapClassDefinition(mapClassName):
isMapClass = 1
if isMapClass:
mapClass = context.getMapClassDefinition(mapClassName)
# Build a list of strings and join them for performance reasons
mapDataList = []
mapDataList.append(":%s:%s" % (len(mapClassName), mapClassName))
for key in mapClass.keys():
if object.has_key(key['key']):
thisObj = object[key['key']]
else:
thisObj = None
mapDataList.append(marshall(thisObj, context))
mapData = ''.join(mapDataList)
return "%s:%s:%s" % (MC_INSTANCE_MARKER, len(mapData), mapData)
else:
# Build a list of strings and join them for performance reasons
mapDataList = []
for key in object.keys():
mapDataList.append(
":%s:%s%s" % (len(str(key)), str(key),
marshall(object[key], context)))
mapData = ''.join(mapDataList)
return "%s:%s:%s" % (MAP_MARKER, len(mapData), mapData)
if isinstance(object, STAFMarshallingContext):
if len(object.mapClassMap.keys()) == 0:
return marshall(object.getRootObject(), context)
else:
contextMap = { 'map-class-map': object.mapClassMap }
# Note: We can't simply put the root object as a map key like
# "root-object" and then marshall the whole map, as in
# the unmarshalling routines, we need to be able to
# unmarshall the root object in the context of the
# map-class-map.
mcData = marshall(contextMap, context) + \
marshall(object.getRootObject(), object)
return "%s:%s:%s" % (CONTEXT_MARKER, len(mcData), mcData)
# Check if object (such as a STAXGlobal object) has a callable method
# named stafMarshall.
if (hasattr(object, 'stafMarshall') and
callable(getattr(object, 'stafMarshall'))):
# Return the result from calling the stafMarshall method
return "%s" % (object.stafMarshall(context))
# Otherwise, return a string scalar
return "%s:%s:%s" % (SCALAR_STRING_MARKER, len(str(object)), str(object))
# General unmarshalling function
def unmarshall(data, context = None, flags = UNMARSHALLING_DEFAULTS):
if context is None:
context = STAFMarshallingContext()
if data.startswith(NONE_MARKER):
return STAFMarshallingContext()
if data.startswith(SCALAR_MARKER):
colonIndex = data.find(':')
colonIndex = data.find(':', colonIndex + 1)
theString = data[colonIndex + 1:]
if (theString.startswith(MARSHALLED_DATA_MARKER) and
((flags & IGNORE_INDIRECT_OBJECTS) != IGNORE_INDIRECT_OBJECTS)):
return unmarshall(theString, context, flags)
else:
return STAFMarshallingContext(data[colonIndex + 1:])
if data.startswith(LIST_MARKER):
colonIndex = data.find(':')
numItems = int(data[len(LIST_MARKER):colonIndex])
dataIndex = data.find(':', colonIndex + 1) + 1
list = []
for i in range(numItems):
colonIndex1 = data.find(':', dataIndex)
colonIndex2 = data.find(':', colonIndex1 + 1)
itemLength = int(data[colonIndex1 + 1:colonIndex2])
list.append(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -