text
stringlengths
0
828
Returns the remaining, unread bytes from the buffer.
""""""
pos, self._pos = self._pos, len(self.buffer)
return self.buffer[pos:]"
3944,"def decode(self, bytes):
""""""
Decodes the packet off the byte string.
""""""
self.buffer = bytes
self._pos = 0
Packet = identifier.get_packet_from_id(self._read_variunt())
# unknown packets will be None from the identifier
if Packet is None:
return None
packet = Packet()
packet.ParseFromString(self.remaining_bytes())
return packet"
3945,"def encode(self, packet):
""""""
Pushes a packet to the writer, encoding it on the internal
buffer.
""""""
id = identifier.get_packet_id(packet)
if id is None:
raise EncoderException('unknown packet')
self._write_variunt(id)
self._write(packet.SerializeToString())
return bytes(self.buffer)"
3946,"def setup_logging(level):
"""""" Setup logger. """"""
logging.root.setLevel(level)
logging.root.addHandler(STREAM_HANDLER)"
3947,"def create(self, data):
"""""" Create object from the given data.
The given data may or may not have been validated prior to calling
this function. This function will try its best in creating the object.
If the resulting object cannot be produced, raises ``ValidationError``.
The spec can affect how individual fields will be created by
implementing ``clean()`` for the fields needing customization.
:param data: the data as a dictionary.
:return: instance of ``klass`` or dictionary.
:raises: ``ValidationError`` if factory is unable to create object.
""""""
# todo: copy-paste code from representation.validate -> refactor
if data is None:
return None
prototype = {}
errors = {}
# create and populate the prototype
for field_name, field_spec in self.spec.fields.items():
try:
value = self._create_value(data, field_name, self.spec)
except ValidationError, e:
if field_name not in self.default_create_values:
if hasattr(e, 'message_dict'):
# prefix error keys with top level field name
errors.update(dict(zip(
[field_name + '.' + key for key in e.message_dict.keys()],
e.message_dict.values())))
else:
errors[field_name] = e.messages
else:
key_name = self.property_name_map[field_name]
prototype[key_name] = value
# check extra fields
if self.prevent_extra_fields:
extras = set(data.keys()) - set(self.property_name_map.keys())
if extras:
errors[', '.join(extras)] = ['field(s) not allowed']
# if errors, raise ValidationError
if errors:
raise ValidationError(errors)
# return dict or object based on the prototype
_data = deepcopy(self.default_create_values)
_data.update(prototype)
if self.klass:
instance = self.klass()
instance.__dict__.update(prototype)
return instance
else:
return prototype"
3948,"def serialize(self, entity, request=None):
"""""" Serialize entity into dictionary.