Beispiel #1
0
    def test_rename_dataset(self):
        """
        Test QgsFileUtils.renameDataset
        """
        base_path = tempfile.mkdtemp()
        for ext in ['shp', 'dbf', 'prj', 'qml', 'shx']:
            shutil.copy(f'{unitTestDataPath()}/lines.{ext}',
                        f'{base_path}/lines.{ext}')
            shutil.copy(f'{unitTestDataPath()}/lines.{ext}',
                        f'{base_path}/lines.{ext}')
        self.assertTrue(os.path.exists(f'{base_path}/lines.shp'))

        res, error = QgsFileUtils.renameDataset(
            f'{base_path}/lines.shp', f'{base_path}/other_lines.shp')
        self.assertTrue(res)
        for ext in ['shp', 'dbf', 'prj', 'qml', 'shx']:
            self.assertTrue(os.path.exists(f'{base_path}/other_lines.{ext}'))
            self.assertFalse(os.path.exists(f'{base_path}/lines.{ext}'))

        # skip qml file
        res, error = QgsFileUtils.renameDataset(
            f'{base_path}/other_lines.shp', f'{base_path}/other_lines2.shp',
            Qgis.FileOperationFlags())
        self.assertTrue(res)
        for ext in ['shp', 'dbf', 'prj', 'shx']:
            self.assertTrue(os.path.exists(f'{base_path}/other_lines2.{ext}'))
            self.assertFalse(os.path.exists(f'{base_path}/other_lines.{ext}'))
        self.assertFalse(os.path.exists(f'{base_path}/other_lines2.qml'))
        self.assertTrue(os.path.exists(f'{base_path}/other_lines.qml'))

        # try changing extension -- sidecars won't be renamed
        res, error = QgsFileUtils.renameDataset(
            f'{base_path}/other_lines2.shp', f'{base_path}/other_lines2.txt',
            Qgis.FileOperationFlags())
        self.assertFalse(error)
        self.assertTrue(res)
        self.assertFalse(os.path.exists(f'{base_path}/other_lines2.shp'))
        self.assertTrue(os.path.exists(f'{base_path}/other_lines2.txt'))
        for ext in ['dbf', 'prj', 'shx']:
            self.assertTrue(os.path.exists(f'{base_path}/other_lines2.{ext}'))

        for ext in ['shp', 'dbf', 'prj', 'qml', 'shx']:
            shutil.copy(f'{unitTestDataPath()}/lines.{ext}',
                        f'{base_path}/ll.{ext}')
            shutil.copy(f'{unitTestDataPath()}/lines.{ext}',
                        f'{base_path}/ll.{ext}')

        # file name clash
        with open(f'{base_path}/yy.shp', 'wt') as f:
            f.write('')
        res, error = QgsFileUtils.renameDataset(f'{base_path}/ll.shp',
                                                f'{base_path}/yy.shp',
                                                Qgis.FileOperationFlags())
        self.assertFalse(res)
        self.assertTrue(error)
        # nothing should be renamed
        for ext in ['shp', 'dbf', 'prj', 'qml', 'shx']:
            self.assertTrue(os.path.exists(f'{base_path}/ll.{ext}'))

        # sidecar clash
        with open(f'{base_path}/yyy.shx', 'wt') as f:
            f.write('')
        res, error = QgsFileUtils.renameDataset(f'{base_path}/ll.shp',
                                                f'{base_path}/yyy.shp')
        self.assertFalse(res)
        self.assertTrue(error)
        # no files should have been renamed
        for ext in ['shp', 'dbf', 'prj', 'qml']:
            self.assertTrue(os.path.exists(f'{base_path}/ll.{ext}'))
            self.assertFalse(os.path.exists(f'{base_path}/yyy.{ext}'))
        self.assertTrue(os.path.exists(f'{base_path}/ll.shx'))

        # try renaming missing file
        res, error = QgsFileUtils.renameDataset('/not a file.txt',
                                                f'{base_path}/not a file.txt',
                                                Qgis.FileOperationFlags())
        self.assertFalse(res)
        self.assertTrue(error)