/usr/share/pyshared/ZSI/generate/pyclass.py is in python-zsi 2.1~a1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | ############################################################################
# Joshua R. Boverhof, LBNL
# See LBNLCopyright for copyright notice!
###########################################################################
import pydoc, sys, warnings
from ZSI import TC
# If function.__name__ is read-only, fail
def _x(): return
try:
_x.func_name = '_y'
except:
raise RuntimeError,\
'use python-2.4 or later, cannot set function names in python "%s"'\
%sys.version
del _x
#def GetNil(typecode=None):
# """returns the nilled element, use to set an element
# as nilled for immutable instance.
# """
#
# nil = TC.Nilled()
# if typecode is not None: nil.typecode = typecode
# return nil
#
#
#def GetNilAsSelf(cls, typecode=None):
# """returns the nilled element with typecode specified,
# use returned instance to set this element as nilled.
#
# Key Word Parameters:
# typecode -- use to specify a derived type or wildcard as nilled.
# """
# if typecode is not None and not isinstance(typecode, TC.TypeCode):
# raise TypeError, "Expecting a TypeCode instance"
#
# nil = TC.Nilled()
# nil.typecode = typecode or cls.typecode
# return nil
class pyclass_type(type):
"""Stability: Unstable
type for pyclasses used with typecodes. expects the typecode to
be available in the classdict. creates python properties for accessing
and setting the elements specified in the ofwhat list, and factory methods
for constructing the elements.
Known Limitations:
1)Uses XML Schema element names directly to create method names,
using characters in this set will cause Syntax Errors:
(NCNAME)-(letter U digit U "_")
"""
def __new__(cls, classname, bases, classdict):
"""
"""
#import new
typecode = classdict.get('typecode')
assert typecode is not None, 'MUST HAVE A TYPECODE.'
# Assume this means immutable type. ie. str
if len(bases) > 0:
#classdict['new_Nill'] = classmethod(GetNilAsSelf)
pass
# Assume this means mutable type. ie. ofwhat.
else:
assert hasattr(typecode, 'ofwhat'), 'typecode has no ofwhat list??'
assert hasattr(typecode, 'attribute_typecode_dict'),\
'typecode has no attribute_typecode_dict??'
#classdict['new_Nill'] = staticmethod(GetNil)
if typecode.mixed:
get,set = cls.__create_text_functions_from_what(typecode)
if classdict.has_key(get.__name__):
raise AttributeError,\
'attribute %s previously defined.' %get.__name__
if classdict.has_key(set.__name__):
raise AttributeError,\
'attribute %s previously defined.' %set.__name__
classdict[get.__name__] = get
classdict[set.__name__] = set
for what in typecode.ofwhat:
get,set,new_func = cls.__create_functions_from_what(what)
if classdict.has_key(get.__name__):
raise AttributeError,\
'attribute %s previously defined.' %get.__name__
classdict[get.__name__] = get
if classdict.has_key(set.__name__):
raise AttributeError,\
'attribute %s previously defined.' %set.__name__
classdict[set.__name__] = set
if new_func is not None:
if classdict.has_key(new_func.__name__):
raise AttributeError,\
'attribute %s previously defined.' %new_func.__name__
classdict[new_func.__name__] = new_func
assert not classdict.has_key(what.pname),\
'collision with pname="%s", bail..' %what.pname
pname = what.pname
if pname is None and isinstance(what, TC.AnyElement): pname = 'any'
assert pname is not None, 'Element with no name: %s' %what
# TODO: for pname if keyword just uppercase first letter.
#if pydoc.Helper.keywords.has_key(pname):
pname = pname[0].upper() + pname[1:]
assert not pydoc.Helper.keywords.has_key(pname), 'unexpected keyword: %s' %pname
classdict[pname] =\
property(get, set, None,
'property for element (%s,%s), minOccurs="%s" maxOccurs="%s" nillable="%s"'\
%(what.nspname,what.pname,what.minOccurs,what.maxOccurs,what.nillable)
)
#
# mutable type <complexType> complexContent | modelGroup
# or immutable type <complexType> simpleContent (float, str, etc)
#
if hasattr(typecode, 'attribute_typecode_dict'):
attribute_typecode_dict = typecode.attribute_typecode_dict or {}
for key,what in attribute_typecode_dict.items():
get,set = cls.__create_attr_functions_from_what(key, what)
if classdict.has_key(get.__name__):
raise AttributeError,\
'attribute %s previously defined.' %get.__name__
if classdict.has_key(set.__name__):
raise AttributeError,\
'attribute %s previously defined.' %set.__name__
classdict[get.__name__] = get
classdict[set.__name__] = set
return type.__new__(cls,classname,bases,classdict)
def __create_functions_from_what(what):
if not callable(what):
def get(self):
return getattr(self, what.aname)
if what.maxOccurs > 1:
def set(self, value):
if not (value is None or hasattr(value, '__iter__')):
raise TypeError, 'expecting an iterable instance'
setattr(self, what.aname, value)
else:
def set(self, value):
setattr(self, what.aname, value)
else:
def get(self):
return getattr(self, what().aname)
if what.maxOccurs > 1:
def set(self, value):
if not (value is None or hasattr(value, '__iter__')):
raise TypeError, 'expecting an iterable instance'
setattr(self, what().aname, value)
else:
def set(self, value):
setattr(self, what().aname, value)
#
# new factory function
# if pyclass is None, skip
#
if not callable(what) and getattr(what, 'pyclass', None) is None:
new_func = None
elif (isinstance(what, TC.ComplexType) or
isinstance(what, TC.Array)):
def new_func(self):
'''returns a mutable type
'''
return what.pyclass()
elif not callable(what):
def new_func(self, value):
'''value -- initialize value
returns an immutable type
'''
return what.pyclass(value)
elif (issubclass(what.klass, TC.ComplexType) or
issubclass(what.klass, TC.Array)):
def new_func(self):
'''returns a mutable type or None (if no pyclass).
'''
p = what().pyclass
if p is None: return
return p()
else:
def new_func(self, value=None):
'''if simpleType provide initialization value, else
if complexType value should be left as None.
Parameters:
value -- initialize value or None
returns a mutable instance (value is None)
or an immutable instance or None (if no pyclass)
'''
p = what().pyclass
if p is None: return
if value is None: return p()
return p(value)
#TODO: sub all illegal characters in set
# (NCNAME)-(letter U digit U "_")
if new_func is not None:
new_func.__name__ = 'new_%s' %what.pname
get.func_name = 'get_element_%s' %what.pname
set.func_name = 'set_element_%s' %what.pname
return get,set,new_func
__create_functions_from_what = staticmethod(__create_functions_from_what)
def __create_attr_functions_from_what(key, what):
def get(self):
'''returns attribute value for attribute %s, else None.
''' %str(key)
return getattr(self, what.attrs_aname, {}).get(key, None)
def set(self, value):
'''set value for attribute %s.
value -- initialize value, immutable type
''' %str(key)
if not hasattr(self, what.attrs_aname):
setattr(self, what.attrs_aname, {})
getattr(self, what.attrs_aname)[key] = value
#TODO: sub all illegal characters in set
# (NCNAME)-(letter U digit U "_")
if type(key) in (tuple, list):
get.__name__ = 'get_attribute_%s' %key[1]
set.__name__ = 'set_attribute_%s' %key[1]
else:
get.__name__ = 'get_attribute_%s' %key
set.__name__ = 'set_attribute_%s' %key
return get,set
__create_attr_functions_from_what = \
staticmethod(__create_attr_functions_from_what)
def __create_text_functions_from_what(what):
def get(self):
'''returns text content, else None.
'''
return getattr(self, what.mixed_aname, None)
get.im_func = 'get_text'
def set(self, value):
'''set text content.
value -- initialize value, immutable type
'''
setattr(self, what.mixed_aname, value)
get.im_func = 'set_text'
return get,set
__create_text_functions_from_what = \
staticmethod(__create_text_functions_from_what)
|