Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #36] add create map feature #37

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions .github/workflows/python-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 mypy black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: run black check
run: |
black --check .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with mypy
run: "mypy geonodectl --check-untyped-defs"
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 mypy black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: run black check
run: |
black --check .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with mypy
run: "mypy geonodectl --check-untyped-defs"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Currently not all features of the API are implemented. Here is a list of what yo
| resource | list, delete, download metadata |
| dataset | list, delete, patch, describe, upload |
| documents | list, delete, patch, describe, upload |
| maps | list, delete, patch, describe |
| maps | list, delete, patch, describe, create |
| geoapps | list, delete, patch, describe |
| people | list, delete, patch, describe |
| uploads | list |
Expand Down
96 changes: 57 additions & 39 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ from pathlib import Path
from geonoderest.apiconf import GeonodeApiConf
from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.datasets import GeonodeDatasetsHandler
from geonoderest.resources import GeonodeResourceHandler, SUPPORTED_METADATA_TYPES, DEFAULT_METADATA_TYPE
from geonoderest.resources import (
GeonodeResourceHandler,
SUPPORTED_METADATA_TYPES,
DEFAULT_METADATA_TYPE,
)
from geonoderest.documents import GeonodeDocumentsHandler
from geonoderest.maps import GeonodeMapsHandler
from geonoderest.people import GeonodePeopleHandler
Expand Down Expand Up @@ -135,9 +139,7 @@ To use this tool you have to set the following environment variables before star

# DELETE
resource_delete = resource_subparsers.add_parser("delete", help="delete resource")
resource_delete.add_argument(
type=int, dest="pk", help="pk of resource to delete"
)
resource_delete.add_argument(type=int, dest="pk", help="pk of resource to delete")

# METADATA
resource_metadata = resource_subparsers.add_parser(
Expand Down Expand Up @@ -201,7 +203,7 @@ To use this tool you have to set the following environment variables before star
default=DEFAULT_CHARSET,
help="uploads dataset as timeseries",
)

