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

A new parameter to read json files to patch dataset metadata (including attributes) #30

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ dmypy.json

# Pyre type checker
.pyre/

# VSCode
.vscode

# JSON
*.json
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ geonodectl ds patch 36 --set 'category={"identifier": "farming"}'
...
```

patch dataset from jsonb:
```
geonodectl ds patch 36 --json_path 'path_to/your_json_with_attributes_to_patch.json'
```

delete dataset:
```
❯ ./geonodectl ds delete 36
Expand Down
6 changes: 6 additions & 0 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ To use this tool you have to set the following environment variables before star
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
)

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

# DESCRIBE
datasets_describe = datasets_subparsers.add_parser(
Expand Down
55 changes: 37 additions & 18 deletions geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Dict
from typing import List, Dict, Optional
import json

from geonoderest.geonodetypes import GeonodeCmdOutObjectKey, GeonodeCmdOutListKey
Expand Down Expand Up @@ -46,33 +46,52 @@ def delete(self, pk: int, **kwargs):
self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}")
self.http_delete(endpoint=f"resources/{pk}/delete")

def cmd_patch(self, pk: int, fields: str, **kwargs):
obj = self.patch(pk, fields, **kwargs)
print_json(obj)

def patch(self, pk: int, fields: str, **kwargs) -> Dict:
"""tries to generate object from incoming json string

def cmd_patch(
self,
pk: int,
fields: Optional[str] = None,
json_path: Optional[str] = None,
**kwargs,
):
"""
Tries to generate object from incoming json string
Args:
pk (int): pk of the object
fields (str): string of potential json object

Returns:
Dict: obj details
json_path (str): path to a json file

Raises:
ValueError: catches json.decoder.JSONDecodeError and raises ValueError as decoding is not working
"""
try:
json_data = json.loads(fields)
except ValueError:
raise (
ValueError(
f"unable to decode argument: | {fields} | to json object ..."
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:
try:
json_data = json.loads(fields)
except ValueError:
raise (
ValueError(
f"unable to decode argument: | {fields} | to json object ..."
)
)
obj = self.http_patch(
endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_data
)
else:
raise ValueError(
"At least one of 'fields' or 'json_path' must be provided."
)

return self.http_patch(endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_data)
print_json(obj)

def cmd_describe(self, pk: int, **kwargs):
obj = self.get(pk=pk, **kwargs)
Expand Down