dronefly-core 0.5.3__tar.gz → 0.5.4.dev1__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 (48) hide show
  1. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/PKG-INFO +2 -2
  2. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/clients/inat.py +14 -7
  3. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/cli.py +1 -1
  4. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/generic.py +51 -29
  5. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/observation_search.py +1 -2
  6. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/paginator.py +1 -1
  7. dronefly_core-0.5.4.dev1/dronefly/core/parsers/constants.py +250 -0
  8. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/url.py +1 -1
  9. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/query/query.py +19 -4
  10. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/pyproject.toml +6 -7
  11. dronefly_core-0.5.3/dronefly/core/parsers/constants.py +0 -134
  12. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/README.md +0 -0
  13. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/__init__.py +0 -0
  14. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/clients/__init__.py +0 -0
  15. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/__init__.py +0 -0
  16. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/base.py +0 -0
  17. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/constants.py +0 -0
  18. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/exceptions.py +0 -0
  19. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/constants.py +0 -0
  20. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/controllers/inat/__init__.py +0 -0
  21. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/controllers/inat/observation_controller.py +0 -0
  22. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/controllers/inat/user_controller.py +0 -0
  23. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/__init__.py +0 -0
  24. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/constants.py +0 -0
  25. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/__init__.py +0 -0
  26. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/count.py +0 -0
  27. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/counts.py +0 -0
  28. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/menu.py +0 -0
  29. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/source.py +0 -0
  30. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/taxon.py +0 -0
  31. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/taxon_list.py +0 -0
  32. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/__init__.py +0 -0
  33. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/base_formatters.py +0 -0
  34. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/config.py +0 -0
  35. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/context.py +0 -0
  36. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/controlled_terms.py +0 -0
  37. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/place_count.py +0 -0
  38. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/taxon_list.py +0 -0
  39. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/user.py +0 -0
  40. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/models/user_count.py +0 -0
  41. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/__init__.py +0 -0
  42. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/natural.py +0 -0
  43. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/unixlike.py +0 -0
  44. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/query/__init__.py +0 -0
  45. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/query/base.py +0 -0
  46. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/query/formatters.py +0 -0
  47. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/query/taxon.py +0 -0
  48. {dronefly_core-0.5.3 → dronefly_core-0.5.4.dev1}/dronefly/core/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dronefly-core
3
- Version: 0.5.3
3
+ Version: 0.5.4.dev1
4
4
  Summary: Core dronefly components
5
5
  Author: Ben Armstrong
6
6
  Author-email: Ben Armstrong <synrg@debian.org>
@@ -8,7 +8,7 @@ License-Expression: AGPL-3.0-or-later
8
8
  Classifier: Programming Language :: Python :: 3
9
9
  Classifier: Programming Language :: Python :: 3.11
10
10
  Classifier: Programming Language :: Python :: 3.12
11
- Requires-Dist: pyinaturalist>=0.21.1,<0.22
11
+ Requires-Dist: pyinaturalist>=1.0.0.dev2
12
12
  Requires-Dist: dateparser>=1.1.1,<2
13
13
  Requires-Dist: filelock>=3.13.3,<4
14
14
  Requires-Dist: inflect>=5.3.0,<6
@@ -1,4 +1,5 @@
1
1
  """Module to access iNaturalist API."""
2
+
2
3
  import asyncio
3
4
  from contextlib import contextmanager
4
5
  from functools import partial
@@ -17,13 +18,19 @@ from ..constants import INAT_DEFAULTS, RATELIMIT_FILE, RATELIMIT_LOCK_FILE, CACH
17
18
  from ..controllers.inat import ObservationController, UserController
18
19
  from ..paginator import Paginator
19
20
 
20
-
21
- DRONEFLY_SESSION = ClientSession(
22
- bucket_class=FileLockSQLiteBucket,
23
- cache_file=CACHE_FILE,
24
- ratelimit_path=RATELIMIT_FILE,
25
- lock_path=RATELIMIT_LOCK_FILE,
26
- )
21
+ if "use_lock_file" in signature(ClientSession.__init__).parameters:
22
+ # i.e. pyinaturalist 1.0.0
23
+ DRONEFLY_SESSION = ClientSession(
24
+ cache_file=CACHE_FILE, ratelimit_path=RATELIMIT_FILE, use_lock_file=True
25
+ )
26
+ else:
27
+ # i.e. pyinaturalist 0.21.1
28
+ DRONEFLY_SESSION = ClientSession(
29
+ bucket_class=FileLockSQLiteBucket,
30
+ cache_file=CACHE_FILE,
31
+ ratelimit_path=RATELIMIT_FILE,
32
+ lock_path=RATELIMIT_LOCK_FILE,
33
+ )
27
34
 
