Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'''
Read the actual data from a PLY file.
'''
dtype = self.dtype(byte_order)
if text:
self._read_txt(stream)
elif _can_mmap(stream) and not self._have_list:
# Loading the data is straightforward. We will memory map
# the file in copy-on-write mode.
num_bytes = self.count * dtype.itemsize
offset = stream.tell()
stream.seek(0, 2)
max_bytes = stream.tell() - offset
if max_bytes < num_bytes:
raise PlyElementParseError("early end-of-file", self,
max_bytes // dtype.itemsize)
self._data = _np.memmap(stream, dtype,
'c', offset, self.count)
# Fix stream position
stream.seek(offset + self.count * dtype.itemsize)
else:
# A simple load is impossible.
self._read_bin(stream, byte_order)
self._check_sanity()
def test_element_parse_error_repr():
prop = PlyProperty('x', 'f4')
elt = PlyElement('test', [prop], 0)
e = PlyElementParseError('text', elt, 0, prop)
assert repr(e)
may contain list properties.
'''
self._data = _np.empty(self.count, dtype=self.dtype())
k = 0
for line in _islice(iter(stream.readline, b''), self.count):
fields = iter(line.strip().split())
for prop in self.properties:
try:
self._data[prop.name][k] = prop._from_fields(fields)
except StopIteration:
raise PlyElementParseError("early end-of-line",
self, k, prop)
except ValueError:
raise PlyElementParseError("malformed input",
self, k, prop)
try:
next(fields)
except StopIteration:
pass
else:
raise PlyElementParseError("expected end-of-line",
self, k)
k += 1
if k < self.count:
del self._data
raise PlyElementParseError("early end-of-file", self, k)
def _read_txt(self, stream):
'''
Load a PLY element from an ASCII-format PLY file. The element
may contain list properties.
'''
self._data = _np.empty(self.count, dtype=self.dtype())
k = 0
for line in _islice(iter(stream.readline, b''), self.count):
fields = iter(line.strip().split())
for prop in self.properties:
try:
self._data[prop.name][k] = prop._from_fields(fields)
except StopIteration:
raise PlyElementParseError("early end-of-line",
self, k, prop)
except ValueError:
raise PlyElementParseError("malformed input",
self, k, prop)
try:
next(fields)
except StopIteration:
pass
else:
raise PlyElementParseError("expected end-of-line",
self, k)
k += 1
if k < self.count:
del self._data
raise PlyElementParseError("early end-of-file", self, k)
fields = iter(line.strip().split())
for prop in self.properties:
try:
self._data[prop.name][k] = prop._from_fields(fields)
except StopIteration:
raise PlyElementParseError("early end-of-line",
self, k, prop)
except ValueError:
raise PlyElementParseError("malformed input",
self, k, prop)
try:
next(fields)
except StopIteration:
pass
else:
raise PlyElementParseError("expected end-of-line",
self, k)
k += 1
if k < self.count:
del self._data
raise PlyElementParseError("early end-of-file", self, k)
def _read_bin(self, stream, byte_order):
'''
Load a PLY element from a binary PLY file. The element may
contain list properties.
'''
self._data = _np.empty(self.count, dtype=self.dtype(byte_order))
for k in _range(self.count):
for prop in self.properties:
try:
self._data[prop.name][k] = \
prop._read_bin(stream, byte_order)
except StopIteration:
raise PlyElementParseError("early end-of-file",
self, k, prop)
self, k, prop)
except ValueError:
raise PlyElementParseError("malformed input",
self, k, prop)
try:
next(fields)
except StopIteration:
pass
else:
raise PlyElementParseError("expected end-of-line",
self, k)
k += 1
if k < self.count:
del self._data
raise PlyElementParseError("early end-of-file", self, k)