gazu 1.0.3__py2.py3-none-any.whl → 1.1.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gazu/client.py CHANGED
@@ -1,8 +1,11 @@
1
+ from __future__ import annotations
2
+
1
3
  import sys
2
4
  import functools
3
5
  import json
4
6
  import shutil
5
7
  import os
8
+ from typing import Any, Callable, cast
6
9
 
7
10
  from .encoder import CustomJSONEncoder
8
11
 
@@ -33,15 +36,15 @@ DEBUG = os.getenv("GAZU_DEBUG", "false").lower() == "true"
33
36
  class KitsuClient(object):
34
37
  def __init__(
35
38
  self,
36
- host,
37
- ssl_verify=True,
38
- cert=None,
39
- use_refresh_token=True,
40
- callback_not_authenticated=None,
41
- tokens={"access_token": None, "refresh_token": None},
42
- access_token=None,
43
- refresh_token=None,
44
- ):
39
+ host: str,
40
+ ssl_verify: bool = True,
41
+ cert: str | None = None,
42
+ use_refresh_token: bool = True,
43
+ callback_not_authenticated: Callable | None = None,
44
+ tokens: dict = {"access_token": None, "refresh_token": None},
45
+ access_token: str | None = None,
46
+ refresh_token: str | None = None,
47
+ ) -> None:
45
48
  self.tokens = tokens
46
49
  if access_token:
47
50
  self.access_token = access_token
@@ -57,22 +60,22 @@ class KitsuClient(object):
57
60
  self.event_host = host
58
61
 
59
62
  @property
60
- def access_token(self):
63
+ def access_token(self) -> str | None:
61
64
  return self.tokens.get("access_token", None)
62
65
 
63
66
  @access_token.setter
64
- def access_token(self, token):
67
+ def access_token(self, token: str) -> None:
65
68
  self.tokens["access_token"] = token
66
69
 
67
70
  @property
68
- def refresh_token(self):
71
+ def refresh_token(self) -> str | None:
69
72
  return self.tokens.get("refresh_token", None)
70
73
 
71
74
  @refresh_token.setter
72
- def refresh_token(self, token):
75
+ def refresh_token(self, token: str) -> None:
73
76
  self.tokens["refresh_token"] = token
74
77
 
75
- def refresh_access_token(self):
78
+ def refresh_access_token(self) -> dict[str, str]:
76
79
  """
77
80
  Refresh access tokens for this client.
78
81
 
@@ -94,7 +97,7 @@ class KitsuClient(object):
94
97
 
95
98
  return tokens
96
99
 
97
- def make_auth_header(self):
100
+ def make_auth_header(self) -> dict[str, str]:
98
101
  """
99
102
  Make headers required to authenticate.
100
103
 
@@ -110,13 +113,13 @@ class KitsuClient(object):
110
113
 
111
114
 
112
115
  def create_client(
113
- host,
114
- ssl_verify=True,
115
- cert=None,
116
- use_refresh_token=False,
117
- callback_not_authenticated=None,
118
- **kwargs
119
- ):
116
+ host: str,
117
+ ssl_verify: bool = True,
118
+ cert: str | None = None,
119
+ use_refresh_token: bool = False,
120
+ callback_not_authenticated: Callable | None = None,
121
+ **kwargs: Any,
122
+ ) -> KitsuClient:
120
123
  """
121
124
  Create a client with given parameters.
122
125
 
@@ -136,11 +139,14 @@ def create_client(
136
139
  cert=cert,
137
140
  use_refresh_token=use_refresh_token,
138
141
  callback_not_authenticated=callback_not_authenticated,
139
- **kwargs
142
+ **kwargs,
140
143
  )
141
144
 
142
145
 
146
+ # For type-checking, assume that the default client has been created (we're not
147
+ # in setup mode). We do this by using typing.cast() to spoof the type.
143
148
  default_client = None
149
+ default_client = cast(KitsuClient, default_client)
144
150
  try:
145
151
  import requests
146
152
 
@@ -154,7 +160,7 @@ except Exception:
154
160
  print("Warning, running in setup mode!")
155
161
 
156
162
 
157
- def host_is_up(client=default_client):
163
+ def host_is_up(client: KitsuClient = default_client) -> bool:
158
164
  """
