Beispiel #1
0
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'
Beispiel #2
0
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'
Beispiel #3
0
 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()
Beispiel #4
0
    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()
Beispiel #5
0
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)
Beispiel #6
0
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)
Beispiel #7
0
 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()
Beispiel #8
0
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))
Beispiel #9
0
    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()
Beispiel #10
0
    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()
Beispiel #11
0
    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()
Beispiel #12
0
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)
Beispiel #13
0
 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
Beispiel #14
0
def test_open_file_url_as_write_fails(url_text):
    with pytest.raises(ValueError):
        with _iotools.open_file(url_text, mode='w') as _:
            pass
Beispiel #15
0
def test_open_file_url_text(url_text):
    with _iotools.open_file(url_text) as file:
        assert b'COMPAS framework' in file.read()
Beispiel #16
0
def test_open_file_url_image(url_image):
    with _iotools.open_file(url_image) as file:
        assert len(file.read()) == REMOTE_IMAGE_FILE_SIZE
Beispiel #17
0
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
Beispiel #18
0
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
Beispiel #19
0
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
Beispiel #20
0
 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()
Beispiel #21
0
 def open(self):
     with _iotools.open_file(self.filepath, 'r') as f:
         self.content = f.readlines()
Beispiel #22
0
 def write(self):
     with _iotools.open_file(self.filepath, 'w') as self.file:
         self.write_header()
         self.write_meshes()
Beispiel #23
0
 def write(self):
     with _iotools.open_file(self.filepath, 'w') as self.file:
         self.write_header()
         self.write_vertices()
         self.write_faces()
Beispiel #24
0
    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
Beispiel #25
0
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
Beispiel #26
0
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
Beispiel #27
0
 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())
Beispiel #28
0
 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()
Beispiel #29
0
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()
Beispiel #30
0
 def read(self):
     with _iotools.open_file(self.filepath, 'rb') as fp:
         for line in fp:
             print(line.strip())