def test_open_file_write_path(): path = os.path.join(tempfile.gettempdir(), 'test-file.txt') with _iotools.open_file(path, 'w') as file: file.write('Hello world') with _iotools.open_file(path, 'r') as file: assert file.read() == 'Hello world'
def test_open_file_write_file_object(): path = os.path.join(tempfile.gettempdir(), 'test-file.txt') with open(path, mode='w') as f: with _iotools.open_file(f) as file: file.write('Hello world') with open(path, mode='r') as f: with _iotools.open_file(f) as file: assert file.read() == 'Hello world'
def write(self): if not self.mesh.is_trimesh(): raise ValueError('Mesh must be triangular to be encoded in STL.') if not self.binary: with _iotools.open_file(self.filepath, 'w') as self.file: self.write_header() self.write_faces() self.write_footer() else: with _iotools.open_file(self.filepath, 'wb') as self.file: self.file.seek(0) self.write_binary_header() self.write_binary_num_faces() self.write_binary_faces()
def read(self): """Read the data. Returns ------- None """ is_binary = False with _iotools.open_file(self.filepath, 'rb') as file: line = file.readline().strip() if b'solid' in line: is_binary = False else: is_binary = True try: if not is_binary: self._read_ascii() else: self._read_binary() except Exception: # raise if it was already detected as binary, but failed anyway if is_binary: raise # else, ascii parsing failed, try binary is_binary = True self._read_binary()
def json_load(fp): """Read COMPAS object data from a JSON file. Parameters ---------- fp : path string, file-like object or URL string A readable path, a file-like object or a URL pointing to a file. Returns ------- data The (COMPAS) data contained in the file. Examples -------- >>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> compas.json_dump(data1, 'data.json') >>> data2 = compas.json_load('data.json') >>> data1 == data2 True """ with _iotools.open_file(fp, 'r') as f: return json.load(f, cls=DataDecoder)
def json_dump(data, fp, pretty=False): """Write a collection of COMPAS object data to a JSON file. Parameters ---------- data : any Any JSON serializable object. This includes any (combination of) COMPAS object(s). fp : path string or file-like object A writeable file-like object or the path to a file. pretty : bool, optional ``True`` to format the output with indentation, otherwise ``False``. Returns ------- None Examples -------- >>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> compas.json_dump(data1, 'data.json') >>> data2 = compas.json_load('data.json') >>> data1 == data2 True """ with _iotools.open_file(fp, 'w') as f: kwargs = dict(sort_keys=True, indent=4) if pretty else {} return json.dump(data, f, cls=DataEncoder, **kwargs)
def write(self): with _iotools.open_file(self.filepath, 'w') as self.file: self.write_header() if self.unweld: self.write_vertices_and_faces() else: self.write_vertices() self.write_faces()
def test_iter_file_chunks_path_image(path_image): CHUNK_SIZE = 30 chunks = [] with _iotools.open_file(path_image, 'rb') as file: for data in _iotools.iter_file(file, size=CHUNK_SIZE): chunks.append(data) assert len(chunks) == math.ceil(IMAGE_FILE_SIZE / float(CHUNK_SIZE))
def open(self): """Open the file and read its contents. Returns ------- None """ with _iotools.open_file(self.filepath, 'r') as f: self.content = f.readlines()
def write(self): """Write the meshes to the file. Returns ------- None """ with _iotools.open_file(self.filepath, 'w') as self.file: self._write_header() self._write_meshes()
def write(self): """Write the data to a file. Returns ------- None """ with _iotools.open_file(self.filepath, 'w') as self.file: self._write_header() self._write_vertices() self._write_faces()
def xml_from_file(source, tree_parser=None): if tree_parser: raise NotImplementedError( 'XML parsing on CPython 3.7 and older does not support a custom tree parser' ) tree_parser = ET.XMLPullParser parser = tree_parser(events=('start', 'start-ns')) with _iotools.open_file(source) as file: for data in _iotools.iter_file(file): parser.feed(data) return _process_all_events(parser)
def read_data_binary(self): if not self.end_header: raise Exception( 'header has not been read, or the file is not valid') with _iotools.open_file(self.filepath, 'rb') as self.file: self.file.seek(self.end_header) for section in self.sections: if section == 'vertex': self.read_vertices_binary_wo_numpy() elif section == 'edge': self.read_edges_binary_wo_numpy() elif section == 'face': self.read_faces_binary_wo_numpy() else: print( 'user-defined elements are not supported: {0}'.format( section)) pass
def test_open_file_url_as_write_fails(url_text): with pytest.raises(ValueError): with _iotools.open_file(url_text, mode='w') as _: pass
def test_open_file_url_text(url_text): with _iotools.open_file(url_text) as file: assert b'COMPAS framework' in file.read()
def test_open_file_url_image(url_image): with _iotools.open_file(url_image) as file: assert len(file.read()) == REMOTE_IMAGE_FILE_SIZE
def test_open_file_memory_stream(): text = b"All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third." data = io.BytesIO(text) with _iotools.open_file(data, mode='rb') as f: assert f.read() == text
def test_open_file_object_text(path_text): with open(path_text, mode='r') as f: with _iotools.open_file(f) as file: assert len(file.read()) == TEXT_FILE_SIZE
def test_open_file_object_binary(path_image): with open(path_image, mode='rb') as f: with _iotools.open_file(f) as file: assert len(file.read()) == IMAGE_FILE_SIZE
def _read_ascii(self): with _iotools.open_file(self.filepath, 'r') as file: self.file = file self.file.seek(0) self.facets = self._read_solids_ascii()
def open(self): with _iotools.open_file(self.filepath, 'r') as f: self.content = f.readlines()
def write(self): with _iotools.open_file(self.filepath, 'w') as self.file: self.write_header() self.write_meshes()
def write(self): with _iotools.open_file(self.filepath, 'w') as self.file: self.write_header() self.write_vertices() self.write_faces()
def read_header(self): # the header is always in ascii format # read it as text # otherwise file.tell() can't be used reliably # to figure out where the header ends with _iotools.open_file(self.filepath) as file: file.seek(0) line = file.readline().rstrip() if line.lower() != 'ply': raise Exception('not a valid ply file') self.start_header = file.tell() element_type = None while True: line = file.readline() line = line.rstrip() self.header.append(line) if line.startswith('format'): element_type = None self.format = line[len('format') + 1:].split(' ')[0] elif line.startswith('comment'): element_type = None self.comments.append(line[len('comment') + 1:]) elif line.startswith('element'): parts = line.split() element_type = parts[1] if element_type == 'vertex': self.sections.append('vertex') self.number_of_vertices = int(parts[2]) elif element_type == 'edge': self.sections.append('edge') self.number_of_edges = int(parts[2]) elif element_type == 'face': self.sections.append('face') self.number_of_faces = int(parts[2]) else: element_type = None raise Exception elif line.startswith('property'): parts = line.split() if element_type == 'vertex': property_type = parts[1] property_name = parts[2] self.vertex_properties.append( (property_name, property_type)) elif element_type == 'edge': property_type = parts[1] property_name = parts[2] self.edge_properties.append( (property_name, property_type)) elif element_type == 'face': property_type = parts[1] if property_type == 'list': property_length = parts[2] property_type = parts[3] property_name = parts[4] self.face_properties.append( (property_name, property_type, property_length)) else: property_type = parts[1] property_name = parts[2] self.face_properties.append( (property_name, property_type)) else: element_type = None raise Exception elif line == 'end_header': element_type = None self.end_header = file.tell() break else: pass
def test_open_file_does_not_close_file_objects(path_image): with open(path_image, mode='rb') as f: with _iotools.open_file(f): pass assert not f.closed assert f.closed
def test_open_file_closes_path_like(path_image): with _iotools.open_file(path_image, mode='rb') as file: assert not file.closed assert file.closed
def read(self): """Read the contents of the file.""" with _iotools.open_file(self.filepath, 'rb') as fp: for line in fp: print(line.strip())
def _read_binary(self): with _iotools.open_file(self.filepath, 'rb') as file: self.file = file self.file.seek(0) self.header = self._read_header_binary() self.facets = self._read_facets_binary()
def shared_xml_from_file(source, tree_parser, http_response_type): target = TreeBuilderWithNamespaces() with _iotools.open_file(source, 'r') as f: tree = ET.parse(f, tree_parser(target=target)) return tree.getroot()
def read(self): with _iotools.open_file(self.filepath, 'rb') as fp: for line in fp: print(line.strip())