| 1 | n/a | """aetypes - Python objects representing various AE types.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from warnings import warnpy3k |
|---|
| 4 | n/a | warnpy3k("In 3.x, the aetypes module is removed.", stacklevel=2) |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | from Carbon.AppleEvents import * |
|---|
| 7 | n/a | import struct |
|---|
| 8 | n/a | from types import * |
|---|
| 9 | n/a | import string |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | # |
|---|
| 12 | n/a | # convoluted, since there are cyclic dependencies between this file and |
|---|
| 13 | n/a | # aetools_convert. |
|---|
| 14 | n/a | # |
|---|
| 15 | n/a | def pack(*args, **kwargs): |
|---|
| 16 | n/a | from aepack import pack |
|---|
| 17 | n/a | return pack( *args, **kwargs) |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def nice(s): |
|---|
| 20 | n/a | """'nice' representation of an object""" |
|---|
| 21 | n/a | if type(s) is StringType: return repr(s) |
|---|
| 22 | n/a | else: return str(s) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | class Unknown: |
|---|
| 25 | n/a | """An uninterpreted AE object""" |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def __init__(self, type, data): |
|---|
| 28 | n/a | self.type = type |
|---|
| 29 | n/a | self.data = data |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def __repr__(self): |
|---|
| 32 | n/a | return "Unknown(%r, %r)" % (self.type, self.data) |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def __aepack__(self): |
|---|
| 35 | n/a | return pack(self.data, self.type) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | class Enum: |
|---|
| 38 | n/a | """An AE enumeration value""" |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def __init__(self, enum): |
|---|
| 41 | n/a | self.enum = "%-4.4s" % str(enum) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def __repr__(self): |
|---|
| 44 | n/a | return "Enum(%r)" % (self.enum,) |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def __str__(self): |
|---|
| 47 | n/a | return string.strip(self.enum) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def __aepack__(self): |
|---|
| 50 | n/a | return pack(self.enum, typeEnumeration) |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def IsEnum(x): |
|---|
| 53 | n/a | return isinstance(x, Enum) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def mkenum(enum): |
|---|
| 56 | n/a | if IsEnum(enum): return enum |
|---|
| 57 | n/a | return Enum(enum) |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | # Jack changed the way this is done |
|---|
| 60 | n/a | class InsertionLoc: |
|---|
| 61 | n/a | def __init__(self, of, pos): |
|---|
| 62 | n/a | self.of = of |
|---|
| 63 | n/a | self.pos = pos |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def __repr__(self): |
|---|
| 66 | n/a | return "InsertionLoc(%r, %r)" % (self.of, self.pos) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def __aepack__(self): |
|---|
| 69 | n/a | rec = {'kobj': self.of, 'kpos': self.pos} |
|---|
| 70 | n/a | return pack(rec, forcetype='insl') |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | # Convenience functions for dsp: |
|---|
| 73 | n/a | def beginning(of): |
|---|
| 74 | n/a | return InsertionLoc(of, Enum('bgng')) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def end(of): |
|---|
| 77 | n/a | return InsertionLoc(of, Enum('end ')) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | class Boolean: |
|---|
| 80 | n/a | """An AE boolean value""" |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def __init__(self, bool): |
|---|
| 83 | n/a | self.bool = (not not bool) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def __repr__(self): |
|---|
| 86 | n/a | return "Boolean(%r)" % (self.bool,) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | def __str__(self): |
|---|
| 89 | n/a | if self.bool: |
|---|
| 90 | n/a | return "True" |
|---|
| 91 | n/a | else: |
|---|
| 92 | n/a | return "False" |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | def __aepack__(self): |
|---|
| 95 | n/a | return pack(struct.pack('b', self.bool), 'bool') |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def IsBoolean(x): |
|---|
| 98 | n/a | return isinstance(x, Boolean) |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | def mkboolean(bool): |
|---|
| 101 | n/a | if IsBoolean(bool): return bool |
|---|
| 102 | n/a | return Boolean(bool) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | class Type: |
|---|
| 105 | n/a | """An AE 4-char typename object""" |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | def __init__(self, type): |
|---|
| 108 | n/a | self.type = "%-4.4s" % str(type) |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def __repr__(self): |
|---|
| 111 | n/a | return "Type(%r)" % (self.type,) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | def __str__(self): |
|---|
| 114 | n/a | return string.strip(self.type) |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def __aepack__(self): |
|---|
| 117 | n/a | return pack(self.type, typeType) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | def IsType(x): |
|---|
| 120 | n/a | return isinstance(x, Type) |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | def mktype(type): |
|---|
| 123 | n/a | if IsType(type): return type |
|---|
| 124 | n/a | return Type(type) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | class Keyword: |
|---|
| 128 | n/a | """An AE 4-char keyword object""" |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def __init__(self, keyword): |
|---|
| 131 | n/a | self.keyword = "%-4.4s" % str(keyword) |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def __repr__(self): |
|---|
| 134 | n/a | return "Keyword(%r)" % repr(self.keyword) |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def __str__(self): |
|---|
| 137 | n/a | return string.strip(self.keyword) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def __aepack__(self): |
|---|
| 140 | n/a | return pack(self.keyword, typeKeyword) |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | def IsKeyword(x): |
|---|
| 143 | n/a | return isinstance(x, Keyword) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | class Range: |
|---|
| 146 | n/a | """An AE range object""" |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | def __init__(self, start, stop): |
|---|
| 149 | n/a | self.start = start |
|---|
| 150 | n/a | self.stop = stop |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | def __repr__(self): |
|---|
| 153 | n/a | return "Range(%r, %r)" % (self.start, self.stop) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | def __str__(self): |
|---|
| 156 | n/a | return "%s thru %s" % (nice(self.start), nice(self.stop)) |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | def __aepack__(self): |
|---|
| 159 | n/a | return pack({'star': self.start, 'stop': self.stop}, 'rang') |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | def IsRange(x): |
|---|
| 162 | n/a | return isinstance(x, Range) |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | class Comparison: |
|---|
| 165 | n/a | """An AE Comparison""" |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def __init__(self, obj1, relo, obj2): |
|---|
| 168 | n/a | self.obj1 = obj1 |
|---|
| 169 | n/a | self.relo = "%-4.4s" % str(relo) |
|---|
| 170 | n/a | self.obj2 = obj2 |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def __repr__(self): |
|---|
| 173 | n/a | return "Comparison(%r, %r, %r)" % (self.obj1, self.relo, self.obj2) |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def __str__(self): |
|---|
| 176 | n/a | return "%s %s %s" % (nice(self.obj1), string.strip(self.relo), nice(self.obj2)) |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | def __aepack__(self): |
|---|
| 179 | n/a | return pack({'obj1': self.obj1, |
|---|
| 180 | n/a | 'relo': mkenum(self.relo), |
|---|
| 181 | n/a | 'obj2': self.obj2}, |
|---|
| 182 | n/a | 'cmpd') |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def IsComparison(x): |
|---|
| 185 | n/a | return isinstance(x, Comparison) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | class NComparison(Comparison): |
|---|
| 188 | n/a | # The class attribute 'relo' must be set in a subclass |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def __init__(self, obj1, obj2): |
|---|
| 191 | n/a | Comparison.__init__(obj1, self.relo, obj2) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | class Ordinal: |
|---|
| 194 | n/a | """An AE Ordinal""" |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def __init__(self, abso): |
|---|
| 197 | n/a | # self.obj1 = obj1 |
|---|
| 198 | n/a | self.abso = "%-4.4s" % str(abso) |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | def __repr__(self): |
|---|
| 201 | n/a | return "Ordinal(%r)" % (self.abso,) |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | def __str__(self): |
|---|
| 204 | n/a | return "%s" % (string.strip(self.abso)) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | def __aepack__(self): |
|---|
| 207 | n/a | return pack(self.abso, 'abso') |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | def IsOrdinal(x): |
|---|
| 210 | n/a | return isinstance(x, Ordinal) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | class NOrdinal(Ordinal): |
|---|
| 213 | n/a | # The class attribute 'abso' must be set in a subclass |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def __init__(self): |
|---|
| 216 | n/a | Ordinal.__init__(self, self.abso) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | class Logical: |
|---|
| 219 | n/a | """An AE logical expression object""" |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def __init__(self, logc, term): |
|---|
| 222 | n/a | self.logc = "%-4.4s" % str(logc) |
|---|
| 223 | n/a | self.term = term |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | def __repr__(self): |
|---|
| 226 | n/a | return "Logical(%r, %r)" % (self.logc, self.term) |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def __str__(self): |
|---|
| 229 | n/a | if type(self.term) == ListType and len(self.term) == 2: |
|---|
| 230 | n/a | return "%s %s %s" % (nice(self.term[0]), |
|---|
| 231 | n/a | string.strip(self.logc), |
|---|
| 232 | n/a | nice(self.term[1])) |
|---|
| 233 | n/a | else: |
|---|
| 234 | n/a | return "%s(%s)" % (string.strip(self.logc), nice(self.term)) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | def __aepack__(self): |
|---|
| 237 | n/a | return pack({'logc': mkenum(self.logc), 'term': self.term}, 'logi') |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | def IsLogical(x): |
|---|
| 240 | n/a | return isinstance(x, Logical) |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | class StyledText: |
|---|
| 243 | n/a | """An AE object respresenting text in a certain style""" |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def __init__(self, style, text): |
|---|
| 246 | n/a | self.style = style |
|---|
| 247 | n/a | self.text = text |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | def __repr__(self): |
|---|
| 250 | n/a | return "StyledText(%r, %r)" % (self.style, self.text) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | def __str__(self): |
|---|
| 253 | n/a | return self.text |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def __aepack__(self): |
|---|
| 256 | n/a | return pack({'ksty': self.style, 'ktxt': self.text}, 'STXT') |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | def IsStyledText(x): |
|---|
| 259 | n/a | return isinstance(x, StyledText) |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | class AEText: |
|---|
| 262 | n/a | """An AE text object with style, script and language specified""" |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | def __init__(self, script, style, text): |
|---|
| 265 | n/a | self.script = script |
|---|
| 266 | n/a | self.style = style |
|---|
| 267 | n/a | self.text = text |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def __repr__(self): |
|---|
| 270 | n/a | return "AEText(%r, %r, %r)" % (self.script, self.style, self.text) |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | def __str__(self): |
|---|
| 273 | n/a | return self.text |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | def __aepack__(self): |
|---|
| 276 | n/a | return pack({keyAEScriptTag: self.script, keyAEStyles: self.style, |
|---|
| 277 | n/a | keyAEText: self.text}, typeAEText) |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | def IsAEText(x): |
|---|
| 280 | n/a | return isinstance(x, AEText) |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | class IntlText: |
|---|
| 283 | n/a | """A text object with script and language specified""" |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def __init__(self, script, language, text): |
|---|
| 286 | n/a | self.script = script |
|---|
| 287 | n/a | self.language = language |
|---|
| 288 | n/a | self.text = text |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | def __repr__(self): |
|---|
| 291 | n/a | return "IntlText(%r, %r, %r)" % (self.script, self.language, self.text) |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | def __str__(self): |
|---|
| 294 | n/a | return self.text |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | def __aepack__(self): |
|---|
| 297 | n/a | return pack(struct.pack('hh', self.script, self.language)+self.text, |
|---|
| 298 | n/a | typeIntlText) |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | def IsIntlText(x): |
|---|
| 301 | n/a | return isinstance(x, IntlText) |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | class IntlWritingCode: |
|---|
| 304 | n/a | """An object representing script and language""" |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def __init__(self, script, language): |
|---|
| 307 | n/a | self.script = script |
|---|
| 308 | n/a | self.language = language |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | def __repr__(self): |
|---|
| 311 | n/a | return "IntlWritingCode(%r, %r)" % (self.script, self.language) |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | def __str__(self): |
|---|
| 314 | n/a | return "script system %d, language %d"%(self.script, self.language) |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | def __aepack__(self): |
|---|
| 317 | n/a | return pack(struct.pack('hh', self.script, self.language), |
|---|
| 318 | n/a | typeIntlWritingCode) |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | def IsIntlWritingCode(x): |
|---|
| 321 | n/a | return isinstance(x, IntlWritingCode) |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | class QDPoint: |
|---|
| 324 | n/a | """A point""" |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | def __init__(self, v, h): |
|---|
| 327 | n/a | self.v = v |
|---|
| 328 | n/a | self.h = h |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | def __repr__(self): |
|---|
| 331 | n/a | return "QDPoint(%r, %r)" % (self.v, self.h) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | def __str__(self): |
|---|
| 334 | n/a | return "(%d, %d)"%(self.v, self.h) |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | def __aepack__(self): |
|---|
| 337 | n/a | return pack(struct.pack('hh', self.v, self.h), |
|---|
| 338 | n/a | typeQDPoint) |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | def IsQDPoint(x): |
|---|
| 341 | n/a | return isinstance(x, QDPoint) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | class QDRectangle: |
|---|
| 344 | n/a | """A rectangle""" |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def __init__(self, v0, h0, v1, h1): |
|---|
| 347 | n/a | self.v0 = v0 |
|---|
| 348 | n/a | self.h0 = h0 |
|---|
| 349 | n/a | self.v1 = v1 |
|---|
| 350 | n/a | self.h1 = h1 |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | def __repr__(self): |
|---|
| 353 | n/a | return "QDRectangle(%r, %r, %r, %r)" % (self.v0, self.h0, self.v1, self.h1) |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | def __str__(self): |
|---|
| 356 | n/a | return "(%d, %d)-(%d, %d)"%(self.v0, self.h0, self.v1, self.h1) |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | def __aepack__(self): |
|---|
| 359 | n/a | return pack(struct.pack('hhhh', self.v0, self.h0, self.v1, self.h1), |
|---|
| 360 | n/a | typeQDRectangle) |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | def IsQDRectangle(x): |
|---|
| 363 | n/a | return isinstance(x, QDRectangle) |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | class RGBColor: |
|---|
| 366 | n/a | """An RGB color""" |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | def __init__(self, r, g, b): |
|---|
| 369 | n/a | self.r = r |
|---|
| 370 | n/a | self.g = g |
|---|
| 371 | n/a | self.b = b |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | def __repr__(self): |
|---|
| 374 | n/a | return "RGBColor(%r, %r, %r)" % (self.r, self.g, self.b) |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | def __str__(self): |
|---|
| 377 | n/a | return "0x%x red, 0x%x green, 0x%x blue"% (self.r, self.g, self.b) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | def __aepack__(self): |
|---|
| 380 | n/a | return pack(struct.pack('hhh', self.r, self.g, self.b), |
|---|
| 381 | n/a | typeRGBColor) |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | def IsRGBColor(x): |
|---|
| 384 | n/a | return isinstance(x, RGBColor) |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | class ObjectSpecifier: |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | """A class for constructing and manipulation AE object specifiers in python. |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | An object specifier is actually a record with four fields: |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | key type description |
|---|
| 393 | n/a | --- ---- ----------- |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | 'want' type 4-char class code of thing we want, |
|---|
| 396 | n/a | e.g. word, paragraph or property |
|---|
| 397 | n/a | |
|---|
| 398 | n/a | 'form' enum how we specify which 'want' thing(s) we want, |
|---|
| 399 | n/a | e.g. by index, by range, by name, or by property specifier |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | 'seld' any which thing(s) we want, |
|---|
| 402 | n/a | e.g. its index, its name, or its property specifier |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | 'from' object the object in which it is contained, |
|---|
| 405 | n/a | or null, meaning look for it in the application |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | Note that we don't call this class plain "Object", since that name |
|---|
| 408 | n/a | is likely to be used by the application. |
|---|
| 409 | n/a | """ |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | def __init__(self, want, form, seld, fr = None): |
|---|
| 412 | n/a | self.want = want |
|---|
| 413 | n/a | self.form = form |
|---|
| 414 | n/a | self.seld = seld |
|---|
| 415 | n/a | self.fr = fr |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | def __repr__(self): |
|---|
| 418 | n/a | s = "ObjectSpecifier(%r, %r, %r" % (self.want, self.form, self.seld) |
|---|
| 419 | n/a | if self.fr: |
|---|
| 420 | n/a | s = s + ", %r)" % (self.fr,) |
|---|
| 421 | n/a | else: |
|---|
| 422 | n/a | s = s + ")" |
|---|
| 423 | n/a | return s |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | def __aepack__(self): |
|---|
| 426 | n/a | return pack({'want': mktype(self.want), |
|---|
| 427 | n/a | 'form': mkenum(self.form), |
|---|
| 428 | n/a | 'seld': self.seld, |
|---|
| 429 | n/a | 'from': self.fr}, |
|---|
| 430 | n/a | 'obj ') |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | def IsObjectSpecifier(x): |
|---|
| 433 | n/a | return isinstance(x, ObjectSpecifier) |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | # Backwards compatibility, sigh... |
|---|
| 437 | n/a | class Property(ObjectSpecifier): |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | def __init__(self, which, fr = None, want='prop'): |
|---|
| 440 | n/a | ObjectSpecifier.__init__(self, want, 'prop', mktype(which), fr) |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | def __repr__(self): |
|---|
| 443 | n/a | if self.fr: |
|---|
| 444 | n/a | return "Property(%r, %r)" % (self.seld.type, self.fr) |
|---|
| 445 | n/a | else: |
|---|
| 446 | n/a | return "Property(%r)" % (self.seld.type,) |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | def __str__(self): |
|---|
| 449 | n/a | if self.fr: |
|---|
| 450 | n/a | return "Property %s of %s" % (str(self.seld), str(self.fr)) |
|---|
| 451 | n/a | else: |
|---|
| 452 | n/a | return "Property %s" % str(self.seld) |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | class NProperty(ObjectSpecifier): |
|---|
| 456 | n/a | # Subclasses *must* self baseclass attributes: |
|---|
| 457 | n/a | # want is the type of this property |
|---|
| 458 | n/a | # which is the property name of this property |
|---|
| 459 | n/a | |
|---|
| 460 | n/a | def __init__(self, fr = None): |
|---|
| 461 | n/a | #try: |
|---|
| 462 | n/a | # dummy = self.want |
|---|
| 463 | n/a | #except: |
|---|
| 464 | n/a | # self.want = 'prop' |
|---|
| 465 | n/a | self.want = 'prop' |
|---|
| 466 | n/a | ObjectSpecifier.__init__(self, self.want, 'prop', |
|---|
| 467 | n/a | mktype(self.which), fr) |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | def __repr__(self): |
|---|
| 470 | n/a | rv = "Property(%r" % (self.seld.type,) |
|---|
| 471 | n/a | if self.fr: |
|---|
| 472 | n/a | rv = rv + ", fr=%r" % (self.fr,) |
|---|
| 473 | n/a | if self.want != 'prop': |
|---|
| 474 | n/a | rv = rv + ", want=%r" % (self.want,) |
|---|
| 475 | n/a | return rv + ")" |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | def __str__(self): |
|---|
| 478 | n/a | if self.fr: |
|---|
| 479 | n/a | return "Property %s of %s" % (str(self.seld), str(self.fr)) |
|---|
| 480 | n/a | else: |
|---|
| 481 | n/a | return "Property %s" % str(self.seld) |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | class SelectableItem(ObjectSpecifier): |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | def __init__(self, want, seld, fr = None): |
|---|
| 487 | n/a | t = type(seld) |
|---|
| 488 | n/a | if t == StringType: |
|---|
| 489 | n/a | form = 'name' |
|---|
| 490 | n/a | elif IsRange(seld): |
|---|
| 491 | n/a | form = 'rang' |
|---|
| 492 | n/a | elif IsComparison(seld) or IsLogical(seld): |
|---|
| 493 | n/a | form = 'test' |
|---|
| 494 | n/a | elif t == TupleType: |
|---|
| 495 | n/a | # Breakout: specify both form and seld in a tuple |
|---|
| 496 | n/a | # (if you want ID or rele or somesuch) |
|---|
| 497 | n/a | form, seld = seld |
|---|
| 498 | n/a | else: |
|---|
| 499 | n/a | form = 'indx' |
|---|
| 500 | n/a | ObjectSpecifier.__init__(self, want, form, seld, fr) |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | class ComponentItem(SelectableItem): |
|---|
| 504 | n/a | # Derived classes *must* set the *class attribute* 'want' to some constant |
|---|
| 505 | n/a | # Also, dictionaries _propdict and _elemdict must be set to map property |
|---|
| 506 | n/a | # and element names to the correct classes |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | _propdict = {} |
|---|
| 509 | n/a | _elemdict = {} |
|---|
| 510 | n/a | def __init__(self, which, fr = None): |
|---|
| 511 | n/a | SelectableItem.__init__(self, self.want, which, fr) |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | def __repr__(self): |
|---|
| 514 | n/a | if not self.fr: |
|---|
| 515 | n/a | return "%s(%r)" % (self.__class__.__name__, self.seld) |
|---|
| 516 | n/a | return "%s(%r, %r)" % (self.__class__.__name__, self.seld, self.fr) |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | def __str__(self): |
|---|
| 519 | n/a | seld = self.seld |
|---|
| 520 | n/a | if type(seld) == StringType: |
|---|
| 521 | n/a | ss = repr(seld) |
|---|
| 522 | n/a | elif IsRange(seld): |
|---|
| 523 | n/a | start, stop = seld.start, seld.stop |
|---|
| 524 | n/a | if type(start) == InstanceType == type(stop) and \ |
|---|
| 525 | n/a | start.__class__ == self.__class__ == stop.__class__: |
|---|
| 526 | n/a | ss = str(start.seld) + " thru " + str(stop.seld) |
|---|
| 527 | n/a | else: |
|---|
| 528 | n/a | ss = str(seld) |
|---|
| 529 | n/a | else: |
|---|
| 530 | n/a | ss = str(seld) |
|---|
| 531 | n/a | s = "%s %s" % (self.__class__.__name__, ss) |
|---|
| 532 | n/a | if self.fr: s = s + " of %s" % str(self.fr) |
|---|
| 533 | n/a | return s |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | def __getattr__(self, name): |
|---|
| 536 | n/a | if name in self._elemdict: |
|---|
| 537 | n/a | cls = self._elemdict[name] |
|---|
| 538 | n/a | return DelayedComponentItem(cls, self) |
|---|
| 539 | n/a | if name in self._propdict: |
|---|
| 540 | n/a | cls = self._propdict[name] |
|---|
| 541 | n/a | return cls(self) |
|---|
| 542 | n/a | raise AttributeError, name |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | class DelayedComponentItem: |
|---|
| 546 | n/a | def __init__(self, compclass, fr): |
|---|
| 547 | n/a | self.compclass = compclass |
|---|
| 548 | n/a | self.fr = fr |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | def __call__(self, which): |
|---|
| 551 | n/a | return self.compclass(which, self.fr) |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | def __repr__(self): |
|---|
| 554 | n/a | return "%s(???, %r)" % (self.__class__.__name__, self.fr) |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | def __str__(self): |
|---|
| 557 | n/a | return "selector for element %s of %s"%(self.__class__.__name__, str(self.fr)) |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | template = """ |
|---|
| 560 | n/a | class %s(ComponentItem): want = '%s' |
|---|
| 561 | n/a | """ |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | exec template % ("Text", 'text') |
|---|
| 564 | n/a | exec template % ("Character", 'cha ') |
|---|
| 565 | n/a | exec template % ("Word", 'cwor') |
|---|
| 566 | n/a | exec template % ("Line", 'clin') |
|---|
| 567 | n/a | exec template % ("paragraph", 'cpar') |
|---|
| 568 | n/a | exec template % ("Window", 'cwin') |
|---|
| 569 | n/a | exec template % ("Document", 'docu') |
|---|
| 570 | n/a | exec template % ("File", 'file') |
|---|
| 571 | n/a | exec template % ("InsertionPoint", 'cins') |
|---|