Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_valid(self):
"""Valid (but not necessarily sane) vCards"""
for vcard_file in VCARDS_VALID:
vcard_text = _get_vcard_file(vcard_file)
if vcard_text is None:
continue
try:
with warnings.catch_warnings(record=True):
vc_obj = vcard_validator.VCard(vcard_text, filename=vcard_file)
self.assertNotEqual(vc_obj, None)
except vcard_errors.VCardError as error:
error_message = '\n\n'.join((
'Expected valid vCard for {vcard_file!r}, but it failed to validate'.format(
vcard_file=vcard_file
),
str(error)
))
self.fail(error_message)
if len(value) != 1:
raise VCardItemCountError(
'{0}: {1:d} (expected 1)'.format(
NOTE_INVALID_SUB_VALUE_COUNT, len(property_.values[0])), {})
validate_uri(value[0])
else:
# Inline vCard object
pass # TODO: Un-escape and validate value
elif property_name == 'URL':
#
_expect_no_parameters(property_)
_expect_value_count(property_.values, 1)
validate_uri(property_.values[0][0])
except VCardError as error:
error.context['Property'] = property_name
err_type = type(error)
raise err_type(error.message, error.context)
message += '\n{0}: {1}'.format(_stringify(key), _stringify(self.context.pop(key)))
return message
class VCardLineError(VCardError):
"""Raised for line-related errors."""
pass
class VCardNameError(VCardError):
"""Raised for name-related errors."""
pass
class VCardValueError(VCardError):
"""Raised for value-related errors."""
pass
class VCardItemCountError(VCardError):
"""Raised when a required number of something is not present."""
pass
class UsageError(Exception):
"""Raise in case of invalid parameters."""
def __init__(self, message):
Exception.__init__(self)
self._message = message
def __str__(self):
def __init__(self, text, filename=None):
"""
Create vCard object from text string. Includes text (the entire
unprocessed vCard), group (optional prefix on each line) and
properties.
@param text: String containing a single vCard
"""
if text == '' or text is None:
raise VCardError(NOTE_EMPTY_VCARD, {'vCard line': 1, 'File line': 1})
self.text = text
self.filename = filename
lines = unfold_vcard_lines(self.text.splitlines(True))
# Groups
self.group = get_vcard_group(lines)
lines = remove_vcard_groups(lines, self.group)
# Properties
self.properties = get_vcard_properties(lines)
# Sort context information
keys = ['File', 'File line', 'vCard line', 'Property', 'Property line', 'String']
for key in keys:
if key in self.context:
message += '\n{0}: {1}'.format(_stringify(key), _stringify(self.context.pop(key)))
return message
class VCardLineError(VCardError):
"""Raised for line-related errors."""
pass
class VCardNameError(VCardError):
"""Raised for name-related errors."""
pass
class VCardValueError(VCardError):
"""Raised for value-related errors."""
pass
class VCardItemCountError(VCardError):
"""Raised when a required number of something is not present."""
pass
class UsageError(Exception):
"""Raise in case of invalid parameters."""
def get_vcard_properties(lines):
"""
Get the properties for each line. RFC 2426 pages 28, 29.
@param lines: List of unfolded vCard lines
@return: List of properties, for simplicity. AFAIK sequence doesn't matter
and duplicates add no information, but ignoring this to make sure vCard
output looks like the original.
"""
properties = []
for index in range(len(lines)):
property_line = lines[index]
if property_line != NEWLINE_CHARACTERS:
try:
properties.append(get_vcard_property(property_line))
except VCardError as error:
error.context['vCard line'] = index
err_type = type(error)
raise err_type(
error.message,
error.context)
for mandatory_property in MANDATORY_PROPERTIES:
if mandatory_property not in [property_.name.upper() for property_ in properties]:
raise VCardItemCountError(
'{0}: {1}'.format(NOTE_MISSING_PROPERTY, mandatory_property), {'Property': mandatory_property})
return properties
class VCardLineError(VCardError):
"""Raised for line-related errors."""
pass
class VCardNameError(VCardError):
"""Raised for name-related errors."""
pass
class VCardValueError(VCardError):
"""Raised for value-related errors."""
pass
class VCardItemCountError(VCardError):
"""Raised when a required number of something is not present."""
pass
class UsageError(Exception):
"""Raise in case of invalid parameters."""
def __init__(self, message):
Exception.__init__(self)
self._message = message
def __str__(self):
return self._message
Outputs error with ordered context info.
@return: Printable error message
"""
message = _stringify(self.message)
# Sort context information
keys = ['File', 'File line', 'vCard line', 'Property', 'Property line', 'String']
for key in keys:
if key in self.context:
message += '\n{0}: {1}'.format(_stringify(key), _stringify(self.context.pop(key)))
return message
class VCardLineError(VCardError):
"""Raised for line-related errors."""
pass
class VCardNameError(VCardError):
"""Raised for name-related errors."""
pass
class VCardValueError(VCardError):
"""Raised for value-related errors."""
pass
class VCardItemCountError(VCardError):
"""Raised when a required number of something is not present."""