Skip to content

Commit

Permalink
Initial processing support for the Kart plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishcampbell committed Aug 14, 2024
1 parent b0fa22c commit b469738
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 4 deletions.
13 changes: 10 additions & 3 deletions kart/kartapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def generate_clone_arguments(
dst: str,
location: Optional[str] = None,
extent: Optional[QgsReferencedRectangle] = None,
depth: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
) -> List[str]:
Expand All @@ -313,6 +314,8 @@ def generate_clone_arguments(
if extent is not None:
kart_extent = f"{extent.crs().authid()};{extent.asWktPolygon()}"
commands.extend(["--spatial-filter", kart_extent])
if depth is not None:
commands.extend(["--depth", str(depth)])

return commands

Expand All @@ -322,6 +325,7 @@ def clone(
dst: str,
location: Optional[str] = None,
extent: Optional[QgsReferencedRectangle] = None,
depth: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
output_handler: Callable[[str], None] = None,
Expand All @@ -330,7 +334,7 @@ def clone(
Performs a (blocking, main thread only) clone operation
"""
commands = Repository.generate_clone_arguments(
src, dst, location, extent, username, password
src, dst, location, extent, depth, username, password
)
executeKart(commands, feedback=output_handler)
return Repository(dst)
Expand Down Expand Up @@ -396,8 +400,11 @@ def init(self, location=None):
else:
self.executeKart(["init"])

def importIntoRepo(self, source):
self.executeKart(["import", source])
def importIntoRepo(self, source, dataset=None):
importArgs = [source]
if dataset:
importArgs += ["--dataset", dataset]
self.executeKart(["import"] + importArgs)

def checkUserConfigured(self):
configDict = self._config()
Expand Down
1 change: 1 addition & 0 deletions kart/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ repository=https://github.com/koordinates/kart-qgis-plugin
tracker=https://github.com/koordinates/kart-qgis-plugin/issues
icon=img/kart.png
category=Plugins
hasProcessingProvider=yes
12 changes: 11 additions & 1 deletion kart/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import platform

from qgis.core import QgsProject, Qgis, QgsMessageOutput
from qgis.core import QgsApplication, QgsProject, Qgis, QgsMessageOutput

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QAction
Expand All @@ -11,6 +11,7 @@
from kart.gui.settingsdialog import SettingsDialog
from kart.kartapi import checkKartInstalled, kartVersionDetails
from kart.layers import LayerTracker
from kart.processing import KartProvider


pluginPath = os.path.dirname(__file__)
Expand All @@ -19,6 +20,11 @@
class KartPlugin(object):
def __init__(self, iface):
self.iface = iface
self.provider = None

def initProcessing(self):
self.provider = KartProvider()
QgsApplication.processingRegistry().addProvider(self.provider)

def initGui(self):

Expand All @@ -43,6 +49,8 @@ def initGui(self):
QgsProject.instance().layerWasAdded.connect(self.tracker.layerAdded)
QgsProject.instance().crsChanged.connect(self.tracker.updateRubberBands)

self.initProcessing()

def showDock(self):
if checkKartInstalled():
self.dock.show()
Expand Down Expand Up @@ -87,3 +95,5 @@ def unload(self):

QgsProject.instance().layerRemoved.disconnect(self.tracker.layerRemoved)
QgsProject.instance().layerWasAdded.disconnect(self.tracker.layerAdded)

QgsApplication.processingRegistry().removeProvider(self.provider)
32 changes: 32 additions & 0 deletions kart/processing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from qgis.core import QgsProcessingProvider

from kart.gui import icons

from .branches import RepoCreateBranch, RepoDeleteBranch, RepoSwitchBranch
from .data import RepoImportData
from .remotes import RepoPullFromRemote, RepoPushToRemote
from .repos import RepoClone, RepoInit
from .tags import RepoCreateTag


class KartProvider(QgsProcessingProvider):
def loadAlgorithms(self, *args, **kwargs):

self.addAlgorithm(RepoInit())
self.addAlgorithm(RepoClone())
self.addAlgorithm(RepoCreateTag())
self.addAlgorithm(RepoSwitchBranch())
self.addAlgorithm(RepoCreateBranch())
self.addAlgorithm(RepoDeleteBranch())
self.addAlgorithm(RepoImportData())
self.addAlgorithm(RepoPullFromRemote())
self.addAlgorithm(RepoPushToRemote())

def id(self, *args, **kwargs):
return "Kart"

def name(self, *args, **kwargs):
return self.tr("Kart")

def icon(self):
return icons.kartIcon
16 changes: 16 additions & 0 deletions kart/processing/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProcessingAlgorithm


class KartAlgorithm(QgsProcessingAlgorithm):
def createInstance(self):
return type(self)()

def name(self):
return f"kart_{self.__class__.__name__.lower()}"

def tr(self, string):
return QCoreApplication.translate("Processing", string)

def initAlgorithm(self, config=None):
return {}
136 changes: 136 additions & 0 deletions kart/processing/branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from qgis.core import QgsProcessingParameterFile, QgsProcessingParameterString
from kart.gui import icons

from .base import KartAlgorithm


class RepoCreateBranch(KartAlgorithm):
REPO_PATH = "REPO_PATH"
REPO_BRANCH_NAME = "REPO_BRANCH_NAME"

def displayName(self):
return self.tr("Create Branch")

def shortHelpString(self):
return self.tr("Create a new branch")

def icon(self):
return icons.createBranchIcon

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterFile(
self.REPO_PATH,
self.tr("Repo Path"),
behavior=QgsProcessingParameterFile.Folder,
)
)

self.addParameter(
QgsProcessingParameterString(
self.REPO_BRANCH_NAME,
self.tr("Branch Name"),
)
)

def processAlgorithm(self, parameters, context, feedback):
from kart.kartapi import Repository

repo_path = self.parameterAsFile(parameters, self.REPO_PATH, context)
branch_name = self.parameterAsString(parameters, self.REPO_REFISH, context)

repo = Repository(repo_path)
repo.createBranch(branch_name)

return {
self.REPO_PATH: repo_path,
}


class RepoSwitchBranch(KartAlgorithm):
REPO_PATH = "REPO_PATH"
REPO_BRANCH_NAME = "REPO_BRANCH_NAME"

def displayName(self):
return self.tr("Switch to Branch")

def shortHelpString(self):
return self.tr("Switches to a named branch")

def icon(self):
return icons.checkoutIcon

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterFile(
self.REPO_PATH,
self.tr("Repo Path"),
behavior=QgsProcessingParameterFile.Folder,
)
)

self.addParameter(
QgsProcessingParameterString(
self.REPO_BRANCH_NAME,
self.tr("Branch Name"),
)
)

def processAlgorithm(self, parameters, context, feedback):
from kart.kartapi import Repository

repo_path = self.parameterAsFile(parameters, self.REPO_PATH, context)
branch_name = self.parameterAsString(parameters, self.REPO_BRANCH_NAME, context)

repo = Repository(repo_path)
repo.checkoutBranch(branch_name)

return {
self.REPO_PATH: repo_path,
}


class RepoDeleteBranch(KartAlgorithm):
REPO_PATH = "REPO_PATH"
REPO_BRANCH_NAME = "REPO_BRANCH_NAME"

def displayName(self):
return self.tr("Delete Branch")

def shortHelpString(self):
return self.tr("Delete a branch")

def icon(self):
return icons.deleteIcon

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterFile(
self.REPO_PATH,
self.tr("Repo Path"),
behavior=QgsProcessingParameterFile.Folder,
)
)

self.addParameter(
QgsProcessingParameterString(
self.REPO_BRANCH_NAME,
self.tr("Branch Name"),
)
)

def processAlgorithm(self, parameters, context, feedback):
from kart.kartapi import Repository

repo_path = self.parameterAsFile(parameters, self.REPO_PATH, context)
branch_name = self.parameterAsString(parameters, self.REPO_BRANCH_NAME, context)

repo = Repository(repo_path)
repo.deleteBranch(branch_name)

return {
self.REPO_PATH: repo_path,
}
73 changes: 73 additions & 0 deletions kart/processing/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from qgis.core import (
QgsProcessingParameterFile,
QgsProcessingParameterString,
QgsProcessingOutputFolder,
)
from kart.gui import icons

from .base import KartAlgorithm


class RepoImportData(KartAlgorithm):
REPO_PATH = "REPO_PATH"
REPO_DATA_PATH = "REPO_DATA_PATH"
REPO_DATASET_NAME = "REPO_DATASET_NAME"

def displayName(self):
return self.tr("Import Data")

def shortHelpString(self):
return self.tr("Import data into a repository")

def icon(self):
return icons.importIcon

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterFile(
self.REPO_PATH,
self.tr("Repo Path"),
behavior=QgsProcessingParameterFile.Folder,
)
)

self.addParameter(
QgsProcessingParameterFile(
self.REPO_DATA_PATH,
self.tr("Data Path"),
behavior=QgsProcessingParameterFile.File,
)
)

self.addParameter(
QgsProcessingParameterString(
self.REPO_DATASET_NAME,
self.tr("Dataset Name"),
optional=True,
)
)

self.addOutput(
QgsProcessingOutputFolder(
self.REPO_PATH,
self.tr("Repo Path"),
)
)

def processAlgorithm(self, parameters, context, feedback):
from kart.kartapi import Repository

repo_path = self.parameterAsFile(parameters, self.REPO_PATH, context)
data_path = self.parameterAsFile(parameters, self.REPO_DATA_PATH, context)
dataset_name = self.parameterAsString(
parameters, self.REPO_DATASET_NAME, context
)

repo = Repository(repo_path)
repo.importIntoRepo(data_path, dataset_name)

return {
self.REPO_PATH: repo_path,
self.REPO_DATASET_NAME: dataset_name,
}
Loading

0 comments on commit b469738

Please sign in to comment.