def load(cls, path, project_name):
        """
        Read project from disk location <path>/<project_name>

        Returns:
        Newly created ProjectScene instance.

        Exceptions:
        IOError - if project cannot be read
        ValueError - if xml cannot be parsed
        """
        settings = {}
        project_type = None

        project_dir = os.path.join(path, project_name)

        # Read xml from file
        xml_tree = ET.parse(os.path.join(project_dir, project_name + ".xml"))

        # parse xml tree
        root = xml_tree.getroot()
        if root.tag != "scene":
            raise ValueError('Expected root tag to be "scene"')

        # extract header info
        for element in root:
            if element.tag == "categories":
                for child_element in element:
                    if child_element.tag == "id":
                        settings["categories_id"] = child_element.text
                    elif child_element.tag == "url":
                        settings["categories_url"] = child_element.text
            elif element.tag == "project_type":
                project_type = element.text
            elif element.tag == "version":
                settings["version"] = element.text

            # Skip elements tag for now.
            # Any other tags are silently ignored

        if project_type is None:
            raise ValueError("xml is missing project_type tag")

        # extract elements
        for element in root:
            if element.tag == "elements":
                elements = ProjectObjectDict.load(project_type=project_type,
                                                  base_elem=element,
                                                  path=project_dir)
            # header and any other tags are silently ignored

        return cls(project_type, elements, settings)
Exemplo n.º 2
0
    def test_object_io(self):
        """Write to files and then read back in."""

        # check that writing meshes creates the directory and stores a glb file there
        orig_pod = ProjectObjectDict.example()
        path = self.temp_directory
        xml = orig_pod.save(path)
        # FIXME: Disabled check.  Only works in Python 3.
        # ET interface must be different.
        #        self.assertIsInstance(xml, ET.Element)
        self.assertTrue(
            os.path.isfile(os.path.join(self.temp_directory, "1.glb")))

        # check that reading meshes gives the same as what we started with
        new_pod = ProjectObjectDict.load("meshes", xml, path)
        self.assertTrue(new_pod.almost_equal(orig_pod))

        for obj in orig_pod.values():
            self.assertTrue(obj.almost_equal(new_pod[obj.id]))