datasets_upload.add_argument(
"--mosaic",
action="store_true",
Expand All @@ -213,14 +215,12 @@ To use this tool you have to set the following environment variables before star
datasets_patch = datasets_subparsers.add_parser(
"patch", help="patch datasets metadata"
)
datasets_patch.add_argument(
type=int, dest="pk", help="pk of dataset to patch"
)
datasets_patch.add_argument(type=int, dest="pk", help="pk of dataset to patch")
datasets_patch.add_argument(
"--set",
dest="fields",
type=str,
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

datasets_patch.add_argument(
Expand Down Expand Up @@ -284,17 +284,14 @@ To use this tool you have to set the following environment variables before star
documents_patch = documents_subparsers.add_parser(
"patch", help="patch documents metadata"
)
documents_patch.add_argument(
type=int, dest="pk", help="pk of document to patch"
)
documents_patch.add_argument(type=int, dest="pk", help="pk of document to patch")
documents_patch.add_argument(
"--set",
dest="fields",
type=str,
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)


# DESCRIBE
documents_describe = documents_subparsers.add_parser(
"describe", help="get document details"
Expand Down Expand Up @@ -323,26 +320,53 @@ To use this tool you have to set the following environment variables before star

# PATCH
maps_patch = maps_subparsers.add_parser("patch", help="patch maps metadata")
maps_patch.add_argument(
type=int, dest="pk", help="pk of map to patch"
)
maps_patch.add_argument(type=int, dest="pk", help="pk of map to patch")
maps_patch.add_argument(
"--set",
dest="fields",
type=str,
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

# DESCRIBE
maps_describe = maps_subparsers.add_parser("describe", help="get map details")
maps_describe.add_argument(
type=int, dest="pk", help="pk of map to describe ..."
)
maps_describe.add_argument(type=int, dest="pk", help="pk of map to describe ...")

# DELETE
maps_delete = maps_subparsers.add_parser("delete", help="delete existing map")
maps_delete.add_argument(
type=int, dest="pk", help="pk of map to delete ..."
maps_delete.add_argument(type=int, dest="pk", help="pk of map to delete ...")

# CREATE
maps_create = maps_subparsers.add_parser("create", help="create an (empty) map")

maps_create.add_argument(
"--title",
type=str,
required=True,
dest="title",
help="title of the new dataset ...",
)

maps_create.add_argument(
"--set",
dest="fields",
type=str,
help='add metadata by providing a json string like: \'\'{ "category": {"identifier": "farming"}, "abstract": "test abstract" }\'\'',
)

maps_create.add_argument(
"--json_path",
dest="json_path",
type=str,
help="add metadata by providing a path to a json file",
)

maps_create.add_argument(
"--maplayers",
nargs="+",
dest="maplayers",
type=int,
help="space seperate list of integers of pks to add as maplayer to the map",
)

############################
Expand All @@ -362,14 +386,12 @@ To use this tool you have to set the following environment variables before star
geoapps_patch = geoapps_subparsers.add_parser(
"patch", help="patch geoapps metadata"
)
geoapps_patch.add_argument(
type=int, dest="pk", help="pk of geoapp to patch"
)
geoapps_patch.add_argument(type=int, dest="pk", help="pk of geoapp to patch")
geoapps_patch.add_argument(
"--set",
dest="fields",
type=str,
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

# DESCRIBE
Expand All @@ -384,9 +406,7 @@ To use this tool you have to set the following environment variables before star
geoapps_delete = geoapps_subparsers.add_parser(
"delete", help="delete existing geoapp"
)
geoapps_delete.add_argument(
type=int, dest="pk", help="pk of geoapp to delete ..."
)
geoapps_delete.add_argument(type=int, dest="pk", help="pk of geoapp to delete ...")

##########################
# USERS ARGUMENT PARSING #
Expand All @@ -400,14 +420,12 @@ To use this tool you have to set the following environment variables before star

# PATCH
people_patch = people_subparsers.add_parser("patch", help="patch users metadata")
people_patch.add_argument(
type=int, dest="pk", help="pk of user to patch"
)
people_patch.add_argument(type=int, dest="pk", help="pk of user to patch")
people_patch.add_argument(
"--set",
dest="fields",
type=str,
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

# DESCRIBE
Expand Down Expand Up @@ -440,9 +458,7 @@ To use this tool you have to set the following environment variables before star

# DELETE
people_delete = people_subparsers.add_parser("delete", help="delete existing user")
people_delete.add_argument(
type=int, dest="pk", help="pk of geoapp to delete ..."
)
people_delete.add_argument(type=int, dest="pk", help="pk of geoapp to delete ...")

###########################
# UPLOAD ARGUMENT PARSING #
Expand All @@ -458,7 +474,9 @@ To use this tool you have to set the following environment variables before star
#####################################
# EXECUTIONREQUEST ARGUMENT PARSING #
#####################################
executionrequest = subparsers.add_parser("executionrequest", help="executionrequest commands")
executionrequest = subparsers.add_parser(
"executionrequest", help="executionrequest commands"
)
executionrequest_subparsers = executionrequest.add_subparsers(
help="geonodectl executionrequest commands", dest="subcommand", required=True
)
Expand Down Expand Up @@ -511,7 +529,7 @@ To use this tool you have to set the following environment variables before star
case "uploads":
g_obj = GeonodeUploadsHandler(env=geonode_env)
case "executionrequest" | "execrequest":
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
case _:
raise NotImplemented

Expand Down
6 changes: 5 additions & 1 deletion geonoderest/apiconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def from_env_vars() -> "GeonodeApiConf":
"""
Creates a new GeonodeApiConf object from environment variables
"""
if not "GEONODE_API_URL" in os.environ or "GEONODE_API_BASIC_AUTH" not in os.environ:
if (
not "GEONODE_API_URL" in os.environ
or "GEONODE_API_BASIC_AUTH" not in os.environ
):

raise SystemExit(
"env vars not set: GEONODE_API_URL, GEONODE_API_BASIC_AUTH"
)
Expand Down
8 changes: 8 additions & 0 deletions geonoderest/cmdprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List, Union, Dict
from tabulate import tabulate
import json
import logging
import sys

from geonoderest.geonodetypes import GeonodeCmdOutObjectKey

Expand Down Expand Up @@ -50,3 +52,9 @@ def print_json(json_str: Union[str, dict]):
None
"""
print(json.dumps(json_str, indent=2))


def json_decode_error_handler(json_str: str, error: json.decoder.JSONDecodeError):
logging.error(f"Error decoding json string:\n {json_str} ...")
logging.error(f"{error}")
sys.exit(1)
4 changes: 2 additions & 2 deletions geonoderest/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def cmd_upload(
file_path: Path,
metadata_only: bool = False,
charset: str = "UTF-8",
**kwargs
**kwargs,
):
"""upload data and show them on the cmdline

Expand Down Expand Up @@ -60,7 +60,7 @@ def upload(
file_path: Path,
charset: str = "UTF-8",
metadata_only: bool = False,
**kwargs
**kwargs,
) -> Dict:
"""upload a document to geonode

Expand Down
58 changes: 37 additions & 21 deletions geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

from geonoderest.geonodetypes import GeonodeCmdOutObjectKey, GeonodeCmdOutListKey
from geonoderest.rest import GeonodeRest
from geonoderest.cmdprint import print_list_on_cmd, print_json
from geonoderest.cmdprint import (
print_list_on_cmd,
print_json,
json_decode_error_handler,
)


class GeonodeObjectHandler(GeonodeRest):
Expand Down Expand Up @@ -49,6 +53,8 @@ def delete(self, pk: int, **kwargs):
def cmd_patch(
self,
pk: int,
fields: Optional[str] = None,
json_path: Optional[str] = None,
**kwargs,
):
"""
Expand All @@ -61,33 +67,43 @@ def cmd_patch(
Raises:
ValueError: catches json.decoder.JSONDecodeError and raises ValueError as decoding is not working
"""
obj = self.patch(pk=pk, **kwargs)

json_content: Dict = {}
if json_path:
with open(json_path, "r") as file:
try:
j_dict = json.load(file)
except json.decoder.JSONDecodeError as E:
json_decode_error_handler(str(file), E)

if "attribute_set" in j_dict:
j_dict.pop("attribute_set", None)
json_content = {**json_content, **j_dict}

if fields:
try:
f_dict = json.loads(fields)
json_content = {**json_content, **f_dict}
except json.decoder.JSONDecodeError as E:
json_decode_error_handler(fields, E)

if json_content == {}:
raise ValueError(
"At least one of 'fields' or 'json_path' must be provided."
)

obj = self.patch(pk=pk, json_content=json_content, **kwargs)
print_json(obj)

def patch(
self,
pk: int,
fields: Optional[str] = None,
json_path: Optional[str] = None,
json_content: Optional[Dict] = None,
**kwargs,
):
if json_path:
with open(json_path, "r") as file:
fields_dict = json.load(file)
if "attribute_set" in fields_dict:
fields_dict["data"] = {
"attribute_set": fields_dict["attribute_set"]
}
fields_dict.pop("attribute_set", None)
obj = self.http_patch(
endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=fields_dict
)
elif fields:
obj = self.http_patch(endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=fields)
else:
raise ValueError(
"At least one of 'fields' or 'json_path' must be provided."
)
obj = self.http_patch(
endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_content
)
return obj

def cmd_describe(self, pk: int, **kwargs):
Expand Down
Loading
Loading