28
35
 
29
36
  def asyncify(client: pyiNatClient, method):
@@ -426,7 +426,7 @@ class CLICommands(Commands):
426
426
  raise (ArgumentError(err))
427
427
  obs_args = query_response.obs_args()
428
428
  observations = client.observations.search(**obs_args)
429
- if not observations:
429
+ if not observations.count():
430
430
  raise LookupError(
431
431
  f"No observations {query_response.obs_query_description()}"
432
432
  )
@@ -373,11 +373,12 @@ def format_taxon_names(
373
373
 
374
374
  def format_taxon_name(
375
375
  taxon: Taxon,
376
- with_term=False,
377
- hierarchy=False,
378
- with_rank=True,
379
- with_common=True,
380
- lang=None,
376
+ with_term: bool = False,
377
+ hierarchy: bool = False,
378
+ with_rank: bool = True,
379
+ with_common: bool = True,
380
+ with_italics: bool = True,
381
+ lang: str = None,
381
382
  ):
382
383
  """Format taxon name.
383
384
 
@@ -395,6 +396,9 @@ def format_taxon_name(
395
396
  If specified and hierarchy=False, includes the rank for ranks higher than species.
396
397
  with_common: bool, optional
397
398
  If specified, include common name in parentheses after scientific name.
399
+ with_italics: bool, optional
400
+ If specified (default), render the name in italics according to
401
+ conventions for the rank.
398
402
  lang: str, optional
399
403
  If specified, prefer the first name with its locale == lang instead of
400
404
  the preferred_common_name.
@@ -438,29 +442,29 @@ def format_taxon_name(
438
442
 
439
443
  common = get_common_name() if with_common else None
440
444
  name = taxon.name
441
-
442
445
  rank = taxon.rank
443
- rank_level = RANK_LEVELS[rank]
444
446
 
445
447
  # Note: We follow how iNat uses italics taxon names on the website, i.e. we:
446
448
  # - don't apply italics to the name when it is rank Genushybrid or Subgenus
447
449
  # - do italicize the name for Genus and every rank at species or below
448
450
  # - any abbreviated english keywords within italicized intraspecific ranks
449
451
  # are not italicized (spp. var. f.)
450
- if rank == "genus" or rank_level <= RANK_LEVELS["species"]:
451
- name = f"*{name}*"
452
- if rank_level > RANK_LEVELS["species"]:
453
- if hierarchy:
454
- bold = ("\n> **", "**") if rank in TAXON_PRIMARY_RANKS else ("", "")
455
- name = f"{bold[0]}{name}{bold[1]}"
456
- elif with_rank:
457
- name = f"{rank.capitalize()} {name}"
458
- else:
459
- if rank in TRINOMIAL_ABBR:
460
- tri = name.split(" ")
461
- if len(tri) == 3:
462
- # Note: name already italicized, so close/reopen italics around insertion.
463
- name = f"{tri[0]} {tri[1]}* {TRINOMIAL_ABBR[rank]} *{tri[2]}"
452
+ if rank and with_italics:
453
+ rank_level = RANK_LEVELS[rank]
454
+ if rank == "genus" or rank_level <= RANK_LEVELS["species"]:
455
+ name = f"*{name}*"
456
+ if rank_level > RANK_LEVELS["species"]:
457
+ if hierarchy:
458
+ bold = ("\n> **", "**") if rank in TAXON_PRIMARY_RANKS else ("", "")
459
+ name = f"{bold[0]}{name}{bold[1]}"
460
+ elif with_rank:
461
+ name = f"{rank.capitalize()} {name}"
462
+ else:
463
+ if rank in TRINOMIAL_ABBR:
464
+ tri = name.split(" ")
465
+ if len(tri) == 3:
466
+ # Note: name already italicized, so close/reopen italics around insertion.
467
+ name = f"{tri[0]} {tri[1]}* {TRINOMIAL_ABBR[rank]} *{tri[2]}"
464
468
  full_name = f"{name} ({common})" if common else name
465
469
  if not taxon.is_active:
466
470
  full_name += " \N{HEAVY EXCLAMATION MARK SYMBOL} Inactive Taxon"
@@ -510,14 +514,22 @@ def format_quality_grade(options: dict = {}):
510
514
  def format_obs_spp_count(
511
515
  count: Union[UserCount, PlaceCount],
512
516
  query_response: QueryResponse,
517
+ entries: list = None,
513
518
  ):
514
519
  """Format observation & species counts for a user or place."""
515
520
  obs_args = query_response.obs_args()
516
521
  if isinstance(count, UserCount):
517
522
  name = count.login
518
- obs_args[count.countable_param] = count.id
523
+ if count.id == -1:
524
+ obs_args[count.countable_param] = ",".join(
525
+ [str(entry.id) for entry in entries[0:-1]]
526
+ )
527
+ else:
528
+ obs_args[count.countable_param] = count.id
519
529
  else:
520
530
  name = count.display_name
531
+ if count.id == -1:
532
+ obs_args["place_id"] = ",".join([str(entry.id) for entry in entries])
521
533
  obs_args["place_id"] = count.id
522
534
  url = obs_url_from_v1(obs_args)
523
535
  taxon = obs_args.get("taxon", None)
@@ -1046,7 +1058,14 @@ class CountsFormatter(ListFormatter):
1046
1058
 
1047
1059
  formatted_page = [header]
1048
1060
  for count in page:
1049
- formatted_entry = format_obs_spp_count(count, query_response)
1061
+ if count.id == -1:
1062
+ formatted_entry = format_obs_spp_count(
1063
+ count, query_response, entries=page
1064
+ )
1065
+ else:
1066
+ formatted_entry = format_obs_spp_count(
1067
+ count, query_response, entries=page
1068
+ )
1050
1069
  formatted_page.append(formatted_entry)
1051
1070
  return "\n".join(formatted_page)
1052
1071
 
@@ -1250,8 +1269,8 @@ class ObservationFormatter(BaseFormatter):
1250
1269
  # - FIXME: the inconsistent return type is confusing
1251
1270
  return result
1252
1271
 
1253
- def format_title_summary(self, with_link: bool = True):
1254
- title = self.format_title(with_link=with_link)
1272
+ def format_title_summary(self, with_link: bool = True, with_italics: bool = True):
1273
+ title = self.format_title(with_link=with_link, with_italics=with_italics)
1255
1274
  summary = self.format_summary(self.taxon, self.taxon_summary)
1256
1275
  title, summary = self.format_community_id(
1257
1276
  title, summary, self.community_taxon_summary
@@ -1273,9 +1292,9 @@ class ObservationFormatter(BaseFormatter):
1273
1292
  )
1274
1293
  return taxon_str
1275
1294
 
1276
- def format_title(self, with_link: bool = True):
1295
+ def format_title(self, with_link: bool = True, with_italics: bool = True):
1277
1296
  title = ""
1278
- taxon_str = self.get_taxon_name(self.taxon)
1297
+ taxon_str = self.get_taxon_name(self.taxon, with_italics=with_italics)
1279
1298
  if with_link and self.with_link:
1280
1299
  taxon_str = format_link(
1281
1300
  taxon_str, f"{WWW_BASE_URL}/observations/{self.obs.id}"
@@ -1295,10 +1314,13 @@ class ObservationFormatter(BaseFormatter):
1295
1314
  delim = " " if self.compact else ", "
1296
1315
  return f"{delim}{ICONS[label]}" + (str(count) if count > 1 else "")
1297
1316
 
1298
- def get_taxon_name(self, taxon):
1317
+ def get_taxon_name(self, taxon, with_italics: bool = True):
1299
1318
  if taxon:
1300
1319
  taxon_str = format_taxon_name(
1301
- taxon, with_rank=not self.compact, with_common=False
1320
+ taxon,
1321
+ with_rank=not self.compact,
1322
+ with_common=False,
1323
+ with_italics=with_italics,
1302
1324
  )
1303
1325
  else:
1304
1326
  taxon_str = "Unknown"
@@ -1,7 +1,6 @@
1
1
  from typing import Any, Union
2
2
 
3
- from pyinaturalist import Observation
4
- from pyinaturalist.paginator import Paginator
3
+ from pyinaturalist import Observation, Paginator
5
4
 
6
5
  from dronefly.core.utils import obs_url_from_v1
7
6
 
@@ -1,4 +1,4 @@
1
- from pyinaturalist.paginator import (
1
+ from pyinaturalist import (
2
2
  Paginator as pyiNatPaginator,
3
3
  IDPaginator as pyiNatIDPaginator,
4
4
  )
@@ -0,0 +1,250 @@
1
+ """Constants for parsers."""
2
+
3
+ ARGPARSE_ARGS = {
4
+ "of": {"nargs": "+", "dest": "main", "default": []},
5
+ "in": {"nargs": "+", "dest": "ancestor", "default": []},
6
+ "by": {"nargs": "+", "dest": "user", "default": []},
7
+ "not-by": {"nargs": "+", "dest": "unobserved_by", "default": []},
8
+ "id-by": {"nargs": "+", "dest": "id_by", "default": []},
9
+ "except-by": {"nargs": "+", "dest": "except_by", "default": []},
10
+ "from": {"nargs": "+", "dest": "place", "default": []},
11
+ "rank": {"nargs": "+", "dest": "ranks", "default": []},
12
+ "with": {"nargs": "+", "dest": "controlled_term"},
13
+ "per": {"nargs": "+", "dest": "per", "default": []},
14
+ "sort-by": {"nargs": "+", "dest": "sort_by", "default": []},
15
+ "asc": {"dest": "order", "action": "store_const", "const": "asc"},
16
+ "desc": {"dest": "order", "action": "store_const", "const": "desc"},
17
+ "opt": {"nargs": "+", "dest": "options", "default": []},
18
+ "in-prj": {"nargs": "+", "dest": "project", "default": []},
19
+ "since": {"nargs": "+", "dest": "obs_d1", "default": []},
20
+ "until": {"nargs": "+", "dest": "obs_d2", "default": []},
21
+ "on": {"nargs": "+", "dest": "obs_on", "default": []},
22
+ "added-since": {"nargs": "+", "dest": "added_d1", "default": []},
23
+ "added-until": {"nargs": "+", "dest": "added_d2", "default": []},
24
+ "added-on": {"nargs": "+", "dest": "added_on", "default": []},
25
+ }
26
+ REMAINING_ARGS = list(ARGPARSE_ARGS)[1:]
27
+ MACROS = {
28
+ "rg": {"opt": ["quality_grade=research"]},
29
+ "nid": {"opt": ["quality_grade=needs_id"]},
30
+ "oldest": {"asc": None, "sort by": "observed"},
31
+ "newest": {"desc": None, "sort by": "observed"},
32
+ "my": {"by": "me"},
33
+ "home": {"from": "home"},
34
+ "faves": {"sort by": "votes", "opt": ["popular"]},
35
+ "spp": {"opt": ["hrank=species"]},
36
+ "species": {"opt": ["hrank=species"]},
37
+ "unseen": {"not by": "me", "from": "home"},
38
+ "most": {"per": "species", "sort by": "obs"},
39
+ "least": {"per": "species", "sort by": "obs", "asc": None},
40
+ }
41
+ # Keyword 'any' functions as a taxon name that means "no specific taxon"
42
+ # in a query expression, e.g. when an observations search is performed
43
+ # without a taxon_id parameter.
44
+ ANY_TAXA = {
45
+ "any": {
46
+ "query_macro": {},
47
+ "taxon_params": {
48
+ "name": "Any",
49
+ "preferred_common_name": "",
50
+ },
51
+ },
52
+ }
53
+ # Groups of taxa:
54
+ # - can't function in place of a single taxon, and are typically only used in
55
+ # observation searches & counts
56
+ # - must not be expanded when adjacent to other taxon keywords in the query
57
+ GROUP_TAXA = {
58
+ "unknown": {
59
+ "query_macro": {
60
+ "opt": [
61
+ "iconic_taxa=unknown",
62
+ "without_taxon_id=67333,151817,131236",
63
+ ]
64
+ },
65
+ "taxon_params": {
66
+ "name": "Unknown",
67
+ "preferred_common_name": "",
68
+ },
69
+ },
70
+ "waspsonly": {
71
+ "query_macro": {
72
+ "of": "124417",
73
+ "opt": ["without_taxon_id=47336,630955"],
74
+ },
75
+ "taxon_params": {
76
+ "name": "Apocrita without Formicidae, Anthophila",
77
+ "preferred_common_name": "Wasps without Ants and Bees",
78
+ },
79
+ },
80
+ "mothsonly": {
81
+ "query_macro": {"of": "47157", "opt": ["without_taxon_id=47224"]},
82
+ "taxon_params": {
83
+ "name": "Lepidoptera without Papilionoidea",
84
+ "preferred_common_name": "Moths",
85
+ },
86
+ },
87
+ "herps": {
88
+ "query_macro": {"opt": ["taxon_ids=20978,26036"]},
89
+ "taxon_params": {
90
+ "name": "Amphibia, Reptilia",
91
+ "preferred_common_name": "Amphibians & Reptiles aka Herps",
92
+ },
93
+ },
94
+ "lichenish": {
95
+ "query_macro": {
96
+ "opt": [
97
+ "taxon_ids=152028,54743,152030,175541,127378,117881,117869,175246",
98
+ "without_taxon_id=372831,1040687,1040689,352459",
99
+ ]
100
+ },
101
+ "taxon_params": {
102
+ "name": "Lecanoromycetes, Arthoniomycetes, etc.",
103
+ "preferred_common_name": "Lichenized Fungi",
104
+ },
105
+ },
106
+ "nonflowering": {
107
+ "query_macro": {
108
+ "of": "47126",
109
+ "opt": ["without_taxon_id=47125"],
110
+ },
111
+ "taxon_params": {
112
+ "name": "Plantae without Angiospermae",
113
+ "preferred_common_name": "Non-flowering Plants",
114
+ },
115
+ },
116
+ "nonvascular": {
117
+ "query_macro": {
118
+ "of": "47126",
119
+ "opt": ["without_taxon_id=211194"],
120
+ },
121
+ "taxon_params": {
122
+ "name": "Plantae without Tracheophyta",
123
+ "preferred_common_name": "Non-vascular Plants",
124
+ },
125
+ },
126
+ "inverts": {
127
+ "query_macro": {
128
+ "of": "1",
129
+ "opt": ["without_taxon_id=355675"],
130
+ },
131
+ "taxon_params": {
132
+ "name": "Animalia without Vertebrata",
133
+ "preferred_common_name": "Invertebrates",
134
+ },
135
+ },
136
+ "seaslugs": {
137
+ "query_macro": {
138
+ "opt": [
139
+ (
140
+ "taxon_ids=130687,775798,775804,49784,500752,47113,775801,"
141
+ "775833,775805,495793,47801,801507"
142
+ ),
143
+ ],
144
+ },
145
+ "taxon_params": {
146
+ "name": "Nudibranchia, Aplysiida, etc.",
147
+ "preferred_common_name": "Nudibranchs, Sea Hares & Other Marine Slugs",
148
+ },
149
+ },
150
+ "allfish": {
151
+ "query_macro": {
152
+ "opt": ["taxon_ids=47178,47273,797045,85497"],
153
+ },
154
+ "taxon_params": {
155
+ "name": "Actinopterygii, Agnatha, Elasmobranchii, Sarcopterygii",
156
+ "preferred_common_name": "Extant Fish",
157
+ },
158
+ },
159
+ "roachesonly": {
160
+ "query_macro": {
161
+ "of": "81769",
162
+ "opt": ["without_taxon_id=118903"],
163
+ },
164
+ "taxon_params": {
165
+ "name": "Blattodea without Termitoidae",
166
+ "preferred_common_name": "Cockroaches without termites",
167
+ },
168
+ },
169
+ }
170
+ VIRTUAL_TAXA = {
171
+ **ANY_TAXA,
172
+ **GROUP_TAXA,
173
+ }
174
+ GROUP_MACROS = {key: config["query_macro"] for key, config in GROUP_TAXA.items()}
175
+
176
+
177
+ def taxon_group_members(params):
178
+ taxon_ids = params.get("taxon_ids")
179
+ without_taxon_id = params.get("without_taxon_id")
180
+ includes = set(taxon_ids.split(",") if taxon_ids else [])
181
+ excludes = set(without_taxon_id.split(",") if without_taxon_id else [])
182
+ iconic_taxa = params.get("iconic_taxa")
183
+ if iconic_taxa:
184
+ includes.add(iconic_taxa)
185
+ return includes, excludes
186
+
187
+
188
+ def group_macro_taxon_ids(group_name, group_macro):
189
+ params = dict((option.split("=") + [""])[:2] for option in group_macro.get("opt"))
190
+ includes, excludes = taxon_group_members(params) if params else (set(), set())
191
+ of = group_macro.get("of")
192
+ if of:
193
+ includes.add(of)
194
+ return group_name, {"includes": includes, "excludes": excludes}
195
+
196
+
197
+ GROUP_TAXON_IDS = dict(group_macro_taxon_ids(k, v) for k, v in GROUP_MACROS.items())
198
+
199
+ VALID_OBS_OPTS = [
200
+ "captive",
201
+ "csi",
202
+ "day",
203
+ "endemic",
204
+ "geoprivacy",
205
+ "hrank",
206
+ "hour",
207
+ "iconic_taxa",
208
+ "id",
209
+ "identified",
210
+ "introduced",
211
+ "lrank",
212
+ "month",
213
+ "native",
214
+ "not_id",
215
+ "order",
216
+ "order_by",
217
+ "out_of_range",
218
+ "page",
219
+ "pcid",
220
+ "photos",
221
+ "place_id",
222
+ "popular",
223
+ "project_id",
224
+ "q",
225
+ "quality_grade",
226
+ "rank",
227
+ "search_on",
228
+ "sounds",
229
+ "taxon_geoprivacy",
230
+ "taxon_ids",
231
+ "threatened",
232
+ "user_id",
233
+ "verifiable",
234
+ "without_taxon_id",
235
+ "year",
236
+ ]
237
+ VALID_OBS_SORT_BY = {
238
+ "added": "created_at",
239
+ "observed": "observed_on",
240
+ "votes": "votes",
241
+ # Technically these are valid values, but we are not supporting them:
242
+ # - these fields are not typically shown in Dronefly displays
243
+ # - these sort options are of limited utility & hard to explain
244
+ # "guess": "species_guess",
245
+ # "id": "id",
246
+ }
247
+ VALID_TAXON_LIST_SORT_BY = [
248
+ "obs",
249
+ "name",
250
+ ]
@@ -45,7 +45,7 @@ STATIC_URL_PAT = (
45
45
  _OBS_LINK_PAT = r"\b(?P<url>" + WWW_URL_PAT + r"/observations/(?P<obs_id>\d+))\b"
46
46
  PAT_OBS_LINK = re.compile(_OBS_LINK_PAT, re.I)
47
47
 
48
- QUERY_PAT = r"\??(?:&?[^\n=& ]*=[^\n=& ]*)*"
48
+ QUERY_PAT = r"\?(?:&?[^\n=& ]*=[^\n=& ]*)*"
49
49
  _OBS_QUERY_PAT = r"(?P<url>" + WWW_URL_PAT + r"/observations" + QUERY_PAT + ")"
50
50
  PAT_OBS_QUERY = re.compile(_OBS_QUERY_PAT)
51
51
  _SELECTED_OBS_LINK_PAT = (
@@ -81,13 +81,26 @@ def _get_options(query_options: list):
81
81
  # Accept a limited selection of options:
82
82
  # - all of these to date apply only to observations, though others could
83
83
  # be added later
84
- # - all options and values are lowercased
85
- for (key, *val) in map(lambda opt: opt.lower().split("="), query_options):
86
- val = val[0] if val else "true"
84
+ # - all options and values are lowercased, with the exception of
85
+ # iconic_taxa which has all values capitalized except for `unknown`!
86
+ for (key, *val) in map(lambda opt: opt.split("="), query_options):
87
+ if not val:
88
+ val = "true"
89
+ elif key == "iconic_taxa" and len(val) > 1:
90
+ iconic_taxa = []
91
+ for iconic_taxon in val[1].split(","):
92
+ iconic_taxa.append(
93
+ iconic_taxon
94
+ if iconic_taxon == "unknown"
95
+ else iconic_taxon.capitalize()
96
+ )
97
+ val = ",".join(iconic_taxa)
98
+ else:
99
+ val = val[0]
87
100
  # - conservatively, only alphanumeric, comma, dash or
88
101
  # underscore characters accepted in values so far
89
102
  # - TODO: proper validation per field type
90
- if key in VALID_OBS_OPTS and re.match(r"^[a-z0-9,_-]*$", val):
103
+ if key in VALID_OBS_OPTS and re.match(r"^[A-Za-z0-9,_-]*$", val):
91
104
  options[key] = val
92
105
  return options
93
106
 
@@ -185,6 +198,8 @@ async def match_user(client, user_str):
185
198
  user_id = None
186
199
  if user_str == "me":
187
200
  user_id = client.ctx.author.inat_user_id
201
+ if not user_id:
202
+ raise LookupError("Your iNat account is not known.")
188
203
  if user_str == "any":
189
204
  return None
190
205
  if not user_id:
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dronefly-core"
3
- version = "0.5.3"
3
+ version = "0.5.4.dev1"
4
4
  description = "Core dronefly components"
5
5
  authors = [{ name = "Ben Armstrong", email = "synrg@debian.org" }]
6
6
  requires-python = ">=3.11,<3.13"
@@ -12,7 +12,7 @@ classifiers = [
12
12
  "Programming Language :: Python :: 3.12",
13
13
  ]
14
14
  dependencies = [
15
- "pyinaturalist>=0.21.1,<0.22",
15
+ "pyinaturalist>=1.0.0.dev2",
16
16
  "dateparser>=1.1.1,<2",
17
17
  "filelock>=3.13.3,<4",
18
18
  "inflect>=5.3.0,<6",
@@ -28,12 +28,11 @@ target-version = ['py311']
28
28
 
29
29
  [dependency-groups]
30
30
  dev = [
31
- "black>=24.3.0,<25",
31
+ "black>=26.1.0,<27",
32
32
  "pre-commit>=4.6.0",
33
- "pytest>=7.2.1,<8",
34
- "pytest-mock>=3.10.0,<4",
35
- "pylint>=2.10.2,<3",
36
- "pytest-asyncio>=0.20.3,<0.21",
33
+ "pytest>=9.0,<10.dev0",
34
+ "pytest-asyncio>=1.4.0,<1.5",
35
+ "pylint>=4.1.3,<5",
37
36
  "flake8-pyproject>=1.2.4,<2",
38
37
  ]
39
38
 
@@ -1,134 +0,0 @@
1
- """Constants for parsers."""
2
-
3
- ARGPARSE_ARGS = {
4
- "of": {"nargs": "+", "dest": "main", "default": []},
5
- "in": {"nargs": "+", "dest": "ancestor", "default": []},
6
- "by": {"nargs": "+", "dest": "user", "default": []},
7
- "not-by": {"nargs": "+", "dest": "unobserved_by", "default": []},
8
- "id-by": {"nargs": "+", "dest": "id_by", "default": []},
9
- "except-by": {"nargs": "+", "dest": "except_by", "default": []},
10
- "from": {"nargs": "+", "dest": "place", "default": []},
11
- "rank": {"nargs": "+", "dest": "ranks", "default": []},
12
- "with": {"nargs": "+", "dest": "controlled_term"},
13
- "per": {"nargs": "+", "dest": "per", "default": []},
14
- "sort-by": {"nargs": "+", "dest": "sort_by", "default": []},
15
- "asc": {"dest": "order", "action": "store_const", "const": "asc"},
16
- "desc": {"dest": "order", "action": "store_const", "const": "desc"},
17
- "opt": {"nargs": "+", "dest": "options", "default": []},
18
- "in-prj": {"nargs": "+", "dest": "project", "default": []},
19
- "since": {"nargs": "+", "dest": "obs_d1", "default": []},
20
- "until": {"nargs": "+", "dest": "obs_d2", "default": []},
21
- "on": {"nargs": "+", "dest": "obs_on", "default": []},
22
- "added-since": {"nargs": "+", "dest": "added_d1", "default": []},
23
- "added-until": {"nargs": "+", "dest": "added_d2", "default": []},
24
- "added-on": {"nargs": "+", "dest": "added_on", "default": []},
25
- }
26
- REMAINING_ARGS = list(ARGPARSE_ARGS)[1:]
27
- MACROS = {
28
- "rg": {"opt": ["quality_grade=research"]},
29
- "nid": {"opt": ["quality_grade=needs_id"]},
30
- "oldest": {"asc": None, "sort by": "observed"},
31
- "newest": {"desc": None, "sort by": "observed"},
32
- "my": {"by": "me"},
33
- "home": {"from": "home"},
34
- "faves": {"sort by": "votes", "opt": ["popular"]},
35
- "spp": {"opt": ["hrank=species"]},
36
- "species": {"opt": ["hrank=species"]},
37
- "unseen": {"not by": "me", "from": "home"},
38
- "most": {"per": "species", "sort by": "obs"},
39
- "least": {"per": "species", "sort by": "obs", "asc": None},
40
- }
41
- # Groups of taxa:
42
- # - can't function in place of a single taxon, and are typically only used in
43
- # observation searches & counts
44
- # - must not be expanded when adjacent to other taxon keywords in the query
45
- GROUP_MACROS = {
46
- # Because there are no iconic taxa for these three taxa, they must be specifically
47
- # excluded in order to match only actual unknowns (Bacteria, Archaea, & Viruses):
48
- "unknown": {"opt": ["iconic_taxa=unknown", "without_taxon_id=67333,151817,131236"]},
49
- "waspsonly": {"of": "apocrita", "opt": ["without_taxon_id=47336,630955"]},
50
- "mothsonly": {"of": "lepidoptera", "opt": ["without_taxon_id=47224"]},
51
- "herps": {"opt": ["taxon_ids=20978,26036"]},
52
- "lichenish": {
53
- "opt": [
54
- "taxon_ids=152028,54743,152030,175541,127378,117881,117869,175246",
55
- "without_taxon_id=372831,1040687,1040689,352459",
56
- ]
57
- },
58
- "nonflowering": {
59
- "of": "plantae",
60
- "opt": ["without_taxon_id=47125"],
61
- },
62
- "nonvascular": {
63
- "of": "plantae",
64
- "opt": ["without_taxon_id=211194"],
65
- },
66
- "inverts": {"of": "animalia", "opt": ["without_taxon_id=355675"]},
67
- "seaslugs": {
68
- "opt": [
69
- (
70
- "taxon_ids=130687,775798,775804,49784,500752,47113,775801,"
71
- "775833,775805,495793,47801,801507"
72
- ),
73
- ],
74
- },
75
- "allfish": {
76
- "opt": ["taxon_ids=47178,47273,797045,85497"],
77
- },
78
- "roachesonly": {
79
- "of": "blattodea",
80
- "opt": ["without_taxon_id=118903"],
81
- },
82
- }
83
- VALID_OBS_OPTS = [
84
- "captive",
85
- "csi",
86
- "day",
87
- "endemic",
88
- "geoprivacy",
89
- "hrank",
90
- "hour",
91
- "iconic_taxa",
92
- "id",
93
- "identified",
94
- "introduced",
95
- "lrank",
96
- "month",
97
- "native",
98
- "not_id",
99
- "order",
100
- "order_by",
101
- "out_of_range",
102
- "page",
103
- "pcid",
104
- "photos",
105
- "place_id",
106
- "popular",
107
- "project_id",
108
- "q",
109
- "quality_grade",
110
- "rank",
111
- "search_on",
112
- "sounds",
113
- "taxon_geoprivacy",
114
- "taxon_ids",
115
- "threatened",
116
- "user_id",
117
- "verifiable",
118
- "without_taxon_id",
119
- "year",
120
- ]
121
- VALID_OBS_SORT_BY = {
122
- "added": "created_at",
123
- "observed": "observed_on",
124
- "votes": "votes",
125
- # Technically these are valid values, but we are not supporting them:
126
- # - these fields are not typically shown in Dronefly displays
127
- # - these sort options are of limited utility & hard to explain
128
- # "guess": "species_guess",
129
- # "id": "id",
130
- }
131
- VALID_TAXON_LIST_SORT_BY = [
132
- "obs",
133
- "name",
134
- ]