dronefly-core 0.4.2__tar.gz → 0.5.0.dev0__tar.gz
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.
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/PKG-INFO +2 -2
- dronefly_core-0.5.0.dev0/dronefly/core/clients/inat.py +72 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/commands/__init__.py +90 -146
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/constants.py +3 -1
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/generic.py +1 -22
- dronefly_core-0.5.0.dev0/dronefly/core/models/__init__.py +7 -0
- dronefly_core-0.5.0.dev0/dronefly/core/models/base_formatters.py +22 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/models/config.py +24 -0
- dronefly_core-0.5.0.dev0/dronefly/core/models/context.py +45 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/constants.py +5 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/url.py +2 -2
- dronefly_core-0.5.0.dev0/dronefly/core/query/base.py +162 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/query/query.py +117 -167
- dronefly_core-0.5.0.dev0/dronefly/core/query/taxon.py +405 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/pyproject.toml +1 -1
- dronefly_core-0.4.2/dronefly/core/clients/inat.py +0 -55
- dronefly_core-0.4.2/dronefly/core/models/__init__.py +0 -3
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/LICENSE +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/README.md +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/clients/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/constants.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/menu.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/source.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/taxon.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/taxon_list.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/models/controlled_terms.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/models/taxon_list.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/models/user.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/natural.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/unixlike.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/query/__init__.py +0 -0
- {dronefly_core-0.4.2 → dronefly_core-0.5.0.dev0}/dronefly/core/utils/__init__.py +0 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Module to access iNaturalist API."""
|
|
2
|
+
import asyncio
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
from functools import partial
|
|
5
|
+
from inspect import signature
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
from pyinaturalist import (
|
|
9
|
+
ClientSession,
|
|
10
|
+
FileLockSQLiteBucket,
|
|
11
|
+
iNatClient as pyiNatClient,
|
|
12
|
+
)
|
|
13
|
+
from pyinaturalist.constants import RequestParams
|
|
14
|
+
|
|
15
|
+
from ..constants import INAT_DEFAULTS, RATELIMIT_FILE, RATELIMIT_LOCK_FILE, CACHE_FILE
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
DRONEFLY_SESSION = ClientSession(
|
|
19
|
+
bucket_class=FileLockSQLiteBucket,
|
|
20
|
+
cache_file=CACHE_FILE,
|
|
21
|
+
ratelimit_path=RATELIMIT_FILE,
|
|
22
|
+
lock_path=RATELIMIT_LOCK_FILE,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def asyncify(client: pyiNatClient, method):
|
|
27
|
+
async def async_wrapper(*args, **kwargs):
|
|
28
|
+
future = client.loop.run_in_executor(None, partial(method, *args, **kwargs))
|
|
29
|
+
try:
|
|
30
|
+
return await asyncio.wait_for(future, timeout=20)
|
|
31
|
+
except TimeoutError:
|
|
32
|
+
raise LookupError("iNaturalist API request timed out")
|
|
33
|
+
|
|
34
|
+
return async_wrapper
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class iNatClient(pyiNatClient):
|
|
38
|
+
"""iNat client based on pyinaturalist."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, *args, **kwargs):
|
|
41
|
+
_kwargs = {
|
|
42
|
+
"session": DRONEFLY_SESSION,
|
|
43
|
+
**kwargs,
|
|
44
|
+
}
|
|
45
|
+
super().__init__(*args, **_kwargs)
|
|
46
|
+
self.annotations.async_all = asyncify(self, self.annotations.all)
|
|
47
|
+
self.taxa.populate = asyncify(self, self.taxa.populate)
|
|
48
|
+
self.observations.taxon_summary = asyncify(
|
|
49
|
+
self, self.observations.taxon_summary
|
|
50
|
+
)
|
|
51
|
+
self.observations.life_list = asyncify(self, self.observations.life_list)
|
|
52
|
+
|
|
53
|
+
def add_defaults(
|
|
54
|
+
self,
|
|
55
|
+
request_function,
|
|
56
|
+
kwargs: Optional[RequestParams] = None,
|
|
57
|
+
auth: bool = False,
|
|
58
|
+
):
|
|
59
|
+
_kwargs = super().add_defaults(request_function, kwargs, auth)
|
|
60
|
+
|
|
61
|
+
inat_defaults = self.ctx.get_inat_defaults() if self.ctx else INAT_DEFAULTS
|
|
62
|
+
request_params = signature(request_function).parameters
|
|
63
|
+
for param in inat_defaults:
|
|
64
|
+
if param in request_params:
|
|
65
|
+
_kwargs.setdefault(param, inat_defaults[param])
|
|
66
|
+
|
|
67
|
+
return _kwargs
|
|
68
|
+
|
|
69
|
+
@contextmanager
|
|
70
|
+
def set_ctx(self, ctx):
|
|
71
|
+
self.ctx = ctx
|
|
72
|
+
yield self
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
from enum import Enum
|
|
2
3
|
import re
|
|
3
4
|
from typing import Union
|
|
4
5
|
|
|
5
|
-
from attrs import define
|
|
6
6
|
from requests import HTTPError
|
|
7
7
|
from rich.markdown import Markdown
|
|
8
8
|
|
|
9
9
|
from ..clients.inat import iNatClient
|
|
10
10
|
from ..constants import (
|
|
11
11
|
CONFIG_PATH,
|
|
12
|
-
INAT_DEFAULTS,
|
|
13
|
-
INAT_USER_DEFAULT_PARAMS,
|
|
14
12
|
RANK_EQUIVALENTS,
|
|
15
13
|
RANKS_FOR_LEVEL,
|
|
16
14
|
RANK_KEYWORDS,
|
|
@@ -18,19 +16,17 @@ from ..constants import (
|
|
|
18
16
|
)
|
|
19
17
|
|
|
20
18
|
from ..parsers import NaturalParser
|
|
19
|
+
from ..parsers.constants import VALID_OBS_SORT_BY
|
|
20
|
+
from ..query import prepare_query
|
|
21
21
|
from ..formatters.generic import (
|
|
22
|
-
BaseFormatter,
|
|
23
|
-
TaxonListFormatter,
|
|
24
|
-
ListFormatter,
|
|
25
22
|
ObservationFormatter,
|
|
23
|
+
TaxonListFormatter,
|
|
26
24
|
TaxonFormatter,
|
|
27
25
|
UserFormatter,
|
|
28
26
|
p,
|
|
29
27
|
)
|
|
30
28
|
from ..menus.taxon_list import TaxonListSource
|
|
31
|
-
from ..models
|
|
32
|
-
from ..models.user import User
|
|
33
|
-
from ..query.query import get_base_query_args, QueryResponse
|
|
29
|
+
from ..models import BaseFormatter, Config, Context, ListFormatter
|
|
34
30
|
|
|
35
31
|
|
|
36
32
|
RICH_BQ_NEWLINE_PAT = re.compile(r"^(\> .*?)\n(?=\> )", re.MULTILINE)
|
|
@@ -52,43 +48,12 @@ class Format(Enum):
|
|
|
52
48
|
rich = 2
|
|
53
49
|
|
|
54
50
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# - Provides support for next & prev commands to navigate through
|
|
62
|
-
# paged command results.
|
|
63
|
-
# - Every command providing paged results must:
|
|
64
|
-
# - Set page_formatter to the formatter for the new results.
|
|
65
|
-
# - Set page_number to the initial page number (default: 0).
|
|
66
|
-
# - Therefore, only a single command providing paged results can
|
|
67
|
-
# be active at a time.
|
|
68
|
-
page_formatter: Union[ListFormatter, BaseFormatter] = None
|
|
69
|
-
page_number: int = 0
|
|
70
|
-
per_page: int = 0
|
|
71
|
-
selected: int = 0
|
|
72
|
-
|
|
73
|
-
def get_inat_user_default(self, inat_param: str):
|
|
74
|
-
"""Return iNat API default for user param default, if any, otherwise global default."""
|
|
75
|
-
if inat_param not in INAT_USER_DEFAULT_PARAMS:
|
|
76
|
-
return None
|
|
77
|
-
default = None
|
|
78
|
-
if self.author:
|
|
79
|
-
default = getattr(self.author, inat_param, None)
|
|
80
|
-
if not default:
|
|
81
|
-
default = INAT_DEFAULTS.get(inat_param)
|
|
82
|
-
return default
|
|
83
|
-
|
|
84
|
-
def get_inat_defaults(self):
|
|
85
|
-
"""Return all iNat API defaults."""
|
|
86
|
-
defaults = {**INAT_DEFAULTS}
|
|
87
|
-
for user_param, inat_param in INAT_USER_DEFAULT_PARAMS.items():
|
|
88
|
-
default = self.get_inat_user_default(user_param)
|
|
89
|
-
if default is not None:
|
|
90
|
-
defaults[inat_param] = default
|
|
91
|
-
return defaults
|
|
51
|
+
def _check_obs_query_fields(query_response):
|
|
52
|
+
sort_by = query_response.sort_by
|
|
53
|
+
if sort_by is not None and sort_by not in VALID_OBS_SORT_BY:
|
|
54
|
+
raise ArgumentError(
|
|
55
|
+
f"Invalid `sort by`. Must be one of: `{', '.join(VALID_OBS_SORT_BY.keys())}`"
|
|
56
|
+
)
|
|
92
57
|
|
|
93
58
|
|
|
94
59
|
# TODO: everything below needs to be broken down into different layers
|
|
@@ -96,17 +61,21 @@ class Context:
|
|
|
96
61
|
# - Context
|
|
97
62
|
# - user, channel, etc.
|
|
98
63
|
# - affects which settings are passed to inat (e.g. home place for conservation status)
|
|
99
|
-
@define
|
|
100
64
|
class Commands:
|
|
101
65
|
"""A Dronefly command processor."""
|
|
102
66
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
loop: asyncio.BaseEventLoop,
|
|
70
|
+
format: Format = Format.discord_markdown,
|
|
71
|
+
):
|
|
72
|
+
self.loop = loop
|
|
73
|
+
# TODO: platform: dronefly.Platform
|
|
74
|
+
# - e.g. discord, commandline, web
|
|
75
|
+
self.format = format
|
|
76
|
+
self.inat_client = iNatClient(loop=loop)
|
|
77
|
+
self.parser = NaturalParser()
|
|
78
|
+
self.dronefly_config = Config()
|
|
110
79
|
|
|
111
80
|
def _parse(self, query_str):
|
|
112
81
|
return self.parser.parse(query_str)
|
|
@@ -187,48 +156,21 @@ class Commands:
|
|
|
187
156
|
query = self._parse(_args)
|
|
188
157
|
per_rank = query.per or "main"
|
|
189
158
|
if per_rank not in [*RANK_KEYWORDS, "leaf", "child", "main", "any"]:
|
|
190
|
-
|
|
159
|
+
raise ArgumentError("Specify `per <rank-or-keyword>`")
|
|
191
160
|
sort_by = query.sort_by or None
|
|
192
161
|
if sort_by not in [None, "obs", "name"]:
|
|
193
|
-
|
|
162
|
+
raise ArgumentError("Specify `sort by obs` or `sort by name` (default)")
|
|
194
163
|
order = query.order or None
|
|
195
164
|
if order not in [None, "asc", "desc"]:
|
|
196
|
-
|
|
165
|
+
raise ArgumentError("Specify `order asc` or `order desc`")
|
|
197
166
|
|
|
198
|
-
query_args = get_base_query_args(query)
|
|
199
167
|
with self.inat_client.set_ctx(ctx) as client:
|
|
200
|
-
|
|
201
|
-
# (i.e. no config table lookup yet) to model full query in bot
|
|
202
|
-
if query.user == "me":
|
|
203
|
-
if ctx.author.inat_user_id:
|
|
204
|
-
query_args["user"] = client.users.from_ids(
|
|
205
|
-
ctx.author.inat_user_id
|
|
206
|
-
).one()
|
|
207
|
-
else:
|
|
208
|
-
return "Your iNat user is not known"
|
|
209
|
-
elif query.user == "any":
|
|
210
|
-
# i.e. override default "by me" when no arguments are given
|
|
211
|
-
pass
|
|
212
|
-
else:
|
|
213
|
-
user = client.users.autocomplete(q=query.user).one()
|
|
214
|
-
if user:
|
|
215
|
-
query_args["user"] = user
|
|
216
|
-
if query and query.main and query.main.terms:
|
|
217
|
-
main_query_str = " ".join(query.main.terms)
|
|
218
|
-
taxon = client.taxa.autocomplete(q=main_query_str).one()
|
|
219
|
-
query_args["taxon"] = taxon
|
|
220
|
-
if query.place:
|
|
221
|
-
place = client.places.autocomplete(q=query.place).one()
|
|
222
|
-
query_args["place"] = place
|
|
223
|
-
if query.project:
|
|
224
|
-
project = client.projects.search(q=query.project).one()
|
|
225
|
-
query_args["project"] = project
|
|
226
|
-
query_response = QueryResponse(**query_args)
|
|
168
|
+
query_response = await prepare_query(client, self.dronefly_config, query)
|
|
227
169
|
obs_args = query_response.obs_args()
|
|
228
|
-
life_list = client.observations.life_list(**obs_args)
|
|
170
|
+
life_list = await client.observations.life_list(**obs_args)
|
|
229
171
|
|
|
230
172
|
if not life_list:
|
|
231
|
-
|
|
173
|
+
raise LookupError(f"No life list {query_response.obs_query_description()}")
|
|
232
174
|
|
|
233
175
|
per_page = ctx.per_page
|
|
234
176
|
with_index = self.format == Format.rich
|
|
@@ -259,35 +201,27 @@ class Commands:
|
|
|
259
201
|
query = self._parse(" ".join(args))
|
|
260
202
|
per_rank = query.per or "child"
|
|
261
203
|
if per_rank not in [*RANK_KEYWORDS, "child"]:
|
|
262
|
-
|
|
204
|
+
raise ArgumentError("Specify `per <rank>` or `per child` (default)")
|
|
263
205
|
_per_rank = per_rank
|
|
264
206
|
rank_level = None
|
|
265
207
|
sort_by = query.sort_by or None
|
|
266
208
|
if sort_by not in [None, "obs", "name"]:
|
|
267
|
-
|
|
209
|
+
raise ArgumentError("Specify `sort by obs` or `sort by name` (default)")
|
|
268
210
|
order = query.order or None
|
|
269
211
|
if order not in [None, "asc", "desc"]:
|
|
270
|
-
|
|
212
|
+
raise ArgumentError("Specify `order asc` or `order desc`")
|
|
271
213
|
|
|
272
|
-
query_args = get_base_query_args(query)
|
|
273
214
|
taxon = None
|
|
274
215
|
taxon_list = []
|
|
275
216
|
short_description = ""
|
|
276
217
|
msg = None
|
|
277
218
|
with self.inat_client.set_ctx(ctx) as client:
|
|
278
|
-
|
|
279
|
-
# (i.e. no config table lookup yet) to model full query in bot
|
|
280
|
-
if query and query.main and query.main.terms:
|
|
281
|
-
main_query_str = " ".join(query.main.terms)
|
|
282
|
-
taxon = client.taxa.autocomplete(q=main_query_str).one()
|
|
283
|
-
if taxon:
|
|
284
|
-
taxon = client.taxa.populate(taxon)
|
|
285
|
-
query_args["taxon"] = taxon
|
|
286
|
-
query_response = QueryResponse(**query_args)
|
|
219
|
+
query_response = await prepare_query(client, self.dronefly_config, query)
|
|
287
220
|
taxon = query_response.taxon
|
|
288
221
|
if not taxon:
|
|
289
|
-
|
|
222
|
+
raise LookupError(f"No taxon {query_response.obs_query_description()}")
|
|
290
223
|
|
|
224
|
+
taxon = await client.taxa.populate(taxon)
|
|
291
225
|
_taxon_list = [
|
|
292
226
|
taxon,
|
|
293
227
|
*[_taxon for _taxon in taxon.children if _taxon.is_active],
|
|
@@ -299,10 +233,10 @@ class Commands:
|
|
|
299
233
|
_per_rank = RANK_EQUIVALENTS.get(per_rank) or per_rank
|
|
300
234
|
rank_level = RANK_LEVELS[_per_rank]
|
|
301
235
|
if rank_level >= taxon.rank_level:
|
|
302
|
-
|
|
236
|
+
raise ArgumentError(
|
|
303
237
|
"\N{WARNING SIGN} "
|
|
304
238
|
f"**The rank `{per_rank}` is not lower than "
|
|
305
|
-
"the taxon rank: `{taxon.rank}`.**"
|
|
239
|
+
f"the taxon rank: `{taxon.rank}`.**"
|
|
306
240
|
)
|
|
307
241
|
short_description = p.plural(_per_rank).capitalize()
|
|
308
242
|
_children = [
|
|
@@ -316,12 +250,13 @@ class Commands:
|
|
|
316
250
|
# remainder (direct children - those at the specified rank level)
|
|
317
251
|
# don't constitute a single page of results, then show children
|
|
318
252
|
# instead.
|
|
319
|
-
|
|
253
|
+
descendants_paginator = client.taxa.search(
|
|
320
254
|
taxon_id=_without_rank_ids,
|
|
321
255
|
rank_level=rank_level,
|
|
322
256
|
is_active=True,
|
|
323
257
|
per_page=500,
|
|
324
258
|
)
|
|
259
|
+
descendants_aiter = aiter(descendants_paginator)
|
|
325
260
|
# The choice of 2500 as our limit is arbitrary:
|
|
326
261
|
# - will take 5 more API calls to satisfy
|
|
327
262
|
# - encompasses the largest genera (e.g. Astragalus)
|
|
@@ -329,7 +264,8 @@ class Commands:
|
|
|
329
264
|
# excessive API demands
|
|
330
265
|
# - TODO: switch to using a local DB built from full taxonomy dump
|
|
331
266
|
# so we can lift this restriction
|
|
332
|
-
|
|
267
|
+
first_descendant = await anext(descendants_aiter, None)
|
|
268
|
+
if descendants_paginator.count() > 2500:
|
|
333
269
|
short_description = "Children"
|
|
334
270
|
msg = (
|
|
335
271
|
f"\N{WARNING SIGN} **Too many {p.plural(_per_rank)}. "
|
|
@@ -338,7 +274,10 @@ class Commands:
|
|
|
338
274
|
_per_rank = "child"
|
|
339
275
|
taxon_list = _taxon_list
|
|
340
276
|
else:
|
|
341
|
-
|
|
277
|
+
# Now we can get the remaining descendants:
|
|
278
|
+
taxon_list = [*_children, first_descendant]
|
|
279
|
+
async for taxon in descendants_aiter:
|
|
280
|
+
taxon_list.append(taxon)
|
|
342
281
|
else:
|
|
343
282
|
taxon_list = _children
|
|
344
283
|
# List all ranks at the same level, not just the specified rank
|
|
@@ -376,7 +315,7 @@ class Commands:
|
|
|
376
315
|
|
|
377
316
|
async def next(self, ctx: Context):
|
|
378
317
|
if not ctx.page_formatter:
|
|
379
|
-
|
|
318
|
+
raise CommandError("Type a command that has pages first")
|
|
380
319
|
ctx.page_number += 1
|
|
381
320
|
if ctx.page_number > ctx.page_formatter.last_page():
|
|
382
321
|
ctx.page_number = 0
|
|
@@ -387,13 +326,13 @@ class Commands:
|
|
|
387
326
|
|
|
388
327
|
async def page(self, ctx: Context, page_number: int = 1):
|
|
389
328
|
if not ctx.page_formatter:
|
|
390
|
-
|
|
329
|
+
raise CommandError("Type a command that has pages first")
|
|
391
330
|
last_page = ctx.page_formatter.last_page() + 1
|
|
392
331
|
if page_number > last_page or page_number < 1:
|
|
393
332
|
msg = "Specify page 1"
|
|
394
333
|
if last_page > 1:
|
|
395
334
|
msg += f" through {last_page}"
|
|
396
|
-
|
|
335
|
+
raise ArgumentError(msg)
|
|
397
336
|
ctx.page_number = page_number - 1
|
|
398
337
|
ctx.selected = 0
|
|
399
338
|
return await self._get_formatted_page(
|
|
@@ -402,14 +341,14 @@ class Commands:
|
|
|
402
341
|
|
|
403
342
|
async def sel(self, ctx: Context, sel: int = 1):
|
|
404
343
|
if not ctx.page_formatter:
|
|
405
|
-
|
|
344
|
+
raise CommandError("Type a command that has pages first")
|
|
406
345
|
_page = await ctx.page_formatter.source.get_page(ctx.page_number)
|
|
407
346
|
page_len = len(_page)
|
|
408
347
|
if sel > page_len or sel < 1:
|
|
409
348
|
msg = "Specify entry 1"
|
|
410
349
|
if page_len > 1:
|
|
411
350
|
msg += f" through {page_len}"
|
|
412
|
-
|
|
351
|
+
raise ArgumentError(msg)
|
|
413
352
|
ctx.selected = sel - 1
|
|
414
353
|
return await self._get_formatted_page(
|
|
415
354
|
ctx.page_formatter, ctx.page_number, ctx.selected
|
|
@@ -417,7 +356,7 @@ class Commands:
|
|
|
417
356
|
|
|
418
357
|
async def prev(self, ctx: Context):
|
|
419
358
|
if not ctx.page_formatter:
|
|
420
|
-
|
|
359
|
+
raise CommandError("Type a command that has pages first")
|
|
421
360
|
ctx.page_number -= 1
|
|
422
361
|
if ctx.page_number < 0:
|
|
423
362
|
ctx.page_number = ctx.page_formatter.last_page()
|
|
@@ -433,22 +372,23 @@ class Commands:
|
|
|
433
372
|
if formatter and getattr(formatter, "source", None):
|
|
434
373
|
page = await formatter.source.get_page(ctx.page_number)
|
|
435
374
|
with self.inat_client.set_ctx(ctx) as client:
|
|
436
|
-
taxon = client.taxa.populate(page[ctx.selected])
|
|
375
|
+
taxon = await client.taxa.populate(page[ctx.selected])
|
|
437
376
|
else:
|
|
438
|
-
|
|
377
|
+
raise CommandError("Select a taxon first")
|
|
439
378
|
else:
|
|
440
379
|
query = self._parse(" ".join(args))
|
|
441
380
|
# TODO: Handle all query clauses, not just main.terms
|
|
442
381
|
# TODO: Doesn't do any ranking or filtering of results
|
|
443
382
|
if not query.main or not query.main.terms:
|
|
444
|
-
|
|
383
|
+
raise ArgumentError("Not a taxon")
|
|
445
384
|
|
|
446
385
|
with self.inat_client.set_ctx(ctx) as client:
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
386
|
+
query_response = await prepare_query(
|
|
387
|
+
client, self.dronefly_config, query
|
|
388
|
+
)
|
|
389
|
+
if not query_response.taxon:
|
|
390
|
+
raise LookupError("Nothing found")
|
|
391
|
+
taxon = await client.taxa.populate(query_response.taxon)
|
|
452
392
|
|
|
453
393
|
formatter = TaxonFormatter(
|
|
454
394
|
taxon,
|
|
@@ -461,32 +401,31 @@ class Commands:
|
|
|
461
401
|
|
|
462
402
|
async def obs(self, ctx: Context, *args):
|
|
463
403
|
query = self._parse(" ".join(args))
|
|
464
|
-
# TODO: Handle all query clauses, not just main.terms
|
|
465
|
-
# TODO: Doesn't do any ranking or filtering of results
|
|
466
|
-
if not query.main or not query.main.terms:
|
|
467
|
-
return "Not a taxon"
|
|
468
404
|
|
|
469
|
-
main_query_str = " ".join(query.main.terms)
|
|
470
405
|
with self.inat_client.set_ctx(ctx) as client:
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
406
|
+
query_response = await prepare_query(client, self.dronefly_config, query)
|
|
407
|
+
_check_obs_query_fields(query_response)
|
|
408
|
+
if query_response.taxon:
|
|
409
|
+
query_response.taxon = await client.taxa.populate(query_response.taxon)
|
|
410
|
+
obs_args = query_response.obs_args()
|
|
411
|
+
obs_args["reverse"] = True
|
|
412
|
+
obs_args["limit"] = 1
|
|
413
|
+
obs = await anext(aiter(client.observations.search(**obs_args)), None)
|
|
479
414
|
if not obs:
|
|
480
|
-
|
|
415
|
+
raise LookupError(
|
|
416
|
+
f"No observation found {query_response.obs_query_description()}"
|
|
417
|
+
)
|
|
481
418
|
|
|
482
|
-
taxon_summary = client.observations.taxon_summary(obs.id)
|
|
419
|
+
taxon_summary = await client.observations.taxon_summary(obs.id)
|
|
483
420
|
if obs.community_taxon_id and obs.community_taxon_id != obs.taxon.id:
|
|
484
|
-
community_taxon =
|
|
485
|
-
|
|
421
|
+
community_taxon = await anext(
|
|
422
|
+
aiter(client.taxa.from_ids(obs.community_taxon_id)), None
|
|
423
|
+
)
|
|
424
|
+
community_taxon_summary = await client.observations.taxon_summary(
|
|
486
425
|
obs.id, community=1
|
|
487
426
|
)
|
|
488
427
|
else:
|
|
489
|
-
community_taxon = taxon
|
|
428
|
+
community_taxon = query_response.taxon
|
|
490
429
|
community_taxon_summary = taxon_summary
|
|
491
430
|
|
|
492
431
|
formatter = ObservationFormatter(
|
|
@@ -504,19 +443,21 @@ class Commands:
|
|
|
504
443
|
with self.inat_client.set_ctx(ctx) as client:
|
|
505
444
|
user = None
|
|
506
445
|
try:
|
|
507
|
-
user = client.users(user_id)
|
|
446
|
+
user = await anext(aiter(client.users.from_ids(user_id)), None)
|
|
508
447
|
except HTTPError as err:
|
|
509
448
|
if err.response.status_code == 404:
|
|
510
449
|
pass
|
|
511
450
|
if not user:
|
|
512
|
-
|
|
451
|
+
raise LookupError("User not found.")
|
|
513
452
|
|
|
514
453
|
return self._format_markdown(UserFormatter(user).format())
|
|
515
454
|
|
|
516
455
|
async def user_add(self, ctx: Context, user_abbrev: str, user_id: str):
|
|
517
456
|
if user_abbrev != "me":
|
|
518
|
-
|
|
519
|
-
|
|
457
|
+
raise ArgumentError(
|
|
458
|
+
"Only `user add me <user-id>` is supported at this time."
|
|
459
|
+
)
|
|
460
|
+
user_config = self.dronefly_config.user(ctx.author.id)
|
|
520
461
|
configured_user_id = None
|
|
521
462
|
if user_config:
|
|
522
463
|
configured_user_id = user_config.get("inat_user_id")
|
|
@@ -524,21 +465,23 @@ class Commands:
|
|
|
524
465
|
with self.inat_client.set_ctx(ctx) as client:
|
|
525
466
|
user = None
|
|
526
467
|
try:
|
|
527
|
-
user = client.users(user_id)
|
|
468
|
+
user = await anext(aiter(client.users.from_ids(user_id)), None)
|
|
528
469
|
except HTTPError as err:
|
|
529
470
|
if err.response.status_code == 404:
|
|
530
471
|
pass
|
|
531
472
|
if not user:
|
|
532
|
-
|
|
473
|
+
raise LookupError("User not found.")
|
|
533
474
|
|
|
534
475
|
response = ""
|
|
535
476
|
redefining = False
|
|
536
477
|
if configured_user_id:
|
|
537
478
|
if configured_user_id == user.id:
|
|
538
|
-
|
|
479
|
+
raise ArgumentError("User already added.")
|
|
539
480
|
configured_user = None
|
|
540
481
|
try:
|
|
541
|
-
configured_user =
|
|
482
|
+
configured_user = await anext(
|
|
483
|
+
aiter(client.users.from_ids(configured_user_id)), None
|
|
484
|
+
)
|
|
542
485
|
except HTTPError as err:
|
|
543
486
|
if err.response.status_code == 404:
|
|
544
487
|
pass
|
|
@@ -546,6 +489,7 @@ class Commands:
|
|
|
546
489
|
configured_user_str = UserFormatter(configured_user).format()
|
|
547
490
|
else:
|
|
548
491
|
configured_user_str = f"User id not found: {configured_user_id}"
|
|
492
|
+
raise LookupError(configured_user_str)
|
|
549
493
|
redefining = True
|
|
550
494
|
response += (
|
|
551
495
|
f"- Already defined as another user: {configured_user_str}\n"
|
|
@@ -6,7 +6,9 @@ from pyinaturalist.constants import COMMON_RANKS, RANK_EQUIVALENTS, RANK_LEVELS
|
|
|
6
6
|
|
|
7
7
|
USER_DATA_PATH = os.path.join(user_data_dir(), "dronefly-core")
|
|
8
8
|
CONFIG_PATH = os.path.join(USER_DATA_PATH, "config.toml")
|
|
9
|
-
|
|
9
|
+
RATELIMIT_FILE = os.path.join(USER_DATA_PATH, "ratelimit.db")
|
|
10
|
+
RATELIMIT_LOCK_FILE = os.path.join(USER_DATA_PATH, "ratelimit.lock")
|
|
11
|
+
CACHE_FILE = os.path.join(USER_DATA_PATH, "api_requests.db")
|
|
10
12
|
|
|
11
13
|
# Map Dronefly User param names to iNat API param names:
|
|
12
14
|
INAT_USER_DEFAULT_PARAMS = {
|
|
@@ -32,7 +32,7 @@ from ..constants import (
|
|
|
32
32
|
TRINOMIAL_ABBR,
|
|
33
33
|
RANK_LEVELS,
|
|
34
34
|
)
|
|
35
|
-
from ..models
|
|
35
|
+
from ..models import BaseCountFormatter, BaseFormatter, ListFormatter, TaxonListMetadata
|
|
36
36
|
from ..utils import included_ranks
|
|
37
37
|
from .constants import (
|
|
38
38
|
ICONS,
|
|
@@ -486,27 +486,6 @@ def format_quality_grade(options: dict = {}):
|
|
|
486
486
|
return adjectives
|
|
487
487
|
|
|
488
488
|
|
|
489
|
-
class BaseFormatter:
|
|
490
|
-
def format():
|
|
491
|
-
raise NotImplementedError
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
class ListFormatter(BaseFormatter):
|
|
495
|
-
def format_page():
|
|
496
|
-
raise NotImplementedError
|
|
497
|
-
|
|
498
|
-
def last_page():
|
|
499
|
-
raise NotImplementedError
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
class BaseCountFormatter(BaseFormatter):
|
|
503
|
-
def count():
|
|
504
|
-
raise NotImplementedError
|
|
505
|
-
|
|
506
|
-
def description():
|
|
507
|
-
raise NotImplementedError
|
|
508
|
-
|
|
509
|
-
|
|
510
489
|
class TaxonListFormatter(ListFormatter):
|
|
511
490
|
_pages: dict[dict] = {}
|
|
512
491
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .base_formatters import * # noqa: F401,F403
|
|
2
|
+
from .config import * # noqa: F401,F403
|
|
3
|
+
from .controlled_terms import * # noqa: F401,F403
|
|
4
|
+
from .taxon_list import * # noqa: F401,F403
|
|
5
|
+
from .user import * # noqa: F401,F403
|
|
6
|
+
|
|
7
|
+
from .context import * # noqa: F401,F403
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Abstract base formatter classes
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class BaseFormatter:
|
|
5
|
+
def format():
|
|
6
|
+
raise NotImplementedError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ListFormatter(BaseFormatter):
|
|
10
|
+
def format_page():
|
|
11
|
+
raise NotImplementedError
|
|
12
|
+
|
|
13
|
+
def last_page():
|
|
14
|
+
raise NotImplementedError
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BaseCountFormatter(BaseFormatter):
|
|
18
|
+
def count():
|
|
19
|
+
raise NotImplementedError
|
|
20
|
+
|
|
21
|
+
def description():
|
|
22
|
+
raise NotImplementedError
|
|
@@ -32,3 +32,27 @@ class Config:
|
|
|
32
32
|
return self.data["users"][str(user_id)]
|
|
33
33
|
except KeyError:
|
|
34
34
|
return None
|
|
35
|
+
|
|
36
|
+
def place(self, place_abbrev: str):
|
|
37
|
+
try:
|
|
38
|
+
return self.data["places"][str(place_abbrev)]
|
|
39
|
+
except KeyError:
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
def places(self):
|
|
43
|
+
try:
|
|
44
|
+
return self.data["places"]
|
|
45
|
+
except KeyError:
|
|
46
|
+
return {}
|
|
47
|
+
|
|
48
|
+
def project(self, project_abbrev: str):
|
|
49
|
+
try:
|
|
50
|
+
return self.data["projects"][str(project_abbrev)]
|
|
51
|
+
except KeyError:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
def projects(self):
|
|
55
|
+
try:
|
|
56
|
+
return self.data["projects"]
|
|
57
|
+
except KeyError:
|
|
58
|
+
return {}
|