Skip to content

Commit

Permalink
[Fixes #55] Feature: support delete of multiple objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Wallschlaeger committed Nov 10, 2024
2 parents 5746f2a + 6775175 commit 3a4bc4c
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-black.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install `grep black requirements.txt`
pip install `grep black requirements-dev.txt`
- name: run black check
run: |
black --check .
black --check src/
2 changes: 1 addition & 1 deletion .github/workflows/python-flake8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install `grep flake8 requirements.txt`
pip install `grep flake8 requirements-dev.txt`
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/python-mypy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pip install --upgrade pip mypy==1.10.0
pip install -e .
- name: Test with mypy
run: "mypy geonodectl --check-untyped-defs"
run: "mypy -p src.geonoderest --check-untyped-defs"
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = 'geonodectl'
version="0.2.0"
description="a commandline interface tool for Geonode Rest API v2"
authors = [{ name = "marcel wallschlaeger", email="[email protected]" }]
readme = "Readme.md"
readme = "README.md"
requires-python = '>=3.8'
dependencies = [
"requests==2.32.0",
Expand All @@ -24,5 +24,5 @@ dependencies = [
repository = "https://github.com/GeoNodeUserGroup-DE/geonodectl/"

[project.scripts]
geonodectl = "client.cli:geonodectl"
geonodectl = "geonoderest.geonodectl:geonodectl"

File renamed without changes.
2 changes: 1 addition & 1 deletion src/geonoderest/cmdprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import sys

from geonoderest.geonodetypes import GeonodeCmdOutObjectKey
from .geonodetypes import GeonodeCmdOutObjectKey


def show_list(headers: List[str], values: List[List[str]], tablefmt="github"):
Expand Down
10 changes: 5 additions & 5 deletions src/geonoderest/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from pathlib import Path
from typing import List, Dict

from geonoderest.resources import GeonodeResourceHandler
from geonoderest.geonodetypes import GeonodeHTTPFile
from geonoderest.cmdprint import show_list, print_json
from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from geonoderest.executionrequest import GeonodeExecutionRequestHandler
from .resources import GeonodeResourceHandler
from .geonodetypes import GeonodeHTTPFile
from .cmdprint import show_list, print_json
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from .executionrequest import GeonodeExecutionRequestHandler


class GeonodeDatasetsHandler(GeonodeResourceHandler):
Expand Down
8 changes: 4 additions & 4 deletions src/geonoderest/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from pathlib import Path
from typing import List, Optional, Dict

from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from geonoderest.cmdprint import show_list, print_json
from geonoderest.resources import GeonodeResourceHandler
from geonoderest.geonodetypes import GeonodeHTTPFile
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from .cmdprint import show_list, print_json
from .resources import GeonodeResourceHandler
from .geonodetypes import GeonodeHTTPFile


class GeonodeDocumentsHandler(GeonodeResourceHandler):
Expand Down
7 changes: 3 additions & 4 deletions src/geonoderest/executionrequest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Dict, List

from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from geonoderest.geonodeobject import GeonodeObjectHandler as GOH
from geonoderest.rest import GeonodeRest
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from .rest import GeonodeRest

from geonoderest.cmdprint import print_list_on_cmd, print_json
from .cmdprint import print_list_on_cmd, print_json


class GeonodeExecutionRequestHandler(GeonodeRest):
Expand Down
4 changes: 2 additions & 2 deletions src/geonoderest/geoapps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from geonoderest.resources import GeonodeResourceHandler
from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from .resources import GeonodeResourceHandler
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey


class GeonodeGeoappsHandler(GeonodeResourceHandler):
Expand Down
55 changes: 43 additions & 12 deletions src/client/cli.py → src/geonoderest/geonodectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
from argparse import RawTextHelpFormatter
from pathlib import Path

from geonoderest.apiconf import GeonodeApiConf
from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.datasets import GeonodeDatasetsHandler
from geonoderest.resources import (
from .apiconf import GeonodeApiConf
from .geonodeobject import GeonodeObjectHandler
from .datasets import GeonodeDatasetsHandler
from .resources import (
GeonodeResourceHandler,
SUPPORTED_METADATA_TYPES,
DEFAULT_METADATA_TYPE,
)
from geonoderest.documents import GeonodeDocumentsHandler
from geonoderest.maps import GeonodeMapsHandler
from geonoderest.users import GeonodeUsersHandler
from geonoderest.geoapps import GeonodeGeoappsHandler
from geonoderest.uploads import GeonodeUploadsHandler
from geonoderest.executionrequest import GeonodeExecutionRequestHandler
from geonoderest.tkeywords import GeonodeThesauriKeywordsRequestHandler
from geonoderest.tkeywordlabels import GeonodeThesauriKeywordLabelsRequestHandler
from .documents import GeonodeDocumentsHandler
from .maps import GeonodeMapsHandler
from .users import GeonodeUsersHandler
from .geoapps import GeonodeGeoappsHandler
from .uploads import GeonodeUploadsHandler
from .executionrequest import GeonodeExecutionRequestHandler
from .keywords import GeonodeKeywordsRequestHandler
from .tkeywords import GeonodeThesauriKeywordsRequestHandler
from .tkeywordlabels import GeonodeThesauriKeywordLabelsRequestHandler


GEONODECTL_URL_ENV_VAR: str = "GEONODE_API_URL"
Expand Down Expand Up @@ -660,6 +661,34 @@ def geonodectl():
type=str, dest="exec_id", help="exec_id of executionrequest to describe ..."
)

############################
# KEYWORD ARGUMENT PARSING #
############################
keywords = subparsers.add_parser("keywords", help="(Hierarchical) keyword commands")
keywords_subparsers = keywords.add_subparsers(
help="geonodectl keywords commands", dest="subcommand", required=True
)

# LIST
keywords_list = keywords_subparsers.add_parser("list", help="list keywords")
keywords_list.add_argument(
"--filter",
nargs="*",
action=kwargs_append_action,
dest="filter",
type=str,
help="filter keywords requests by key value pairs. E.g. --filter name=soil",
)

# DESCRIBE
keywords_describe = keywords_subparsers.add_parser(
"describe", help="get thesaurikeyword details"
)
# not fully clean to use pk here, as it is actually keyword but for now ...
keywords_describe.add_argument(
type=str, dest="pk", help="keyword of keywords to describe ..."
)

#####################################
# THESAURI KEYWORD ARGUMENT PARSING #
#####################################
Expand Down Expand Up @@ -762,6 +791,8 @@ def geonodectl():
g_obj = GeonodeUploadsHandler(env=geonode_env)
case "executionrequest" | "execrequest":
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
case "keywords" | "keywords":
g_obj = GeonodeKeywordsRequestHandler(env=geonode_env)
case "thesaurikeywords" | "tkeywords":
g_obj = GeonodeThesauriKeywordsRequestHandler(env=geonode_env)
case "thesaurikeywordlabels" | "tkeywordlabels":
Expand Down
24 changes: 12 additions & 12 deletions src/geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List, Dict, Optional
import json

from geonoderest.geonodetypes import GeonodeCmdOutObjectKey, GeonodeCmdOutListKey
from geonoderest.rest import GeonodeRest
from geonoderest.cmdprint import (
from .geonodetypes import GeonodeCmdOutObjectKey, GeonodeCmdOutListKey
from .rest import GeonodeRest
from .cmdprint import (
print_list_on_cmd,
print_json,
json_decode_error_handler,
Expand Down Expand Up @@ -54,28 +54,28 @@ def __parse_delete_pk_string__(self, pk: str) -> List[int]:
if "-" in pk:
try:
pk_begin, pk_end = pk.split("-")
except:
raise ValueError(f"Invalid pk {pk} found, not a range ...")
except ValueError:
SystemExit(f"Invalid pk {pk} found, not a range ...")
if not all(pk.isdigit() for pk in [pk_begin, pk_end]):
raise ValueError(f"Invalid pk {pk} found, not an integer ...")
SystemExit(f"Invalid pk {pk} found, not an integer ...")
return [i for i in range(int(pk_begin), int(pk_end))]

# pk list: 1,2,3,4,5,6,7
elif "," in pk:
pk_list = pk.split(",")
if not all(x.isdigit() for x in pk_list):
raise ValueError(f"Invalid pk {pk} found, not an integer ...")
return pk_list
SystemExit(f"Invalid pk {pk} found, not an integer ...")
return [int(i) for i in pk_list]

# single pk: 1
else:
if not pk.isdigit():
raise ValueError(f"Invalid pk {pk}, is not an integer ...")
return [pk]
SystemExit(f"Invalid pk {pk}, is not an integer ...")
return [int(pk)]

def cmd_delete(self, pk: str, **kwargs):
for pk in self.__parse_delete_pk_string__(pk):
self.delete(pk=int(pk), **kwargs)
for _pk in self.__parse_delete_pk_string__(pk):
self.delete(pk=_pk, **kwargs)
print(f"{self.JSON_OBJECT_NAME}: {pk} deleted ...")

def delete(self, pk: int, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions src/geonoderest/keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List

from .geonodetypes import (
GeonodeCmdOutListKey,
GeonodeCmdOutObjectKey,
)
from .geonodeobject import GeonodeObjectHandler


class GeonodeKeywordsRequestHandler(GeonodeObjectHandler):
ENDPOINT_NAME = "keywords"
JSON_OBJECT_NAME = "keywords"
SINGULAR_RESOURCE_NAME = "keywords"

LIST_CMDOUT_HEADER: List[GeonodeCmdOutObjectKey] = [
GeonodeCmdOutListKey(key="id"),
GeonodeCmdOutListKey(key="name"),
GeonodeCmdOutListKey(key="slug"),
GeonodeCmdOutListKey(key="link"),
]
10 changes: 5 additions & 5 deletions src/geonoderest/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from typing import List, Dict, Optional

from geonoderest.cmdprint import print_json, json_decode_error_handler
from geonoderest.datasets import GeonodeDatasetsHandler
from geonoderest.resources import GeonodeResourceHandler
from geonoderest.geonodetypes import (
from .cmdprint import print_json, json_decode_error_handler
from .datasets import GeonodeDatasetsHandler
from .resources import GeonodeResourceHandler
from .geonodetypes import (
GeonodeCmdOutListKey,
GeonodeCmdOutDictKey,
)
Expand Down Expand Up @@ -116,7 +116,7 @@ def create(
}

maplayers_list.append(maplayer)
except Exception as err:
except Exception:
logging.error(f"dataset {maplayer_pk} not found ...")
base_json_content = {
"ressource_type": self.SINGULAR_RESOURCE_NAME,
Expand Down
4 changes: 2 additions & 2 deletions src/geonoderest/resources.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import List
import requests

from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from .geonodeobject import GeonodeObjectHandler
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey

SUPPORTED_METADATA_TYPES: List[str] = [
"Atom",
Expand Down
4 changes: 2 additions & 2 deletions src/geonoderest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import requests
import logging

from geonoderest.geonodetypes import GeonodeHTTPFile
from geonoderest.apiconf import GeonodeApiConf
from .geonodetypes import GeonodeHTTPFile
from .apiconf import GeonodeApiConf

urllib3.disable_warnings()

Expand Down
4 changes: 2 additions & 2 deletions src/geonoderest/tkeywordlabels.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List

from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from geonoderest.geonodeobject import GeonodeObjectHandler
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from .geonodeobject import GeonodeObjectHandler


class GeonodeThesauriKeywordLabelsRequestHandler(GeonodeObjectHandler):
Expand Down
6 changes: 3 additions & 3 deletions src/geonoderest/tkeywords.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Dict, List
from typing import List

from geonoderest.geonodetypes import (
from .geonodetypes import (
GeonodeCmdOutListKey,
GeonodeCmdOutObjectKey,
GeonodeCmdOutDictKey,
)
from geonoderest.geonodeobject import GeonodeObjectHandler
from .geonodeobject import GeonodeObjectHandler


class GeonodeThesauriKeywordsRequestHandler(GeonodeObjectHandler):
Expand Down
4 changes: 2 additions & 2 deletions src/geonoderest/uploads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey
from .geonodeobject import GeonodeObjectHandler
from .geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutObjectKey


class GeonodeUploadsHandler(GeonodeObjectHandler):
Expand Down
8 changes: 4 additions & 4 deletions src/geonoderest/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import logging
from typing import Dict, Optional

from geonoderest.resources import GeonodeResourceHandler
from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.geonodetypes import GeonodeCmdOutListKey
from geonoderest.cmdprint import (
from .resources import GeonodeResourceHandler
from .geonodeobject import GeonodeObjectHandler
from .geonodetypes import GeonodeCmdOutListKey
from .cmdprint import (
print_list_on_cmd,
print_json,
json_decode_error_handler,
Expand Down

0 comments on commit 3a4bc4c

Please sign in to comment.