159
165
  Check if the host is up.
160
166
 
@@ -171,7 +177,7 @@ def host_is_up(client=default_client):
171
177
  return response.status_code == 200
172
178
 
173
179
 
174
- def host_is_valid(client=default_client):
180
+ def host_is_valid(client: KitsuClient = default_client) -> bool:
175
181
  """
176
182
  Check if the host is valid by simulating a fake login.
177
183
 
@@ -185,13 +191,15 @@ def host_is_valid(client=default_client):
185
191
  return False
186
192
  try:
187
193
  post("auth/login", {"email": ""})
194
+ return True
188
195
  except Exception as exc:
189
196
  return isinstance(exc, (NotAuthenticatedException, ParameterException))
190
197
 
191
198
 
192
- def get_host(client=default_client):
199
+ def get_host(client: KitsuClient = default_client) -> str:
193
200
  """
194
- Get client.host.
201
+ Get the API URL for the Kitsu host the given client is connected to, e.g
202
+ "http://kitsu.instance.com/api"
195
203
 
196
204
  Args:
197
205
  client (KitsuClient): The client to use for the request.
@@ -202,19 +210,24 @@ def get_host(client=default_client):
202
210
  return client.host
203
211
 
204
212
 
205
- def get_api_url_from_host(client=default_client):
213
+ def get_api_url_from_host(client: KitsuClient = default_client) -> str:
206
214
  """
207
- Get the API url from the host.
215
+ Get the base URL to the Kitsu instance the client is connected to, without
216
+ the `/api` suffix, e.g
217
+
218
+ This can be used to build URL paths to particular records in the Kitsu
219
+ web interface.
208
220
 
209
221
  Args:
210
222
  client (KitsuClient): The client to use for the request.
223
+
211
224
  Returns:
212
- Zou url, retrieved from host.
225
+ str: The base Kitsu URL the client is connected to.
213
226
  """
214
227
  return client.host[:-4]
215
228
 
216
229
 
217
- def set_host(new_host, client=default_client):
230
+ def set_host(new_host: str, client: KitsuClient = default_client) -> str:
218
231
  """
219
232
  Set the host for the client.
220
233
 
@@ -229,7 +242,7 @@ def set_host(new_host, client=default_client):
229
242
  return client.host
230
243
 
231
244
 
232
- def get_event_host(client=default_client):
245
+ def get_event_host(client: KitsuClient = default_client) -> str:
233
246
  """
234
247
  Get the host on which listening for events.
235
248
 
@@ -242,7 +255,7 @@ def get_event_host(client=default_client):
242
255
  return client.event_host or client.host
243
256
 
244
257
 
245
- def set_event_host(new_host, client=default_client):
258
+ def set_event_host(new_host: str, client: KitsuClient = default_client) -> str:
246
259
  """
247
260
  Set the host on which listening for events.
248
261
 
@@ -257,7 +270,9 @@ def set_event_host(new_host, client=default_client):
257
270
  return client.event_host
258
271
 
259
272
 
260
- def set_tokens(new_tokens, client=default_client):
273
+ def set_tokens(
274
+ new_tokens: dict[str, str], client: KitsuClient = default_client
275
+ ) -> dict[str, str]:
261
276
  """
262
277
  Store authentication token to reuse them for all requests.
263
278
 
@@ -272,7 +287,7 @@ def set_tokens(new_tokens, client=default_client):
272
287
  return client.tokens
273
288
 
274
289
 
275
- def make_auth_header(client=default_client):
290
+ def make_auth_header(client: KitsuClient = default_client) -> dict[str, str]:
276
291
  """
277
292
  Make headers required to authenticate.
278
293
 
@@ -285,7 +300,7 @@ def make_auth_header(client=default_client):
285
300
  return client.make_auth_header()
286
301
 
287
302
 
288
- def url_path_join(*items):
303
+ def url_path_join(*items: str) -> str:
289
304
  """
290
305
  Make it easier to build url path by joining every arguments with a '/'
291
306
  character.
@@ -299,7 +314,7 @@ def url_path_join(*items):
299
314
  return "/".join([item.lstrip("/").rstrip("/") for item in items])
300
315
 
301
316
 
302
- def get_full_url(path, client=default_client):
317
+ def get_full_url(path: str, client: KitsuClient = default_client) -> str:
303
318
  """
304
319
  Join host url with given path.
305
320
 
@@ -312,7 +327,7 @@ def get_full_url(path, client=default_client):
312
327
  return url_path_join(get_host(client), path)
313
328
 
314
329
 
315
- def build_path_with_params(path, params):
330
+ def build_path_with_params(path: str, params: dict) -> str:
316
331
  """
317
332
  Add params to a path using urllib encoding.
318
333
 
@@ -321,7 +336,7 @@ def build_path_with_params(path, params):
321
336
  params (dict): The parameters to add as a dict
322
337
 
323
338
  Returns:
324
- str: the builded path
339
+ str: the built path
325
340
  """
326
341
  if not params:
327
342
  return path
@@ -336,7 +351,12 @@ def build_path_with_params(path, params):
336
351
  return path
337
352
 
338
353
 
339
- def get(path, json_response=True, params=None, client=default_client):
354
+ def get(
355
+ path: str,
356
+ json_response: bool = True,
357
+ params: dict | None = None,
358
+ client: KitsuClient = default_client,
359
+ ) -> Any:
340
360
  """
341
361
  Run a get request toward given path for configured host.
342
362
 
@@ -366,13 +386,13 @@ def get(path, json_response=True, params=None, client=default_client):
366
386
  return response.text
367
387
 
368
388
 
369
- def post(path, data, client=default_client):
389
+ def post(path: str, data: Any, client: KitsuClient = default_client) -> Any:
370
390
  """
371
391
  Run a post request toward given path for configured host.
372
392
 
373
393
  Args:
374
394
  path (str): The path to query.
375
- data (dict): The data to post.
395
+ data (Any): The data to post.
376
396
  client (KitsuClient): The client to use for the request.
377
397
 
378
398
  Returns:
@@ -398,7 +418,7 @@ def post(path, data, client=default_client):
398
418
  return result
399
419
 
400
420
 
401
- def put(path, data, client=default_client):
421
+ def put(path: str, data: dict, client: KitsuClient = default_client) -> Any:
402
422
  """
403
423
  Run a put request toward given path for configured host.
404
424
 
@@ -424,7 +444,9 @@ def put(path, data, client=default_client):
424
444
  return response.json()
425
445
 
426
446
 
427
- def delete(path, params=None, client=default_client):
447
+ def delete(
448
+ path: str, params: dict | None = None, client: KitsuClient = default_client
449
+ ) -> str:
428
450
  """
429
451
  Run a delete request toward given path for configured host.
430
452
 
@@ -450,15 +472,16 @@ def delete(path, params=None, client=default_client):
450
472
 
451
473
 
452
474
  def get_message_from_response(
453
- response, default_message="No additional information"
454
- ):
475
+ response: requests.Response,
476
+ default_message: str = "No additional information",
477
+ ) -> str:
455
478
  """
456
479
  A utility function that handles Zou's inconsistent message keys.
457
480
  For a given request, checks if any error messages or regular messages were given and returns their value.
458
481
  If no messages are found, returns a default message.
459
482
 
460
483
  Args:
461
- response: requests.Request - A response to check.
484
+ response: requests.Response - A response to check.
462
485
  default_message: str - An optional default value to revert to if no message is detected.
