Skip to content

Commit

Permalink
Initial support for basic Kart processing opertions.
Browse files Browse the repository at this point in the history
  - Init and clone repositories
  - Create, switch and delete branches
  • Loading branch information
hamishcampbell committed Aug 5, 2024
1 parent a7748f2 commit 167e77d
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 2 deletions.
1 change: 0 additions & 1 deletion kart/kartapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import locale
import math
import os
import re
import subprocess
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
13 changes: 12 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 @@ -12,6 +12,8 @@
from kart.gui.settingsdialog import SettingsDialog
from kart.kartapi import checkKartInstalled, kartVersionDetails
from kart.layers import LayerTracker
from kart.processing.provider import KartProvider


pluginPath = os.path.dirname(__file__)

Expand All @@ -28,6 +30,11 @@ def icon(f):
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 @@ -52,6 +59,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 @@ -96,3 +105,5 @@ def unload(self):

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

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


from kart.processing.tools import (
RepoClone,
RepoInit,
RepoSwitchBranch,
RepoCreateBranch,
RepoDeleteBranch,
)


class KartProvider(QgsProcessingProvider):

def loadAlgorithms(self, *args, **kwargs):

self.addAlgorithm(RepoInit())
self.addAlgorithm(RepoClone())
self.addAlgorithm(RepoSwitchBranch())
self.addAlgorithm(RepoCreateBranch())
self.addAlgorithm(RepoDeleteBranch())

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

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

def icon(self):
from kart.plugin import kartIcon

return kartIcon
245 changes: 245 additions & 0 deletions kart/processing/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterString,
QgsProcessingParameterFolderDestination,
)


class KartAlgorithm(QgsProcessingAlgorithm):
def createInstance(self):
return self.__class__()

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

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


class RepoInit(KartAlgorithm):
REPO_PATH = "REPO_PATH"

def displayName(self):
return self.tr("Create Empty Repo")

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

def group(self):
return self.tr("Repositories")

def groupId(self):
return "kart_repositories"

def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterString(
self.REPO_PATH,
self.tr("Repo Path"),
)
)

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

repo_path = self.parameterAsString(parameters, self.REPO_PATH, context)

repo = Repository(repo_path)
repo.init()

return {self.REPO_PATH: repo_path}


class RepoClone(KartAlgorithm):
REPO_CLONE_URL = "REPO_CLONE_URL"
REPO_CLONE_REFISH = "REPO_CLONE_REFISH"
REPO_OUTPUT_FOLDER = "REPO_OUTPUT_FOLDER"

def displayName(self):
return self.tr("Clone Repo")

def shortHelpString(self):
return self.tr("Clones a repository to a folder")

def group(self):
return self.tr("Repositories")

def groupId(self):
return "kart_repositories"

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterString(
self.REPO_CLONE_URL,
self.tr("Repo URL"),
)
)

self.addParameter(
QgsProcessingParameterString(
self.REPO_CLONE_REFISH,
self.tr("Branch/Tag/Refish"),
optional=True,
)
)

self.addParameter(
QgsProcessingParameterFolderDestination(
self.REPO_OUTPUT_FOLDER, self.tr("Output folder")
)
)

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

repo_url = self.parameterAsString(parameters, self.REPO_CLONE_URL, context)
folder = self.parameterAsString(parameters, self.REPO_OUTPUT_FOLDER, context)
refish = self.parameterAsString(parameters, self.REPO_CLONE_REFISH, context)

repo = Repository.clone(repo_url, folder)
if refish:
repo.checkoutBranch(refish)

return {
self.REPO_OUTPUT_FOLDER: folder,
}


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 group(self):
return self.tr("Branches")

def groupId(self):
return "kart_branches"

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterString(
self.REPO_PATH,
self.tr("Repo Path"),
)
)

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.parameterAsString(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("Swtich to Branch")

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

def group(self):
return self.tr("Branches")

def groupId(self):
return "kart_branches"

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterString(
self.REPO_PATH,
self.tr("Repo Path"),
)
)

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.parameterAsString(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 group(self):
return self.tr("Branches")

def groupId(self):
return "kart_branches"

def initAlgorithm(self, config=None):

self.addParameter(
QgsProcessingParameterString(
self.REPO_PATH,
self.tr("Repo Path"),
)
)

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.parameterAsString(parameters, self.REPO_PATH, context)
branch_name = self.parameterAsString(parameters, self.REPO_REFISH, context)

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

return {
self.REPO_PATH: repo_path,
}

0 comments on commit 167e77d

Please sign in to comment.