Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def encode_member(self, member, data, encoder, encode_default=False):
name = member.name
if name in data:
try:
if member.default is None:
member.encode(data[name], encoder)
elif not member.is_default(data[name]) or encode_default:
member.encode(data[name], encoder)
except EncodeError as e:
e.location.append(member.name)
raise
elif member.optional or member.default is not None:
pass
else:
raise EncodeError(
"{} member '{}' not found in {}.".format(
self.__class__.__name__,
name,
data))
def encode(self, data):
try:
member = self.name_to_member[data[0]]
except KeyError:
raise EncodeError(
"Expected choice {}, but got '{}'.".format(
self.format_names(),
data[0]))
try:
return {member.name: member.encode(data[1])}
except EncodeError as e:
e.location.append(member.name)
raise
def encode(self, data):
try:
member = self.name_to_member[data[0]]
except KeyError:
raise EncodeError(
"Expected choice {}, but got '{}'.".format(
self.format_names(),
data[0]))
element = ElementTree.Element(self.name)
try:
element.append(member.encode(data[1]))
except EncodeError as e:
e.location.append(member.name)
raise
return element
def encode_member(self, member, data, encoder, encode_default=False):
name = member.name
if name in data:
try:
if member.default is None:
member.encode(data[name], encoder)
elif not member.is_default(data[name]) or encode_default:
member.encode(data[name], encoder)
except EncodeError as e:
e.location.append(member.name)
raise
elif member.optional or member.has_default():
pass
else:
raise EncodeError(
"{} member '{}' not found in {}.".format(
self.__class__.__name__,
name,
data))
def encode(self, data):
if sys.version_info[0] > 2:
if not isinstance(data, (int, str)):
raise EncodeError(
'Expected data of type int or str, but got {}.'.format(
data))
else:
if not isinstance(data, (int, long, str, unicode)):
raise EncodeError(
'Expected data of type int or str, but got {}.'.format(
data))
if name in data:
value = data[name]
try:
if isinstance(member, AnyDefinedBy):
member.encode(value, encoded_members, data)
elif not member.is_default(value):
member.encode(value, encoded_members)
except EncodeError as e:
e.location.append(member.name)
raise
elif member.optional:
pass
elif not member.has_default():
raise EncodeError("{} member '{}' not found in {}.".format(
self.__class__.__name__,
name,
data))
def encode_member(self, member, data, encoder):
try:
member.encode(data, encoder)
except EncodeError as e:
e.location.append(member.name)
raise
def encode_member(self, member, data, encoder):
try:
member.encode(data, encoder)
except EncodeError as e:
e.location.append(member.name)
raise