dronefly-core 0.4.1__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.
Files changed (36) hide show
  1. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/PKG-INFO +2 -2
  2. dronefly_core-0.5.0.dev0/dronefly/core/clients/inat.py +72 -0
  3. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/commands/__init__.py +90 -149
  4. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/constants.py +3 -1
  5. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/generic.py +1 -22
  6. dronefly_core-0.5.0.dev0/dronefly/core/models/__init__.py +7 -0
  7. dronefly_core-0.5.0.dev0/dronefly/core/models/base_formatters.py +22 -0
  8. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/models/config.py +24 -0
  9. dronefly_core-0.5.0.dev0/dronefly/core/models/context.py +45 -0
  10. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/constants.py +5 -0
  11. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/url.py +2 -2
  12. dronefly_core-0.5.0.dev0/dronefly/core/query/base.py +162 -0
  13. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/query/query.py +117 -167
  14. dronefly_core-0.5.0.dev0/dronefly/core/query/taxon.py +405 -0
  15. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/pyproject.toml +1 -1
  16. dronefly_core-0.4.1/dronefly/core/clients/inat.py +0 -55
  17. dronefly_core-0.4.1/dronefly/core/models/__init__.py +0 -3
  18. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/LICENSE +0 -0
  19. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/README.md +0 -0
  20. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/__init__.py +0 -0
  21. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/clients/__init__.py +0 -0
  22. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/__init__.py +0 -0
  23. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/formatters/constants.py +0 -0
  24. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/__init__.py +0 -0
  25. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/menu.py +0 -0
  26. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/source.py +0 -0
  27. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/taxon.py +0 -0
  28. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/menus/taxon_list.py +0 -0
  29. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/models/controlled_terms.py +0 -0
  30. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/models/taxon_list.py +0 -0
  31. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/models/user.py +0 -0
  32. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/__init__.py +0 -0
  33. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/natural.py +0 -0
  34. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/parsers/unixlike.py +0 -0
  35. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/query/__init__.py +0 -0
  36. {dronefly_core-0.4.1 → dronefly_core-0.5.0.dev0}/dronefly/core/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: dronefly-core
3
- Version: 0.4.1
3
+ Version: 0.5.0.dev0
4
4
  Summary: Core dronefly components
5
5
  License: AGPL-3.0-or-later
6
6
  Author: Ben Armstrong
@@ -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.config import Config
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
- @define
56
- class Context:
57
- """A Dronefly command context."""
58
-
59
- author: User = User()
60
- # Optional page formatter and current page:
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
- # TODO: platform: dronefly.Platform
104
- # - e.g. discord, commandline, web
105
-
106
- inat_client: iNatClient = iNatClient()
107
- parser: NaturalParser = NaturalParser()
108
- format: Format = Format.discord_markdown
109
- config: Config = Config()
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
- return "Specify `per <rank-or-keyword>`"
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
- return "Specify `sort by obs` or `sort by name` (default)"
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
- return "Specify `order asc` or `order desc`"
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
- # Handle a useful subset of query args in a simplistic way for now
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
- return f"No life list {query_response.obs_query_description()}"
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
@@ -237,12 +179,10 @@ class Commands:
237
179
  with_indent=True,
238
180
  with_index=with_index,
239
181
  )
