/usr/share/pyshared/pycalendar/vcard/property.py is in python-pycalendar 2.0~svn188-1.
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | ##
# Copyright (c) 2007-2011 Cyrus Daboo. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
from pycalendar import stringutils
from pycalendar.adr import Adr
from pycalendar.adrvalue import AdrValue
from pycalendar.attribute import PyCalendarAttribute
from pycalendar.datetime import PyCalendarDateTime
from pycalendar.datetimevalue import PyCalendarDateTimeValue
from pycalendar.dummyvalue import PyCalendarDummyValue
from pycalendar.exceptions import PyCalendarInvalidProperty
from pycalendar.integervalue import PyCalendarIntegerValue
from pycalendar.multivalue import PyCalendarMultiValue
from pycalendar.n import N
from pycalendar.nvalue import NValue
from pycalendar.orgvalue import OrgValue
from pycalendar.parser import ParserContext
from pycalendar.plaintextvalue import PyCalendarPlainTextValue
from pycalendar.utcoffsetvalue import PyCalendarUTCOffsetValue
from pycalendar.value import PyCalendarValue
from pycalendar.vcard import definitions
import cStringIO as StringIO
handleOptions = ("allow", "ignore", "fix", "raise")
missingParameterValues = "fix"
class Property(object):
sDefaultValueTypeMap = {
# 2425 Properties
definitions.Property_SOURCE : PyCalendarValue.VALUETYPE_URI,
definitions.Property_NAME : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_PROFILE : PyCalendarValue.VALUETYPE_TEXT,
# 2426 vCard Properties
# 2426 Section 3.1
definitions.Property_FN : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_N : PyCalendarValue.VALUETYPE_N,
definitions.Property_NICKNAME : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_PHOTO : PyCalendarValue.VALUETYPE_BINARY,
definitions.Property_BDAY : PyCalendarValue.VALUETYPE_DATE,
# 2426 Section 3.2
definitions.Property_ADR : PyCalendarValue.VALUETYPE_ADR,
definitions.Property_LABEL : PyCalendarValue.VALUETYPE_TEXT,
# 2426 Section 3.3
definitions.Property_TEL : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_EMAIL : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_MAILER : PyCalendarValue.VALUETYPE_TEXT,
# 2426 Section 3.4
definitions.Property_TZ : PyCalendarValue.VALUETYPE_UTC_OFFSET,
definitions.Property_GEO : PyCalendarValue.VALUETYPE_GEO,
# 2426 Section 3.5
definitions.Property_TITLE : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_ROLE : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_LOGO : PyCalendarValue.VALUETYPE_BINARY,
definitions.Property_AGENT : PyCalendarValue.VALUETYPE_VCARD,
definitions.Property_ORG : PyCalendarValue.VALUETYPE_ORG,
# 2426 Section 3.6
definitions.Property_CATEGORIES : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_NOTE : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_PRODID : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_REV : PyCalendarValue.VALUETYPE_DATETIME,
definitions.Property_SORT_STRING : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_SOUND : PyCalendarValue.VALUETYPE_BINARY,
definitions.Property_UID : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_URL : PyCalendarValue.VALUETYPE_URI,
definitions.Property_VERSION : PyCalendarValue.VALUETYPE_TEXT,
# 2426 Section 3.7
definitions.Property_CLASS : PyCalendarValue.VALUETYPE_TEXT,
definitions.Property_KEY : PyCalendarValue.VALUETYPE_BINARY,
}
sValueTypeMap = {
definitions.Value_BINARY : PyCalendarValue.VALUETYPE_BINARY,
definitions.Value_BOOLEAN : PyCalendarValue.VALUETYPE_BOOLEAN,
definitions.Value_DATE : PyCalendarValue.VALUETYPE_DATE,
definitions.Value_DATE_TIME : PyCalendarValue.VALUETYPE_DATETIME,
definitions.Value_FLOAT : PyCalendarValue.VALUETYPE_FLOAT,
definitions.Value_INTEGER : PyCalendarValue.VALUETYPE_INTEGER,
definitions.Value_TEXT : PyCalendarValue.VALUETYPE_TEXT,
definitions.Value_TIME : PyCalendarValue.VALUETYPE_TIME,
definitions.Value_URI : PyCalendarValue.VALUETYPE_URI,
definitions.Value_UTCOFFSET : PyCalendarValue.VALUETYPE_UTC_OFFSET,
definitions.Value_VCARD : PyCalendarValue.VALUETYPE_VCARD,
}
sTypeValueMap = {
PyCalendarValue.VALUETYPE_ADR : definitions.Value_TEXT,
PyCalendarValue.VALUETYPE_BINARY : definitions.Value_BINARY,
PyCalendarValue.VALUETYPE_BOOLEAN : definitions.Value_BOOLEAN,
PyCalendarValue.VALUETYPE_DATE : definitions.Value_DATE,
PyCalendarValue.VALUETYPE_DATETIME : definitions.Value_DATE_TIME,
PyCalendarValue.VALUETYPE_FLOAT : definitions.Value_FLOAT,
PyCalendarValue.VALUETYPE_GEO : definitions.Value_FLOAT,
PyCalendarValue.VALUETYPE_INTEGER : definitions.Value_INTEGER,
PyCalendarValue.VALUETYPE_N : definitions.Value_TEXT,
PyCalendarValue.VALUETYPE_ORG : definitions.Value_TEXT,
PyCalendarValue.VALUETYPE_TEXT : definitions.Value_TEXT,
PyCalendarValue.VALUETYPE_TIME : definitions.Value_TIME,
PyCalendarValue.VALUETYPE_URI : definitions.Value_URI,
PyCalendarValue.VALUETYPE_UTC_OFFSET : definitions.Value_UTCOFFSET,
PyCalendarValue.VALUETYPE_VCARD : definitions.Value_VCARD,
}
sMultiValues = set((
definitions.Property_NICKNAME,
definitions.Property_CATEGORIES,
))
sTextVariants = set((
definitions.Property_ADR,
definitions.Property_N,
definitions.Property_ORG,
))
def __init__(self, group=None, name=None, value=None, valuetype=None):
self._init_PyCalendarProperty()
self.mGroup = group
self.mName = name if name is not None else ""
if isinstance(value, int):
self._init_attr_value_int(value)
elif isinstance(value, str):
self._init_attr_value_text(value, valuetype if valuetype else Property.sDefaultValueTypeMap.get(self.mName.upper(), PyCalendarValue.VALUETYPE_TEXT))
elif isinstance(value, PyCalendarDateTime):
self._init_attr_value_datetime(value)
elif isinstance(value, Adr):
self._init_attr_value_adr(value)
elif isinstance(value, N):
self._init_attr_value_n(value)
elif isinstance(value, list) or isinstance(value, tuple):
if name.upper() == definitions.Property_ORG:
self._init_attr_value_org(value)
elif name.upper() == definitions.Property_GEO:
self._init_attr_value_geo(value)
else:
# Assume everything else is a text list
self._init_attr_value_text_list(value)
elif isinstance(value, PyCalendarUTCOffsetValue):
self._init_attr_value_utcoffset(value)
def duplicate(self):
other = Property(self.mGroup, self.mName)
for attrname, attrs in self.mAttributes.items():
other.mAttributes[attrname] = [i.duplicate() for i in attrs]
other.mValue = self.mValue.duplicate()
return other
def __hash__(self):
return hash((
self.mName,
tuple([tuple(self.mAttributes[attrname]) for attrname in sorted(self.mAttributes.keys())]),
self.mValue,
))
def __ne__(self, other): return not self.__eq__(other)
def __eq__(self, other):
if not isinstance(other, Property): return False
return (
self.mGroup == self.mGroup and
self.mName == other.mName and
self.mValue == other.mValue and
self.mAttributes == other.mAttributes
)
def __repr__(self):
return "vCard Property: %s" % (self.getText(),)
def __str__(self):
return self.getText()
def getGroup(self):
return self.mGroup
def setGroup(self, group):
self.mGroup = group
def getName(self):
return self.mName
def setName(self, name):
self.mName = name
def getAttributes(self):
return self.mAttributes
def setAttributes(self, attributes):
self.mAttributes = dict([(k.upper(), v) for k,v in attributes.iteritems()])
def hasAttribute(self, attr):
return self.mAttributes.has_key(attr.upper())
def getAttributeValue(self, attr):
return self.mAttributes[attr.upper()][0].getFirstValue()
def addAttribute(self, attr):
self.mAttributes.setdefault(attr.getName().upper(), []).append(attr)
def replaceAttribute(self, attr):
self.mAttributes[attr.getName().upper()] = [attr]
def removeAttributes(self, attr):
if self.mAttributes.has_key(attr.upper()):
del self.mAttributes[attr.upper()]
def getValue(self):
return self.mValue
def parse(self, data):
# Look for attribute or value delimiter
prop_name, txt = stringutils.strduptokenstr(data, ";:")
if not prop_name:
raise PyCalendarInvalidProperty("Invalid property", data)
# Check for group prefix
splits = prop_name.split(".", 1)
if len(splits) == 2:
# We have both group and name
self.mGroup = splits[0]
self.mName = splits[1]
else:
# We have the name
self.mName = prop_name
# Now loop getting data
try:
stripValueSpaces = False # Fix for AB.app base PHOTO properties that use two spaces at start of line
while txt:
if txt[0] == ';':
# Parse attribute
# Move past delimiter
txt = txt[1:]
# Get quoted string or token - in iCalendar we only look for "=" here
# but for "broken" vCard BASE64 property we need to also terminate on
# ":;"
attribute_name, txt = stringutils.strduptokenstr(txt, "=:;")
if attribute_name is None:
raise PyCalendarInvalidProperty("Invalid property", data)
if txt[0] != "=":
# Deal with parameters without values
if ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_RAISE:
raise PyCalendarInvalidProperty("Invalid property parameter", data)
elif ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_ALLOW:
attribute_value = None
else: # PARSER_IGNORE and PARSER_FIX
attribute_name = None
if attribute_name.upper() == "BASE64" and ParserContext.VCARD_2_BASE64 == ParserContext.PARSER_FIX:
attribute_name = definitions.Parameter_ENCODING
attribute_value = definitions.Parameter_Value_ENCODING_B
stripValueSpaces = True
else:
txt = txt[1:]
attribute_value, txt = stringutils.strduptokenstr(txt, ":;,")
if attribute_value is None:
raise PyCalendarInvalidProperty("Invalid property", data)
# Now add attribute value
if attribute_name is not None:
attrvalue = PyCalendarAttribute(name = attribute_name, value=attribute_value)
self.mAttributes.setdefault(attribute_name.upper(), []).append(attrvalue)
# Look for additional values
while txt[0] == ',':
txt = txt[1:]
attribute_value2, txt = stringutils.strduptokenstr(txt, ":;,")
if attribute_value2 is None:
raise PyCalendarInvalidProperty("Invalid property", data)
attrvalue.addValue(attribute_value2)
elif txt[0] == ':':
txt = txt[1:]
if stripValueSpaces:
txt = txt.replace(" ", "")
self.createValue(txt)
txt = None
except IndexError:
raise PyCalendarInvalidProperty("Invalid property", data)
# We must have a value of some kind
if self.mValue is None:
raise PyCalendarInvalidProperty("Invalid property", data)
return True
def getText(self):
os = StringIO.StringIO()
self.generate(os)
return os.getvalue()
def generate(self, os):
# Write it out always with value
self.generateValue(os, False)
def generateFiltered(self, os, filter):
# Check for property in filter and whether value is written out
test, novalue = filter.testPropertyValue(self.mName.upper())
if test:
self.generateValue(os, novalue)
# Write out the actual property, possibly skipping the value
def generateValue(self, os, novalue):
self.setupValueAttribute()
# Must write to temp buffer and then wrap
sout = StringIO.StringIO()
if self.mGroup:
sout.write(self.mGroup + ".")
sout.write(self.mName)
# Write all attributes
for key in sorted(self.mAttributes.keys()):
for attr in self.mAttributes[key]:
sout.write(";")
attr.generate(sout)
# Write value
sout.write(":")
if self.mName.upper() == "PHOTO" and self.mValue.getType() == PyCalendarValue.VALUETYPE_BINARY:
# Handle AB.app PHOTO values
sout.write("\r\n")
value = self.mValue.getText()
value_len = len(value)
offset = 0
while(value_len > 72):
sout.write(" ")
sout.write(value[offset:offset+72])
sout.write("\r\n")
value_len -= 72
offset += 72
sout.write(" ")
sout.write(value[offset:])
os.write(sout.getvalue())
else:
if self.mValue and not novalue:
self.mValue.generate(sout)
# Get string text
temp = sout.getvalue()
sout.close()
# Look for line length exceed
if len(temp) < 75:
os.write(temp)
else:
# Look for valid utf8 range and write that out
start = 0
written = 0
lineWrap = 74
while written < len(temp):
# Start 74 chars on from where we are
offset = start + lineWrap
if offset >= len(temp):
line = temp[start:]
os.write(line)
written = len(temp)
else:
# Check whether next char is valid utf8 lead byte
while (temp[offset] > 0x7F) and ((ord(temp[offset]) & 0xC0) == 0x80):
# Step back until we have a valid char
offset -= 1
line = temp[start:offset]
os.write(line)
os.write("\r\n ")
lineWrap = 73 # We are now adding a space at the start
written += offset - start
start = offset
os.write("\r\n")
def _init_PyCalendarProperty(self):
self.mGroup = None
self.mName = ""
self.mAttributes = {}
self.mValue = None
def createValue(self, data):
# Tidy first
self.mValue = None
# Get value type from property name
valueType = Property.sDefaultValueTypeMap.get(self.mName.upper(), PyCalendarValue.VALUETYPE_TEXT)
# Check whether custom value is set
if self.mAttributes.has_key(definitions.Parameter_VALUE):
attr = self.getAttributeValue(definitions.Parameter_VALUE)
if attr != definitions.Value_TEXT or self.mName.upper() not in Property.sTextVariants:
valueType = Property.sValueTypeMap.get(attr, valueType)
# Check for multivalued
if self.mName.upper() in Property.sMultiValues:
self.mValue = PyCalendarMultiValue(valueType)
else:
# Create the type
self.mValue = PyCalendarValue.createFromType(valueType)
# Now parse the data
try:
if valueType in (PyCalendarValue.VALUETYPE_DATE, PyCalendarValue.VALUETYPE_DATETIME):
# vCard supports a slightly different, expanded form, of date
self.mValue.parse(data, fullISO=True)
else:
self.mValue.parse(data)
except ValueError:
raise PyCalendarInvalidProperty("Invalid property value", data)
def setValue(self, value):
# Tidy first
self.mValue = None
# Get value type from property name
valueType = Property.sDefaultValueTypeMap.get(self.mName.upper(), PyCalendarDummyValue)
# Check whether custom value is set
if self.mAttributes.has_key(definitions.Parameter_VALUE):
attr = self.getAttributeValue(definitions.Parameter_VALUE)
if attr != definitions.Value_TEXT or self.mName.upper() not in Property.sTextVariants:
valueType = Property.sValueTypeMap.get(attr, valueType)
# Check for multivalued
if self.mName.upper() in Property.sMultiValues:
self.mValue = PyCalendarMultiValue(valueType)
else:
# Create the type
self.mValue = PyCalendarValue.createFromType(valueType)
self.mValue.setValue(value)
def setupValueAttribute(self):
if self.mAttributes.has_key(definitions.Parameter_VALUE):
del self.mAttributes[definitions.Parameter_VALUE]
# Only if we have a value right now
if self.mValue is None:
return
# See if current type is default for this property
default_type = Property.sDefaultValueTypeMap.get(self.mName.upper())
if default_type is not None:
actual_type = self.mValue.getType()
if default_type != actual_type:
actual_value = self.sTypeValueMap.get(actual_type)
if actual_value is not None:
self.mAttributes.setdefault(definitions.Parameter_VALUE, []).append(PyCalendarAttribute(name=definitions.Parameter_VALUE, value=actual_value))
# Creation
def _init_attr_value_int(self, ival):
# Value
self.mValue = PyCalendarIntegerValue(value=ival)
# Attributes
self.setupValueAttribute()
def _init_attr_value_text(self, txt, value_type):
# Value
self.mValue = PyCalendarValue.createFromType(value_type)
if isinstance(self.mValue, PyCalendarPlainTextValue) or isinstance(self.mValue, PyCalendarDummyValue):
self.mValue.setValue(txt)
# Attributes
self.setupValueAttribute()
def _init_attr_value_adr(self, reqstatus):
# Value
self.mValue = AdrValue(reqstatus)
# Attributes
self.setupValueAttribute()
def _init_attr_value_n(self, reqstatus):
# Value
self.mValue = NValue(reqstatus)
# Attributes
self.setupValueAttribute()
def _init_attr_value_org(self, reqstatus):
# Value
self.mValue = OrgValue(reqstatus)
# Attributes
self.setupValueAttribute()
def _init_attr_value_datetime(self, dt):
# Value
self.mValue = PyCalendarDateTimeValue(value=dt)
# Attributes
self.setupValueAttribute()
def _init_attr_value_utcoffset(self, utcoffset):
# Value
self.mValue = PyCalendarUTCOffsetValue()
self.mValue.setValue(utcoffset.getValue())
# Attributes
self.setupValueAttribute()
|