📄 __init__.py
字号:
## Copyright (c) 2005, John Mettraux, OpenWFE.org# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met:# # . Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.# # . Neither the name of the "OpenWFE" nor the names of its contributors may be# used to endorse or promote products derived from this software without# specific prior written permission.# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE.## $Id: __init__.py 2514 2006-04-26 18:07:45Z jmettraux $#""" workitem python classes $Id: __init__.py 2514 2006-04-26 18:07:45Z jmettraux $"""import re#import base64from socket import getfqdnfrom openwfe.otime import currentTimeMillisfrom openwfe.utils import isString## for the helper methods at the end of this filefrom types import DictType, ListTypefrom types import StringType, UnicodeType, IntType, LongType, FloatType## for backward compatibilityfrom UserList import UserListfrom UserDict import UserDictfrom openwfe.xmlutils import getEncoding## ATTRIBUTES#class Attribute: passclass AtomicAttribute (Attribute): """ The base class for all simple (atomic) attributes. You shouldn't use it directly. """ value = None def __init__ (self, value): self.value = value def __hash__ (self): return hash(self.value) def __cmp__ (self, other): if other == None: return 1 return cmp(self.value, other.value)class StringAttribute (AtomicAttribute): passclass IntegerAttribute (AtomicAttribute): passclass LongAttribute (AtomicAttribute): passclass DoubleAttribute (AtomicAttribute): passclass BooleanAttribute (AtomicAttribute): passclass XmlAttribute (AtomicAttribute): pass#class XmlAttribute (AtomicAttribute):# def __init__ (self, value):# AtomicAttribute.__init__(self, value)# print 'XmlAttribute() value is %s' % str(value)class Base64Attribute (AtomicAttribute): pass#class Base64Attribute (AtomicAttribute):# def __init__ (self, value):# if isString(value):# self.value = base64.decodestring(value)# else:# AtomicAttribute.__init__(self, value)class CollectionAttribute (Attribute): passdef isAttribute (object): """ returns true if the object given as parameter is an OpenWFE attribute instance """ if isinstance(object, Attribute): return # the fast lane if isinstance(object, list): for o in object: isAttribute(o) return if isinstance(object, dict): for k in object.keys(): isAttribute(object[k]) return raise TypeError, "Object %s is not an instance of Attribute" % objectclass ListAttribute (UserList, CollectionAttribute): """ An attribute that is a list of attributes """ def __setitem__ (self, index, value): isAttribute(value) super(CollectionAttribute, self).__setitem__(index, value) def append (self, object): isAttribute(object) #super(ListAttribute, self).append(object) self.data.append(object) def __add__ (self, object): isAttribute(object) super(ListAttribute, self).__add__(object) def extend (self, object): isAttribute(object) super(ListAttribute, self).extend(object) def insert (self, index, object): isAttribute(object) #super(ListAttribute, self).insert(index, object) self.data.insert(index, object) def add (self, object): isAttribute(object) self.data.append(object) def get (self, index): return self[index]class MapAttribute (UserDict, CollectionAttribute): """ A dict that accepts any Attribute instance as key """ def checkKey(self, key): isAttribute(key) return key def __setitem__ (self, key, value): key = self.checkKey(key) isAttribute(value) #super(MapAttribute, self).__setitem__(key, value) #print "type(self.data) = %s" % type(self.data) self.data[key] = value def put (self, key, value): self.__setitem__(key, value) def setdefault (self, key, value=None): checkKey(key) if not value == None: isAttribute(value) super(MapAttribute, self).setdefault(key, value) def update (self, dic): for key in dic.keys(): checkKey(key) isAttribute(dic[key]) #super(MapAttribute, self).update(dic) self.data.update(dic) def remove (self, key): self.pop(key) def keySet (self): return self.keys()class StringMapAttribute (MapAttribute): """ A dict that only accepts StringAttribute instances as keys """ def checkKey (self, key): #print 'type of key is >%s<' % type(key) #if isinstance(key, str): if isString(key): return StringAttribute(key) if isinstance(key, StringAttribute): return key raise TypeError, "Key must be of type str or StringAttribute" def puts (self, stringKey, stringValue): """ a put that takes as input 2 strings (instead of 2 attributes) """ k = self.checkKey(stringKey) v = self.checkKey(stringValue) self.put(k, v) def gets (self, stringKey): v = self.get(self.checkKey(stringKey)) if not v: return None #return str(v.value) return v.value## HEADER#class Header: """ Worksession when connected to a Worklist may return you a list of headers. A Header is a 'summary' of a workitem. It is usually built following the rules edicted in owfe/etc-worklist/header-factory.xml """ def __init__ (self): self.flowExpressionId = None self.attributes = None self.lastModified = None self.locked = 0 def __cmp__ (self, other): return self.lastModified.__cmp__(other.lastModified) def sget (self, sKey): """ given a string key (attribute name), returns the correspoding header attribute value, as a string. """ aVal = self.attributes.get(StringAttribute(sKey)) if not aVal: return None return str(aVal.value)## WORKITEMS#class Workitem: """ The base class for the two main workitem classes : Launchitem and InFlowWorkitem. Do not use it directly. """ #workflowDefinitionUrl = None lastModified = None flowStack = [] attributes = None def setAttribute (self, attributeName, attributeValue): """ Sets an attribute of the workitem. It makes sure that attributeName and attributeValue are converted to OpenWFE attributes """ if self.attributes == None: self.attributes = StringMapAttribute() #if not isinstance(attributeValue, Attribute): # attributeValue = StringAttribute(str(attributeValue)) # #if isinstance(attributeName, StringAttribute): # self.attributes[attributeName] = attributeValue #else: # self.attributes[StringAttribute(str(attributeName))] = attributeValue key = toStringAttribute(attributeName) value = py2owfe(attributeValue) self.attributes[key] = value def getAttribute (self, attributeName): """ returns the attribute stored a given attributeName. If there is no attribute under this name, None is returned. attributeName may either be a String or a StringAttribute. If it is of another class, its str() value will be used to find the potential attribute """ if self.attributes == None: return None if isString(attributeName): key = StringAttribute(attributeName) elif isinstance(attributeName, Attribute): key = attributeName else: key = StringAttribute(str(attributeName)) #return self.attributes[key] return self.attributes.get(key) #for a in self.attributes.keys(): # aName = str(a.value) # if aName == attributeName: # return self.attributes[a] def removeAttribute (self, attributeName): """ removes an attributes from the workitem attribute dict """ if self.attributes == None: return for k in self.attributes.keys(): if str(k.value) == attributeName: del self.attributes[k] returnclass Launchitem (Workitem): """ A Launchitem is used to launch a flow """ def __init__ (self): self.workflowDefinitionUrl = None self.descriptionMap = Noneclass Cancelitem (Workitem): """ You shouldn't have to directly use this kind of item. Its a cancellation message adressed to a worklist from an engine. """ def __init__ (self): self.lastExpressionId = None self.participantName = Noneclass InFlowWorkitem (Cancelitem): """ An InFlowWorkitem is a workitem that travels inside an instantiated workflow """ def __init__ (self): self.dispatchTime = None self.filter = None self.history = None def addHistoryItem (self, author, text): """ Adds a piece of history to the InFlowWorkitem. This method is usually called by reactor agents and the like. """ hi = HistoryItem()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -