Skip to content

Commit

Permalink
Use QGIS' utility function to detect and copy sidecar files (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored Jul 23, 2024
1 parent 528345d commit 9d2faac
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions libqfieldsync/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from typing import Dict, List, Optional

from qgis.core import (
Qgis,
QgsAttributeEditorField,
QgsCoordinateTransformContext,
QgsDataSourceUri,
QgsFields,
QgsFileUtils,
QgsMapLayer,
QgsProject,
QgsProviderMetadata,
Expand Down Expand Up @@ -58,7 +60,6 @@ class UnsupportedPrimaryKeyError(Exception):
def get_file_extension_group(filename):
"""
Return the basename and an extension group (if applicable)
Examples:
airports.shp -> 'airport', ['.shp', '.shx', '.dbf', '.sbx', '.sbn', '.shp.xml']
forests.gpkg -> 'forests', ['.gpkg']
Expand Down Expand Up @@ -1016,15 +1017,29 @@ def copy(self, target_path, copied_files, keep_existent=False):
suffix = uri_parts[1]

if self.is_file:
source_path, file_name = os.path.split(self.filename)
basename, extensions = get_file_extension_group(file_name)
for ext in extensions:
dest_file = os.path.join(target_path, basename + ext)
if os.path.exists(os.path.join(source_path, basename + ext)) and (
keep_existent is False or not os.path.isfile(dest_file)
):
shutil.copy(os.path.join(source_path, basename + ext), dest_file)
if Qgis.QGIS_VERSION_INT > 32200:
# QGIS >= 3.22
files_to_copy = QgsFileUtils.sidecarFilesForPath(self.filename)
files_to_copy.add(self.filename)
for file_to_copy in files_to_copy:
source_path, file_name = os.path.split(file_to_copy)
dest_file = os.path.join(target_path, file_name)
if keep_existent is False or not os.path.isfile(dest_file):
shutil.copy(os.path.join(source_path, file_name), dest_file)
else:
# QGIS < 3.22
source_path, file_name = os.path.split(self.filename)
basename, extensions = get_file_extension_group(file_name)
for ext in extensions:
dest_file = os.path.join(target_path, basename + ext)
if os.path.exists(os.path.join(source_path, basename + ext)) and (
keep_existent is False or not os.path.isfile(dest_file)
):
shutil.copy(
os.path.join(source_path, basename + ext), dest_file
)

source_path, file_name = os.path.split(self.filename)
new_source = ""
metadata = self.metadata

Expand Down

0 comments on commit 9d2faac

Please sign in to comment.