240
- root_taxon_id = taxon.id if taxon else None
241
182
  source = TaxonListSource(
242
183
  entries=taxon_list,
243
184
  query_response=query_response,
244
185
  formatter=formatter,
245
- root_taxon_id=root_taxon_id,
246
186
  per_page=per_page,
247
187
  per_rank=per_rank,
248
188
  sort_by=sort_by,
@@ -261,35 +201,27 @@ class Commands:
261
201
  query = self._parse(" ".join(args))
262
202
  per_rank = query.per or "child"
263
203
  if per_rank not in [*RANK_KEYWORDS, "child"]:
264
- return "Specify `per <rank>` or `per child` (default)"
204
+ raise ArgumentError("Specify `per <rank>` or `per child` (default)")
265
205
  _per_rank = per_rank
266
206
  rank_level = None
267
207
  sort_by = query.sort_by or None
268
208
  if sort_by not in [None, "obs", "name"]:
269
- return "Specify `sort by obs` or `sort by name` (default)"
209
+ raise ArgumentError("Specify `sort by obs` or `sort by name` (default)")
270
210
  order = query.order or None
271
211
  if order not in [None, "asc", "desc"]:
272
- return "Specify `order asc` or `order desc`"
212
+ raise ArgumentError("Specify `order asc` or `order desc`")
273
213
 
274
- query_args = get_base_query_args(query)
275
214
  taxon = None
276
215
  taxon_list = []
277
216
  short_description = ""
278
217
  msg = None
279
218
  with self.inat_client.set_ctx(ctx) as client:
280
- # Handle a useful subset of query args in a simplistic way for now
281
- # (i.e. no config table lookup yet) to model full query in bot
282
- if query and query.main and query.main.terms:
283
- main_query_str = " ".join(query.main.terms)
284
- taxon = client.taxa.autocomplete(q=main_query_str).one()
285
- if taxon:
286
- taxon = client.taxa.populate(taxon)
287
- query_args["taxon"] = taxon
288
- query_response = QueryResponse(**query_args)
219
+ query_response = await prepare_query(client, self.dronefly_config, query)
289
220
  taxon = query_response.taxon
290
221
  if not taxon:
291
- return f"No taxon {query_response.obs_query_description()}"
222
+ raise LookupError(f"No taxon {query_response.obs_query_description()}")
292
223
 
224
+ taxon = await client.taxa.populate(taxon)
293
225
  _taxon_list = [
294
226
  taxon,
295
227
  *[_taxon for _taxon in taxon.children if _taxon.is_active],
@@ -301,10 +233,10 @@ class Commands:
301
233
  _per_rank = RANK_EQUIVALENTS.get(per_rank) or per_rank
302
234
  rank_level = RANK_LEVELS[_per_rank]
303
235
  if rank_level >= taxon.rank_level:
304
- return self._format_markdown(
236
+ raise ArgumentError(
305
237
  "\N{WARNING SIGN} "
306
238
  f"**The rank `{per_rank}` is not lower than "
307
- "the taxon rank: `{taxon.rank}`.**"
239
+ f"the taxon rank: `{taxon.rank}`.**"
308
240
  )
309
241
  short_description = p.plural(_per_rank).capitalize()
310
242
  _children = [
@@ -318,12 +250,13 @@ class Commands:
318
250
  # remainder (direct children - those at the specified rank level)
319
251
  # don't constitute a single page of results, then show children
320
252
  # instead.
321
- _descendants = client.taxa.search(
253
+ descendants_paginator = client.taxa.search(
322
254
  taxon_id=_without_rank_ids,
323
255
  rank_level=rank_level,
324
256
  is_active=True,
325
257
  per_page=500,
326
258
  )
259
+ descendants_aiter = aiter(descendants_paginator)
327
260
  # The choice of 2500 as our limit is arbitrary:
328
261
  # - will take 5 more API calls to satisfy
329
262
  # - encompasses the largest genera (e.g. Astragalus)
@@ -331,7 +264,8 @@ class Commands:
331
264
  # excessive API demands
332
265
  # - TODO: switch to using a local DB built from full taxonomy dump
333
266
  # so we can lift this restriction
334
- if _descendants.count() > 2500:
267
+ first_descendant = await anext(descendants_aiter, None)
268
+ if descendants_paginator.count() > 2500:
335
269
  short_description = "Children"
336
270
  msg = (
337
271
  f"\N{WARNING SIGN} **Too many {p.plural(_per_rank)}. "
@@ -340,7 +274,10 @@ class Commands:
340
274
  _per_rank = "child"
341
275
  taxon_list = _taxon_list
342
276
  else:
343
- taxon_list = [taxon, *_children, *_descendants.all()]
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)
344
281
  else:
345
282
  taxon_list = _children
346
283
  # List all ranks at the same level, not just the specified rank
@@ -358,7 +295,6 @@ class Commands:
358
295
  entries=taxon_list,
359
296
  query_response=query_response,
360
297
  formatter=formatter,
361
- root_taxon_id=taxon.id,
362
298
  per_page=per_page,
363
299
  per_rank=_per_rank,
364
300
  sort_by=sort_by,
@@ -379,7 +315,7 @@ class Commands:
379
315
 
380
316
  async def next(self, ctx: Context):
381
317
  if not ctx.page_formatter:
382
- return "Type a command that has pages first"
318
+ raise CommandError("Type a command that has pages first")
383
319
  ctx.page_number += 1
384
320
  if ctx.page_number > ctx.page_formatter.last_page():
385
321
  ctx.page_number = 0
@@ -390,13 +326,13 @@ class Commands:
390
326
 
391
327
  async def page(self, ctx: Context, page_number: int = 1):
392
328
  if not ctx.page_formatter:
393
- return "Type a command that has pages first"
329
+ raise CommandError("Type a command that has pages first")
394
330
  last_page = ctx.page_formatter.last_page() + 1
395
331
  if page_number > last_page or page_number < 1:
396
332
  msg = "Specify page 1"
397
333
  if last_page > 1:
398
334
  msg += f" through {last_page}"
399
- return msg
335
+ raise ArgumentError(msg)
400
336
  ctx.page_number = page_number - 1
401
337
  ctx.selected = 0
402
338
  return await self._get_formatted_page(
@@ -405,14 +341,14 @@ class Commands:
405
341
 
406
342
  async def sel(self, ctx: Context, sel: int = 1):
407
343
  if not ctx.page_formatter:
408
- return "Type a command that has pages first"
344
+ raise CommandError("Type a command that has pages first")
409
345
  _page = await ctx.page_formatter.source.get_page(ctx.page_number)
410
346
  page_len = len(_page)
411
347
  if sel > page_len or sel < 1:
412
348
  msg = "Specify entry 1"
413
349
  if page_len > 1:
414
350
  msg += f" through {page_len}"
415
- return msg
351
+ raise ArgumentError(msg)
416
352
  ctx.selected = sel - 1
417
353
  return await self._get_formatted_page(
418
354
  ctx.page_formatter, ctx.page_number, ctx.selected
@@ -420,7 +356,7 @@ class Commands:
420
356
 
421
357
  async def prev(self, ctx: Context):
422
358
  if not ctx.page_formatter:
423
- return "Type a command that has pages first"
359
+ raise CommandError("Type a command that has pages first")
424
360
  ctx.page_number -= 1
425
361
  if ctx.page_number < 0:
426
362
  ctx.page_number = ctx.page_formatter.last_page()
@@ -436,22 +372,23 @@ class Commands:
436
372
  if formatter and getattr(formatter, "source", None):
437
373
  page = await formatter.source.get_page(ctx.page_number)
438
374
  with self.inat_client.set_ctx(ctx) as client:
439
- taxon = client.taxa.populate(page[ctx.selected])
375
+ taxon = await client.taxa.populate(page[ctx.selected])
440
376
  else:
441
- return "Select a taxon first"
377
+ raise CommandError("Select a taxon first")
442
378
  else:
443
379
  query = self._parse(" ".join(args))
444
380
  # TODO: Handle all query clauses, not just main.terms
445
381
  # TODO: Doesn't do any ranking or filtering of results
446
382
  if not query.main or not query.main.terms:
447
- return "Not a taxon"
383
+ raise ArgumentError("Not a taxon")
448
384
 
449
385
  with self.inat_client.set_ctx(ctx) as client:
450
- main_query_str = " ".join(query.main.terms)
451
- taxon = client.taxa.autocomplete(q=main_query_str).one()
452
- if not taxon:
453
- return "Nothing found"
454
- taxon = client.taxa.populate(taxon)
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)
455
392
 
456
393
  formatter = TaxonFormatter(
457
394
  taxon,
@@ -464,32 +401,31 @@ class Commands:
464
401
 
465
402
  async def obs(self, ctx: Context, *args):
466
403
  query = self._parse(" ".join(args))
467
- # TODO: Handle all query clauses, not just main.terms
468
- # TODO: Doesn't do any ranking or filtering of results
469
- if not query.main or not query.main.terms:
470
- return "Not a taxon"
471
404
 
472
- main_query_str = " ".join(query.main.terms)
473
405
  with self.inat_client.set_ctx(ctx) as client:
474
- taxon = client.taxa.autocomplete(q=main_query_str).one()
475
- if not taxon:
476
- return "No taxon found"
477
- obs = client.observations.search(
478
- user_id=ctx.author.inat_user_id,
479
- taxon_id=taxon.id,
480
- reverse=True,
481
- ).one()
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)
482
414
  if not obs:
483
- return f"No observations by you found for: {taxon.full_name}"
415
+ raise LookupError(
416
+ f"No observation found {query_response.obs_query_description()}"
417
+ )
484
418
 
485
- taxon_summary = client.observations.taxon_summary(obs.id)
419
+ taxon_summary = await client.observations.taxon_summary(obs.id)
486
420
  if obs.community_taxon_id and obs.community_taxon_id != obs.taxon.id:
487
- community_taxon = client.taxa.from_ids(obs.community_taxon_id).one()
488
- community_taxon_summary = client.observations.taxon_summary(
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(
489
425
  obs.id, community=1
490
426
  )
491
427
  else:
492
- community_taxon = taxon
428
+ community_taxon = query_response.taxon
493
429
  community_taxon_summary = taxon_summary
494
430
 
495
431
  formatter = ObservationFormatter(
@@ -507,19 +443,21 @@ class Commands:
507
443
  with self.inat_client.set_ctx(ctx) as client:
508
444
  user = None
509
445
  try:
510
- user = client.users(user_id)
446
+ user = await anext(aiter(client.users.from_ids(user_id)), None)
511
447
  except HTTPError as err:
512
448
  if err.response.status_code == 404:
513
449
  pass
514
450
  if not user:
515
- return "User not found."
451
+ raise LookupError("User not found.")
516
452
 
517
453
  return self._format_markdown(UserFormatter(user).format())
518
454
 
519
455
  async def user_add(self, ctx: Context, user_abbrev: str, user_id: str):
520
456
  if user_abbrev != "me":
521
- return "Only `user add me <user-id>` is supported at this time."
522
- user_config = self.config.user(ctx.author.id)
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)
523
461
  configured_user_id = None
524
462
  if user_config:
525
463
  configured_user_id = user_config.get("inat_user_id")
@@ -527,21 +465,23 @@ class Commands:
527
465
  with self.inat_client.set_ctx(ctx) as client:
528
466
  user = None
529
467
  try:
530
- user = client.users(user_id)
468
+ user = await anext(aiter(client.users.from_ids(user_id)), None)
531
469
  except HTTPError as err:
532
470
  if err.response.status_code == 404:
533
471
  pass
534
472
  if not user:
535
- return "User not found."
473
+ raise LookupError("User not found.")
536
474
 
537
475
  response = ""
538
476
  redefining = False
539
477
  if configured_user_id:
540
478
  if configured_user_id == user.id:
541
- return "User already added."
479
+ raise ArgumentError("User already added.")
542
480
  configured_user = None
543
481
  try:
544
- configured_user = client.users(configured_user_id)
482
+ configured_user = await anext(
483
+ aiter(client.users.from_ids(configured_user_id)), None
484
+ )
545
485
  except HTTPError as err:
546
486
  if err.response.status_code == 404:
547
487
  pass
@@ -549,6 +489,7 @@ class Commands:
549
489
  configured_user_str = UserFormatter(configured_user).format()
550
490
  else:
551
491
  configured_user_str = f"User id not found: {configured_user_id}"
492
+ raise LookupError(configured_user_str)
552
493
  redefining = True
553
494
  response += (
554
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.taxon_list import TaxonListMetadata
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 {}