14
14
class DatastoreEncoder (json .JSONEncoder ):
15
15
"""Custom JSON encoder for datastore values."""
16
16
17
- def default (self , obj ) :
17
+ def default (self , obj : object ) -> object :
18
18
if isinstance (obj , datetime .datetime ):
19
19
return obj .isoformat ()
20
20
if isinstance (obj , decimal .Decimal ):
21
21
return str (obj )
22
22
return json .JSONEncoder .default (self , obj )
23
23
24
24
25
- def delete_datastore_resource (resource_id ) :
25
+ def delete_datastore_resource (resource_id : str ) -> None :
26
26
"""Delete a resource from datastore."""
27
27
try :
28
28
tk .get_action ("datastore_delete" )(
@@ -32,7 +32,7 @@ def delete_datastore_resource(resource_id):
32
32
raise utils .JobError ("Deleting existing datastore failed." )
33
33
34
34
35
- def delete_resource (resource_id ) :
35
+ def delete_resource (resource_id : str ) -> None :
36
36
"""Delete a resource from CKAN."""
37
37
try :
38
38
tk .get_action ("resource_delete" )(
@@ -42,7 +42,7 @@ def delete_resource(resource_id):
42
42
raise utils .JobError ("Deleting existing resource failed." )
43
43
44
44
45
- def datastore_resource_exists (resource_id ) :
45
+ def datastore_resource_exists (resource_id : str ) -> dict :
46
46
"""Check if a resource exists in datastore."""
47
47
data_dict = {
48
48
"resource_id" : resource_id ,
@@ -56,12 +56,17 @@ def datastore_resource_exists(resource_id):
56
56
result = tk .get_action ("datastore_search" )(context , data_dict )
57
57
return result
58
58
except tk .ObjectNotFound :
59
- return False
59
+ return None
60
60
61
61
62
62
def send_resource_to_datastore (
63
- resource , resource_id , headers , records , aliases , calculate_record_count
64
- ):
63
+ resource : dict ,
64
+ resource_id : str ,
65
+ headers : list ,
66
+ records : list ,
67
+ aliases : list ,
68
+ calculate_record_count : bool ,
69
+ ) -> dict :
65
70
"""Store records in CKAN datastore."""
66
71
if resource_id :
67
72
# used to create the "main" resource
@@ -91,7 +96,7 @@ def send_resource_to_datastore(
91
96
raise utils .JobError ("Error sending data to datastore ({!s})." .format (e ))
92
97
93
98
94
- def upload_resource (new_resource , file ) :
99
+ def upload_resource (new_resource : dict , file : str ) -> None :
95
100
"""Upload a new resource to CKAN."""
96
101
site_user = tk .get_action ("get_site_user" )({"ignore_auth" : True }, {})
97
102
context = {
@@ -109,7 +114,7 @@ def upload_resource(new_resource, file):
109
114
raise utils .JobError ("Creating resource failed." )
110
115
111
116
112
- def update_resource (resource ) :
117
+ def update_resource (resource : dict ) -> None :
113
118
"""Update resource metadata."""
114
119
site_user = tk .get_action ("get_site_user" )({"ignore_auth" : True }, {})
115
120
context = {"ignore_auth" : True , "user" : site_user ["name" ], "auth_user_obj" : None }
@@ -119,23 +124,23 @@ def update_resource(resource):
119
124
raise utils .JobError ("Updating existing resource failed." )
120
125
121
126
122
- def get_resource (resource_id ) :
127
+ def get_resource (resource_id : str ) -> dict :
123
128
"""Get available information about the resource from CKAN."""
124
129
resource_dict = tk .get_action ("resource_show" )(
125
130
{"ignore_auth" : True }, {"id" : resource_id }
126
131
)
127
132
return resource_dict
128
133
129
134
130
- def get_package (package_id ) :
135
+ def get_package (package_id : str ) -> dict :
131
136
"""Get available information about a package from CKAN."""
132
137
dataset_dict = tk .get_action ("package_show" )(
133
138
{"ignore_auth" : True }, {"id" : package_id }
134
139
)
135
140
return dataset_dict
136
141
137
142
138
- def resource_exists (package_id , resource_name ) :
143
+ def resource_exists (package_id : str , resource_name : str ) -> tuple [ bool , str | None ] :
139
144
"""
140
145
Check if a resource name exists in a package.
141
146
Returns:
@@ -151,15 +156,21 @@ def resource_exists(package_id, resource_name):
151
156
return False , None
152
157
153
158
154
- def patch_package (package ) :
159
+ def patch_package (package : dict ) -> dict :
155
160
"""Patch package metadata."""
156
161
site_user = tk .get_action ("get_site_user" )({"ignore_auth" : True }, {})
157
162
context = {"ignore_auth" : True , "user" : site_user ["name" ], "auth_user_obj" : None }
158
163
patched_package = tk .get_action ("package_patch" )(context , package )
159
164
return patched_package
160
165
161
166
162
- def revise_package (package_id , match = None , filter = None , update = None , include = None ):
167
+ def revise_package (
168
+ package_id : str ,
169
+ match : dict | None = None ,
170
+ filter : list | None = None ,
171
+ update : dict | None = None ,
172
+ include : list | None = None ,
173
+ ) -> dict :
163
174
"""
164
175
Revise package metadata using the package_revise action API.
165
176
@@ -196,7 +207,9 @@ def revise_package(package_id, match=None, filter=None, update=None, include=Non
196
207
return revised_package
197
208
198
209
199
- def get_scheming_yaml (package_id , scheming_yaml_type = "dataset" ):
210
+ def get_scheming_yaml (
211
+ package_id : str , scheming_yaml_type : str = "dataset"
212
+ ) -> tuple [dict , dict ]:
200
213
"""Get the scheming yaml for a package."""
201
214
package = get_package (package_id )
202
215
if not package :
0 commit comments