463
486
 
464
487
  Returns:
@@ -476,18 +499,21 @@ def get_message_from_response(
476
499
  return message
477
500
 
478
501
 
479
- def check_status(request, path, client=None):
502
+ def check_status(
503
+ request: requests.Response, path: str, client: KitsuClient = None
504
+ ) -> tuple[int, bool]:
480
505
  """
481
506
  Raise an exception related to status code, if the status code does not
482
507
  match a success code. Print error message when it's relevant.
483
508
 
484
509
  Args:
485
- request (Request): The request to validate.
510
+ request (requests.Response): The request to validate.
486
511
  path (str): The path of the request.
487
512
  client (KitsuClient): The client to use for the request.
488
513
 
489
514
  Returns:
490
- int: Status code
515
+ tuple[int, bool]: The status code, and whether or not to retry the
516
+ request.
491
517
 
492
518
  Raises:
493
519
  ParameterException: when 400 response occurs
@@ -549,8 +575,12 @@ def check_status(request, path, client=None):
549
575
 
550
576
 
551
577
  def fetch_all(
552
- path, params=None, client=default_client, paginated=False, limit=None
553
- ):
578
+ path: str,
579
+ params: dict | None = None,
580
+ client: KitsuClient = default_client,
581
+ paginated: bool = False,
582
+ limit: int | None = None,
583
+ ) -> list[dict]:
554
584
  """
555
585
  Args:
556
586
  path (str): The path for which we want to retrieve all entries.
@@ -595,7 +625,9 @@ def fetch_all(
595
625
  return results
596
626
 
597
627
 
598
- def fetch_first(path, params=None, client=default_client):
628
+ def fetch_first(
629
+ path: str, params: dict | None = None, client: KitsuClient = default_client
630
+ ) -> dict | None:
599
631
  """
600
632
  Args:
601
633
  path (str): The path for which we want to retrieve the first entry.
@@ -612,7 +644,12 @@ def fetch_first(path, params=None, client=default_client):
612
644
  return None
613
645
 
614
646
 
615
- def fetch_one(model_name, id, params=None, client=default_client):
647
+ def fetch_one(
648
+ model_name: str,
649
+ id: str,
650
+ params: dict | None = None,
651
+ client: KitsuClient = default_client,
652
+ ) -> dict:
616
653
  """
617
654
  Function dedicated at targeting routes that returns a single model
618
655
  instance.
@@ -631,13 +668,15 @@ def fetch_one(model_name, id, params=None, client=default_client):
631
668
  )
632
669
 
633
670
 
634
- def create(model_name, data, client=default_client):
671
+ def create(
672
+ model_name: str, data: dict, client: KitsuClient = default_client
673
+ ) -> dict:
635
674
  """
636
675
  Create an entry for given model and data.
637
676
 
638
677
  Args:
639
678
  model_name (str): The model type involved.
640
- data (str): The data to use for creation.
679
+ data (dict): The data to use for creation.
641
680
  client (KitsuClient): The client to use for the request.
642
681
 
643
682
  Returns:
@@ -646,7 +685,12 @@ def create(model_name, data, client=default_client):
646
685
  return post(url_path_join("data", model_name), data, client=client)
647
686
 
648
687
 
649
- def update(model_name, model_id, data, client=default_client):
688
+ def update(
689
+ model_name: str,
690
+ model_id: str,
691
+ data: dict,
692
+ client: KitsuClient = default_client,
693
+ ) -> dict:
650
694
  """
651
695
  Update an entry for given model, id and data.
652
696
 
@@ -665,13 +709,13 @@ def update(model_name, model_id, data, client=default_client):
665
709
 
666
710
 
667
711
  def upload(
668
- path,
669
- file_path=None,
670
- data={},
671
- extra_files=[],
672
- files=None,
673
- client=default_client,
674
- ):
712
+ path: str,
713
+ file_path: str = None,
714
+ data: dict = {},
715
+ extra_files: list = [],
716
+ files: dict = None,
717
+ client: KitsuClient = default_client,
718
+ ) -> Any:
675
719
  """
676
720
  Upload file located at *file_path* to given url *path*.
677
721
 
@@ -684,7 +728,7 @@ def upload(
684
728
  client (KitsuClient): The client to use for the request.
685
729
 
686
730
  Returns:
687
- Response: Request response object.
731
+ Any: Response from the API.
688
732
  """
689
733
  url = get_full_url(path, client)
690
734
  if not files:
@@ -711,7 +755,7 @@ def upload(
711
755
  return result
712
756
 
713
757
 
714
- def _build_file_dict(file_path, extra_files):
758
+ def _build_file_dict(file_path: str, extra_files: list[str]) -> dict:
715
759
  """
716
760
  Build a dictionary of files to upload.
717
761
 
@@ -732,7 +776,12 @@ def _build_file_dict(file_path, extra_files):
732
776
  return files
733
777
 
734
778
 
735
- def download(path, file_path, params=None, client=default_client):
779
+ def download(
780
+ path: str,
781
+ file_path: str,
782
+ params: dict | None = None,
783
+ client: KitsuClient = default_client,
784
+ ) -> requests.Response:
736
785
  """
737
786
  Download file located at *file_path* to given url *path*.
738
787
 
@@ -757,7 +806,9 @@ def download(path, file_path, params=None, client=default_client):
757
806
  return response
758
807
 
759
808
 
760
- def get_file_data_from_url(url, full=False, client=default_client):
809
+ def get_file_data_from_url(
810
+ url: str, full: bool = False, client: KitsuClient = default_client
811
+ ) -> bytes:
761
812
  """
762
813
  Return data found at given url.
763
814
 
@@ -782,7 +833,9 @@ def get_file_data_from_url(url, full=False, client=default_client):
782
833
  return response.content
783
834
 
784
835
 
785
- def import_data(model_name, data, client=default_client):
836
+ def import_data(
837
+ model_name: str, data: dict, client: KitsuClient = default_client
838
+ ) -> dict:
786
839
  """
787
840
  Import data for given model.
788
841
 
@@ -797,7 +850,7 @@ def import_data(model_name, data, client=default_client):
797
850
  return post("/import/kitsu/%s" % model_name, data, client=client)
798
851
 
799
852
 
800
- def get_api_version(client=default_client):
853
+ def get_api_version(client: KitsuClient = default_client) -> str:
801
854
  """
802
855
  Get the current version of the API.
803
856
 
@@ -810,7 +863,7 @@ def get_api_version(client=default_client):
810
863
  return get("", client=client)["version"]
811
864
 
812
865
 
813
- def get_current_user(client=default_client):
866
+ def get_current_user(client: KitsuClient = default_client) -> dict:
814
867
  """
815
868
  Get the current user.
816
869
 
gazu/concept.py CHANGED
@@ -1,7 +1,10 @@
1
+ from __future__ import annotations
2
+
1
3
  from . import client as raw
2
4
 
3
5
  from .sorting import sort_by_name
4
6
  from .cache import cache
7
+ from .client import KitsuClient
5
8
  from .helpers import (
6
9
  normalize_model_parameter,
7
10
  normalize_list_of_models_for_links,
@@ -11,7 +14,7 @@ default = raw.default_client
11
14
 
12
15
 
13
16
  @cache
14
- def all_concepts(client=default):
17
+ def all_concepts(client: KitsuClient = default) -> list[dict]:
15
18
  """
16
19
  Returns:
17
20
  list: All concepts from database.
@@ -21,13 +24,15 @@ def all_concepts(client=default):
21
24
 
22
25
 
23
26
  @cache
24
- def all_concepts_for_project(project, client=default):
27
+ def all_concepts_for_project(
28
+ project: str | dict, client: KitsuClient = default
29
+ ) -> list[dict]:
25
30
  """
26
31
  Args:
27
32
  project (str / dict): The project dict or the project ID.
28
33
 
29
34
  Returns:
30
- list: Concepts from database or for given project.
35
+ list: Concepts from database for the given project.
31
36
  """
32
37
  project = normalize_model_parameter(project)
33
38
  concepts = raw.fetch_all(
@@ -37,7 +42,9 @@ def all_concepts_for_project(project, client=default):
37
42
 
38
43
 
39
44
  @cache
40
- def all_previews_for_concept(concept, client=default):
45
+ def all_previews_for_concept(
46
+ concept: str | dict, client: KitsuClient = default
47
+ ) -> list[dict]:
41
48
  """
42
49
  Args:
43
50
  concept (str / dict): The concept dict or the concept ID.
@@ -51,12 +58,19 @@ def all_previews_for_concept(concept, client=default):
51
58
  )
52
59
 
53
60
 
54
- def remove_concept(concept, force=False, client=default):
61
+ def remove_concept(
62
+ concept: str | dict, force: bool = False, client: KitsuClient = default
63
+ ) -> str:
55
64
  """
56
- Remove given concept from database.
65
+ Remove the given Concept from the database.
66
+
67
+ If the Concept has tasks linked to it, this will by default mark the
68
+ Concept as canceled. Deletion can be forced regardless of task links
69
+ with the `force` parameter.
57
70
 
58
71
  Args:
59
72
  concept (dict / str): Concept to remove.
73
+ force (bool): Whether to force the deletion of the concept.
60
74
  """
61
75
  concept = normalize_model_parameter(concept)
62
76
  path = "data/concepts/%s" % concept["id"]
@@ -67,7 +81,7 @@ def remove_concept(concept, force=False, client=default):
67
81
 
68
82
 
69
83
  @cache
70
- def get_concept(concept_id, client=default):
84
+ def get_concept(concept_id: str, client: KitsuClient = default) -> dict:
71
85
  """
72
86
  Args:
73
87
  concept_id (str): ID of claimed concept.
@@ -79,7 +93,9 @@ def get_concept(concept_id, client=default):
79
93
 
80
94
 
81
95
  @cache
82
- def get_concept_by_name(project, concept_name, client=default):
96
+ def get_concept_by_name(
97
+ project: str | dict, concept_name: str, client: KitsuClient = default
98
+ ) -> dict | None:
83
99
  """
84
100
  Args:
85
101
  project (str / dict): The project dict or the project ID.
@@ -97,13 +113,13 @@ def get_concept_by_name(project, concept_name, client=default):
97
113
 
98
114
 
99
115
  def new_concept(
100
- project,
101
- name,
102
- description=None,
103
- data={},
104
- entity_concept_links=[],
105
- client=default,
106
- ):
116
+ project: str | dict,
117
+ name: str,
118
+ description: str | None = None,
119
+ data: dict = {},
120
+ entity_concept_links: list[str | dict] = [],
121
+ client: KitsuClient = default,
122
+ ) -> dict:
107
123
  """
108
124
  Create a concept for given project. Allow to set metadata too.
109
125
 
@@ -111,7 +127,9 @@ def new_concept(
111
127
  project (str / dict): The project dict or the project ID.
112
128
  name (str): The name of the concept to create.
113
129
  data (dict): Free field to set metadata of any kind.
114
- entity_concept_links (list): List of entities to tag.
130
+ entity_concept_links (list): List of entities to tag, as either
131
+ ID strings or model dicts.
132
+
115
133
  Returns:
116
134
  Created concept.
117
135
  """
@@ -135,7 +153,7 @@ def new_concept(
135
153
  return concept
136
154
 
137
155
 
138
- def update_concept(concept, client=default):
156
+ def update_concept(concept: dict, client: KitsuClient = default) -> dict:
139
157
  """
140
158
  Save given concept data into the API. Metadata are fully replaced by the ones
141
159
  set on given concept.