dronefly-core 0.5.2__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.
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/PKG-INFO +2 -2
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/clients/inat.py +14 -7
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/cli.py +25 -7
- dronefly_core-0.5.4.dev1/dronefly/core/controllers/inat/observation_controller.py +54 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/constants.py +8 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/generic.py +122 -45
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/counts.py +1 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/observation_search.py +3 -10
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/source.py +60 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/paginator.py +1 -1
- dronefly_core-0.5.4.dev1/dronefly/core/parsers/constants.py +250 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/url.py +11 -7
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/query/base.py +4 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/query/query.py +32 -13
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/pyproject.toml +6 -7
- dronefly_core-0.5.2/dronefly/core/controllers/inat/observation_controller.py +0 -28
- dronefly_core-0.5.2/dronefly/core/parsers/constants.py +0 -134
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/README.md +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/clients/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/base.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/constants.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/commands/exceptions.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/constants.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/controllers/inat/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/controllers/inat/user_controller.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/formatters/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/count.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/menu.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/taxon.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/menus/taxon_list.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/base_formatters.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/config.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/context.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/controlled_terms.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/place_count.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/taxon_list.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/user.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/models/user_count.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/natural.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/parsers/unixlike.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/query/__init__.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/query/formatters.py +0 -0
- {dronefly_core-0.5.2 → dronefly_core-0.5.4.dev1}/dronefly/core/query/taxon.py +0 -0
- {dronefly_core-0.5.2 → 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
|
+
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.
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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):
|
|
@@ -15,8 +15,9 @@ from ..query import (
|
|
|
15
15
|
match_place,
|
|
16
16
|
match_user,
|
|
17
17
|
prepare_query,
|
|
18
|
-
QueryResponse,
|
|
19
18
|
prepare_query_for_search_obs,
|
|
19
|
+
QueryResponse,
|
|
20
|
+
QueryValueError,
|
|
20
21
|
)
|
|
21
22
|
from ..query.formatters import get_query_taxon_formatter
|
|
22
23
|
from ..formatters.generic import (
|
|
@@ -62,7 +63,10 @@ class CLICommands(Commands):
|
|
|
62
63
|
raise ArgumentError("Specify `order asc` or `order desc`")
|
|
63
64
|
|
|
64
65
|
with self.inat_client.set_ctx(ctx) as client:
|
|
65
|
-
|
|
66
|
+
try:
|
|
67
|
+
query_response = await prepare_query(client, query)
|
|
68
|
+
except QueryValueError as err:
|
|
69
|
+
raise (ArgumentError(err))
|
|
66
70
|
obs_args = query_response.obs_args()
|
|
67
71
|
life_list = await client.observations.life_list(**obs_args)
|
|
68
72
|
|
|
@@ -115,7 +119,10 @@ class CLICommands(Commands):
|
|
|
115
119
|
short_description = ""
|
|
116
120
|
msg = None
|
|
117
121
|
with self.inat_client.set_ctx(ctx) as client:
|
|
118
|
-
|
|
122
|
+
try:
|
|
123
|
+
query_response = await prepare_query(client, query)
|
|
124
|
+
except QueryValueError as err:
|
|
125
|
+
raise (ArgumentError(err))
|
|
119
126
|
taxon = query_response.taxon
|
|
120
127
|
if not taxon:
|
|
121
128
|
raise LookupError(f"No taxon {query_response.obs_query_description()}")
|
|
@@ -279,7 +286,10 @@ class CLICommands(Commands):
|
|
|
279
286
|
query = self._parse(" ".join(args))
|
|
280
287
|
if not query.main or not query.main.terms:
|
|
281
288
|
raise ArgumentError("Not a taxon")
|
|
282
|
-
|
|
289
|
+
try:
|
|
290
|
+
query_response = await prepare_query(client, query)
|
|
291
|
+
except QueryValueError as err:
|
|
292
|
+
raise (ArgumentError(err))
|
|
283
293
|
if not query_response.taxon:
|
|
284
294
|
raise LookupError("Nothing found")
|
|
285
295
|
query_response.taxon = await client.taxa.populate(query_response.taxon)
|
|
@@ -365,7 +375,10 @@ class CLICommands(Commands):
|
|
|
365
375
|
query = self._parse(" ".join(args))
|
|
366
376
|
|
|
367
377
|
with self.inat_client.set_ctx(ctx) as client:
|
|
368
|
-
|
|
378
|
+
try:
|
|
379
|
+
query_response = await prepare_query(client, query)
|
|
380
|
+
except QueryValueError as err:
|
|
381
|
+
raise (ArgumentError(err))
|
|
369
382
|
_check_obs_query_fields(query_response)
|
|
370
383
|
if query_response.taxon:
|
|
371
384
|
query_response.taxon = await client.taxa.populate(query_response.taxon)
|
|
@@ -407,10 +420,13 @@ class CLICommands(Commands):
|
|
|
407
420
|
query = self._parse(_args)
|
|
408
421
|
|
|
409
422
|
with self.inat_client.set_ctx(ctx) as client:
|
|
410
|
-
|
|
423
|
+
try:
|
|
424
|
+
query_response = await prepare_query_for_search_obs(client, query)
|
|
425
|
+
except QueryValueError as err:
|
|
426
|
+
raise (ArgumentError(err))
|
|
411
427
|
obs_args = query_response.obs_args()
|
|
412
428
|
observations = client.observations.search(**obs_args)
|
|
413
|
-
if not observations:
|
|
429
|
+
if not observations.count():
|
|
414
430
|
raise LookupError(
|
|
415
431
|
f"No observations {query_response.obs_query_description()}"
|
|
416
432
|
)
|
|
@@ -419,6 +435,8 @@ class CLICommands(Commands):
|
|
|
419
435
|
with_index = self.format == Format.rich
|
|
420
436
|
formatter = ObservationSearchFormatter(
|
|
421
437
|
with_index=with_index,
|
|
438
|
+
with_links=True,
|
|
439
|
+
with_summaries=True,
|
|
422
440
|
)
|
|
423
441
|
source = ObservationSearchSource(
|
|
424
442
|
iterator=observations,
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
|
|
3
|
+
from pyinaturalist.constants import V1_OBS_ORDER_BY_PROPERTIES
|
|
4
|
+
from pyinaturalist.controllers import (
|
|
5
|
+
ObservationController as pyiNatObservationController,
|
|
6
|
+
)
|
|
7
|
+
from pyinaturalist.docs import copy_doc_signature
|
|
8
|
+
from pyinaturalist.docs import templates as docs
|
|
9
|
+
from pyinaturalist.models import Annotation, BaseModel, Observation
|
|
10
|
+
from pyinaturalist.request_params import validate_multiple_choice_param
|
|
11
|
+
from pyinaturalist.v1 import get_observations
|
|
12
|
+
|
|
13
|
+
from ...paginator import Paginator
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ObservationController(pyiNatObservationController):
|
|
17
|
+
@copy_doc_signature(*docs._get_observations, docs._only_id)
|
|
18
|
+
def search(self, **params) -> Paginator[Observation]:
|
|
19
|
+
params = validate_multiple_choice_param(
|
|
20
|
+
params, "order_by", V1_OBS_ORDER_BY_PROPERTIES
|
|
21
|
+
)
|
|
22
|
+
params = self.client.add_defaults(get_observations, params)
|
|
23
|
+
|
|
24
|
+
return ObservationPaginator(
|
|
25
|
+
loop=self.client.loop,
|
|
26
|
+
annotation_callback=self.client.annotations.lookup,
|
|
27
|
+
**params,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ObservationPaginator(Paginator):
|
|
32
|
+
"""Paginate through observation results
|
|
33
|
+
|
|
34
|
+
This also optionally fills in missing annotation information for each observation, using results
|
|
35
|
+
from ``GET /controlled_terms``.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
request_function: Callable = get_observations,
|
|
41
|
+
model: BaseModel = Observation,
|
|
42
|
+
*args,
|
|
43
|
+
annotation_callback: Callable[[list[Annotation]], list[Annotation]],
|
|
44
|
+
**kwargs,
|
|
45
|
+
):
|
|
46
|
+
super().__init__(request_function, model, *args, **kwargs)
|
|
47
|
+
self.annotation_callback = annotation_callback
|
|
48
|
+
|
|
49
|
+
def next_page(self) -> list[Observation]:
|
|
50
|
+
observations = super().next_page()
|
|
51
|
+
# Use cached controlled_terms lookup to fill in missing annotation details
|
|
52
|
+
for obs in observations:
|
|
53
|
+
obs.annotations = self.annotation_callback(obs.annotations)
|
|
54
|
+
return observations
|
|
@@ -13,3 +13,11 @@ ICONS = {
|
|
|
13
13
|
"sound": "\N{SPEAKER WITH ONE SOUND WAVE}",
|
|
14
14
|
"ident": "\N{LABEL}",
|
|
15
15
|
}
|
|
16
|
+
# For use in cases where an entity is normally
|
|
17
|
+
# expected to have a value but may not, e.g.
|
|
18
|
+
#
|
|
19
|
+
# ICONS.get(obs.quality_grade, UNKNOWN_ICON)
|
|
20
|
+
# - will alaways return an icon representing the quality grade, even if obs is
|
|
21
|
+
# only a partial (as when created from test data or a data set missing
|
|
22
|
+
# fields normally present in observations returned from the API)
|
|
23
|
+
UNKNOWN_ICON = "\N{REPLACEMENT CHARACTER}"
|
|
@@ -47,6 +47,7 @@ from ..models import (
|
|
|
47
47
|
from ..utils import included_ranks
|
|
48
48
|
from .constants import (
|
|
49
49
|
ICONS,
|
|
50
|
+
UNKNOWN_ICON,
|
|
50
51
|
WWW_BASE_URL,
|
|
51
52
|
)
|
|
52
53
|
from dronefly.core.utils import lifelists_url_from_query_response, obs_url_from_v1
|
|
@@ -372,11 +373,12 @@ def format_taxon_names(
|
|
|
372
373
|
|
|
373
374
|
def format_taxon_name(
|
|
374
375
|
taxon: Taxon,
|
|
375
|
-
with_term=False,
|
|
376
|
-
hierarchy=False,
|
|
377
|
-
with_rank=True,
|
|
378
|
-
with_common=True,
|
|
379
|
-
|
|
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,
|
|
380
382
|
):
|
|
381
383
|
"""Format taxon name.
|
|
382
384
|
|
|
@@ -394,6 +396,9 @@ def format_taxon_name(
|
|
|
394
396
|
If specified and hierarchy=False, includes the rank for ranks higher than species.
|
|
395
397
|
with_common: bool, optional
|
|
396
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.
|
|
397
402
|
lang: str, optional
|
|
398
403
|
If specified, prefer the first name with its locale == lang instead of
|
|
399
404
|
the preferred_common_name.
|
|
@@ -437,29 +442,29 @@ def format_taxon_name(
|
|
|
437
442
|
|
|
438
443
|
common = get_common_name() if with_common else None
|
|
439
444
|
name = taxon.name
|
|
440
|
-
|
|
441
445
|
rank = taxon.rank
|
|
442
|
-
rank_level = RANK_LEVELS[rank]
|
|
443
446
|
|
|
444
447
|
# Note: We follow how iNat uses italics taxon names on the website, i.e. we:
|
|
445
448
|
# - don't apply italics to the name when it is rank Genushybrid or Subgenus
|
|
446
449
|
# - do italicize the name for Genus and every rank at species or below
|
|
447
450
|
# - any abbreviated english keywords within italicized intraspecific ranks
|
|
448
451
|
# are not italicized (spp. var. f.)
|
|
449
|
-
if rank
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
if
|
|
461
|
-
|
|
462
|
-
|
|
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]}"
|
|
463
468
|
full_name = f"{name} ({common})" if common else name
|
|
464
469
|
if not taxon.is_active:
|
|
465
470
|
full_name += " \N{HEAVY EXCLAMATION MARK SYMBOL} Inactive Taxon"
|
|
@@ -509,14 +514,22 @@ def format_quality_grade(options: dict = {}):
|
|
|
509
514
|
def format_obs_spp_count(
|
|
510
515
|
count: Union[UserCount, PlaceCount],
|
|
511
516
|
query_response: QueryResponse,
|
|
517
|
+
entries: list = None,
|
|
512
518
|
):
|
|
513
519
|
"""Format observation & species counts for a user or place."""
|
|
514
520
|
obs_args = query_response.obs_args()
|
|
515
521
|
if isinstance(count, UserCount):
|
|
516
522
|
name = count.login
|
|
517
|
-
|
|
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
|
|
518
529
|
else:
|
|
519
530
|
name = count.display_name
|
|
531
|
+
if count.id == -1:
|
|
532
|
+
obs_args["place_id"] = ",".join([str(entry.id) for entry in entries])
|
|
520
533
|
obs_args["place_id"] = count.id
|
|
521
534
|
url = obs_url_from_v1(obs_args)
|
|
522
535
|
taxon = obs_args.get("taxon", None)
|
|
@@ -557,6 +570,8 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
557
570
|
self,
|
|
558
571
|
with_index: bool = False,
|
|
559
572
|
with_url: bool = True,
|
|
573
|
+
with_links: bool = True,
|
|
574
|
+
with_summaries: bool = False,
|
|
560
575
|
short_description: str = "Search: Observations",
|
|
561
576
|
):
|
|
562
577
|
"""
|
|
@@ -566,11 +581,21 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
566
581
|
When True, output index per observation in page.
|
|
567
582
|
with_url: bool, optional
|
|
568
583
|
When True, link the title to the observations matching the query.
|
|
584
|
+
with_links: bool, optional
|
|
585
|
+
When True, link each observation taxon name to the observation of
|
|
586
|
+
the taxon.
|
|
587
|
+
with_summaries: bool, optional
|
|
588
|
+
When False, only a brief title line per observation is returned,
|
|
589
|
+
containing the date of the observation and its taxon.
|
|
590
|
+
When True, a second line containing a compact summary of observation
|
|
591
|
+
details is also returned.
|
|
569
592
|
short_description: str, optional [default: `Search: Observations`]
|
|
570
593
|
Short description of observation search that appears in the title.
|
|
571
594
|
"""
|
|
572
595
|
self.with_index = with_index
|
|
573
596
|
self.with_url = with_url
|
|
597
|
+
self.with_links = with_links
|
|
598
|
+
self.with_summaries = with_summaries
|
|
574
599
|
self.short_description = short_description
|
|
575
600
|
|
|
576
601
|
def format(
|
|
@@ -611,9 +636,16 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
611
636
|
def format_page_of_obs(page: list[Observation]):
|
|
612
637
|
formatted_obs = []
|
|
613
638
|
for obs in page:
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
639
|
+
with_user = not self.source.query_response.user
|
|
640
|
+
params = {
|
|
641
|
+
"compact": True,
|
|
642
|
+
"with_link": self.with_links,
|
|
643
|
+
"with_description": False,
|
|
644
|
+
"with_user": with_user,
|
|
645
|
+
}
|
|
646
|
+
format_params = {"join_title": self.with_summaries}
|
|
647
|
+
obs_formatter = ObservationFormatter(obs, **params)
|
|
648
|
+
description = obs_formatter.format(**format_params)
|
|
617
649
|
if obs.observed_on:
|
|
618
650
|
date = format_datetime(obs.observed_on, compact=True)
|
|
619
651
|
else:
|
|
@@ -621,7 +653,7 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
621
653
|
formatted_obs.append(
|
|
622
654
|
{
|
|
623
655
|
"date": date,
|
|
624
|
-
"
|
|
656
|
+
"description": description,
|
|
625
657
|
}
|
|
626
658
|
)
|
|
627
659
|
return formatted_obs
|
|
@@ -641,6 +673,8 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
641
673
|
|
|
642
674
|
def assemble_page(content: dict, selected: int = 0):
|
|
643
675
|
"""Assemble page content into a formatted page."""
|
|
676
|
+
indent_width = 7 if self.with_index else 1
|
|
677
|
+
indent = "\n" + ("\N{EN SPACE}" * indent_width)
|
|
644
678
|
sections = []
|
|
645
679
|
if content["header"]:
|
|
646
680
|
sections.append(content["header"])
|
|
@@ -648,19 +682,41 @@ class ObservationSearchFormatter(ListFormatter):
|
|
|
648
682
|
sections.append(content["entries_header"])
|
|
649
683
|
if content["entries"]:
|
|
650
684
|
entries = []
|
|
685
|
+
if not self.with_summaries:
|
|
686
|
+
date_len = max(len(entry["date"]) for entry in content["entries"])
|
|
651
687
|
for index, entry in enumerate(content["entries"]):
|
|
652
688
|
_i = f"**`{str(index + 1).zfill(2)}) `**" if self.with_index else ""
|
|
653
689
|
if selected == index:
|
|
654
|
-
_s = "
|
|
690
|
+
_s = "\N{BLACK RIGHT-POINTING SMALL TRIANGLE}"
|
|
655
691
|
_n = "**__"
|
|
656
692
|
_e = "__**"
|
|
657
693
|
else:
|
|
658
|
-
_s = "\N{EN SPACE}"
|
|
694
|
+
_s = "\N{ZERO WIDTH SPACE}\N{EN SPACE}"
|
|
659
695
|
_n = ""
|
|
660
696
|
_e = ""
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
697
|
+
# FIXME: Come up with a better solution for this ugly hack!
|
|
698
|
+
# - Surround the selected pointer with blanks.
|
|
699
|
+
# - Prevents rich markdown from misreading word boundaries, breaking
|
|
700
|
+
# the _n and _e markup (ends up showing '**' literally)
|
|
701
|
+
# - Has the undesirable side effect of contributing two extra spaces
|
|
702
|
+
# to the output that we didn't want.
|
|
703
|
+
# - This is a rich-markdown-specific hack. Discord doesn't have this
|
|
704
|
+
# issue. Strictly speaking, self.with_index is the wrong option
|
|
705
|
+
# to enable it, but currently only the CLI uses that option, and
|
|
706
|
+
# this keeps the hack self-contained.
|
|
707
|
+
if self.with_index:
|
|
708
|
+
_s = f" {_s} "
|
|
709
|
+
if self.with_summaries:
|
|
710
|
+
description, summary = entry["description"].split("\n")
|
|
711
|
+
entries.append(
|
|
712
|
+
f"{_i}{_s}{_n}{description}{_e}{indent}{summary}"
|
|
713
|
+
)
|
|
714
|
+
else:
|
|
715
|
+
description = entry["description"]
|
|
716
|
+
entries.append(
|
|
717
|
+
f"{_i}`{entry['date'].rjust(date_len)}`"
|
|
718
|
+
f"{_s}{_n}{description}{_e}"
|
|
719
|
+
)
|
|
664
720
|
sections.append("\n".join(entries))
|
|
665
721
|
if content["footer"]:
|
|
666
722
|
sections.append(content["footer"])
|
|
@@ -898,13 +954,16 @@ class TaxonListFormatter(ListFormatter):
|
|
|
898
954
|
for index, entry in enumerate(content["entries"]):
|
|
899
955
|
_i = f"**`{str(index + 1).zfill(2)}) `**" if self.with_index else ""
|
|
900
956
|
if selected == index:
|
|
901
|
-
_s = "
|
|
957
|
+
_s = "\N{BLACK RIGHT-POINTING SMALL TRIANGLE}"
|
|
902
958
|
_n = "**__"
|
|
903
959
|
_e = "__**"
|
|
904
960
|
else:
|
|
905
961
|
_s = "\N{EN SPACE}"
|
|
906
962
|
_n = ""
|
|
907
963
|
_e = ""
|
|
964
|
+
# FIXME: Eliminate ugly hack (see comment in ObservationSearchFormatter)
|
|
965
|
+
if self.with_index:
|
|
966
|
+
_s = f" {_s} "
|
|
908
967
|
entries.append(
|
|
909
968
|
f"{_i}`{entry['count']}{entry['direct']}`"
|
|
910
969
|
f"{_s}{entry['indent']}{_n}{entry['name']}{_e}"
|
|
@@ -999,7 +1058,14 @@ class CountsFormatter(ListFormatter):
|
|
|
999
1058
|
|
|
1000
1059
|
formatted_page = [header]
|
|
1001
1060
|
for count in page:
|
|
1002
|
-
|
|
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
|
+
)
|
|
1003
1069
|
formatted_page.append(formatted_entry)
|
|
1004
1070
|
return "\n".join(formatted_page)
|
|
1005
1071
|
|
|
@@ -1195,17 +1261,23 @@ class ObservationFormatter(BaseFormatter):
|
|
|
1195
1261
|
self.community_taxon_summary = community_taxon_summary
|
|
1196
1262
|
|
|
1197
1263
|
def format(self, join_title: bool = True):
|
|
1198
|
-
|
|
1264
|
+
result = self.format_title_summary(with_link=join_title)
|
|
1265
|
+
if join_title:
|
|
1266
|
+
result = ("" if self.compact else "\n").join(result)
|
|
1267
|
+
# returns the tuple of (title, summary) unless join_title
|
|
1268
|
+
# is used, in which case a whole string is returned
|
|
1269
|
+
# - FIXME: the inconsistent return type is confusing
|
|
1270
|
+
return result
|
|
1271
|
+
|
|
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)
|
|
1199
1274
|
summary = self.format_summary(self.taxon, self.taxon_summary)
|
|
1200
1275
|
title, summary = self.format_community_id(
|
|
1201
1276
|
title, summary, self.community_taxon_summary
|
|
1202
1277
|
)
|
|
1203
1278
|
if not self.compact:
|
|
1204
1279
|
title += self.format_media_counts()
|
|
1205
|
-
|
|
1206
|
-
if join_title:
|
|
1207
|
-
result = ("" if self.compact else "\n").join(result)
|
|
1208
|
-
return result
|
|
1280
|
+
return (title, summary)
|
|
1209
1281
|
|
|
1210
1282
|
def format_taxon_link(self, taxon: Taxon):
|
|
1211
1283
|
taxon_str = self.get_taxon_name(taxon)
|
|
@@ -1220,16 +1292,18 @@ class ObservationFormatter(BaseFormatter):
|
|
|
1220
1292
|
)
|
|
1221
1293
|
return taxon_str
|
|
1222
1294
|
|
|
1223
|
-
def format_title(self, with_link: bool = True):
|
|
1295
|
+
def format_title(self, with_link: bool = True, with_italics: bool = True):
|
|
1224
1296
|
title = ""
|
|
1225
|
-
taxon_str = self.get_taxon_name(self.taxon)
|
|
1297
|
+
taxon_str = self.get_taxon_name(self.taxon, with_italics=with_italics)
|
|
1226
1298
|
if with_link and self.with_link:
|
|
1227
1299
|
taxon_str = format_link(
|
|
1228
1300
|
taxon_str, f"{WWW_BASE_URL}/observations/{self.obs.id}"
|
|
1229
1301
|
)
|
|
1230
1302
|
title += taxon_str
|
|
1231
1303
|
if not self.compact:
|
|
1232
|
-
title += f" by {self.obs.user.login} " + ICONS
|
|
1304
|
+
title += f" by {self.obs.user.login} " + ICONS.get(
|
|
1305
|
+
self.obs.quality_grade, UNKNOWN_ICON
|
|
1306
|
+
)
|
|
1233
1307
|
if self.obs.faves:
|
|
1234
1308
|
title += self.format_count("fave", len(self.obs.faves))
|
|
1235
1309
|
if self.obs.comments:
|
|
@@ -1240,10 +1314,13 @@ class ObservationFormatter(BaseFormatter):
|
|
|
1240
1314
|
delim = " " if self.compact else ", "
|
|
1241
1315
|
return f"{delim}{ICONS[label]}" + (str(count) if count > 1 else "")
|
|
1242
1316
|
|
|
1243
|
-
def get_taxon_name(self, taxon):
|
|
1317
|
+
def get_taxon_name(self, taxon, with_italics: bool = True):
|
|
1244
1318
|
if taxon:
|
|
1245
1319
|
taxon_str = format_taxon_name(
|
|
1246
|
-
taxon,
|
|
1320
|
+
taxon,
|
|
1321
|
+
with_rank=not self.compact,
|
|
1322
|
+
with_common=False,
|
|
1323
|
+
with_italics=with_italics,
|
|
1247
1324
|
)
|
|
1248
1325
|
else:
|
|
1249
1326
|
taxon_str = "Unknown"
|
|
@@ -1289,8 +1366,8 @@ class ObservationFormatter(BaseFormatter):
|
|
|
1289
1366
|
line = " ".join((item for item in (login, obs_on, obs_at) if item))
|
|
1290
1367
|
if len(line) > 32:
|
|
1291
1368
|
line = line[0:31] + "…"
|
|
1292
|
-
summary += "`{0: <32}
|
|
1293
|
-
summary += ICONS
|
|
1369
|
+
summary += "`{0: <32}`\N{EN SPACE}".format(line)
|
|
1370
|
+
summary += ICONS.get(obs.quality_grade, UNKNOWN_ICON)
|
|
1294
1371
|
if obs.faves:
|
|
1295
1372
|
summary += self.format_count("fave", len(obs.faves))
|
|
1296
1373
|
if obs.comments:
|
|
@@ -2,6 +2,8 @@ from typing import Any, Union
|
|
|
2
2
|
|
|
3
3
|
from pyinaturalist import Observation, Paginator
|
|
4
4
|
|
|
5
|
+
from dronefly.core.utils import obs_url_from_v1
|
|
6
|
+
|
|
5
7
|
from .source import AsyncIteratorPageSource
|
|
6
8
|
from .menu import BaseSearchMenu
|
|
7
9
|
from ..formatters import ObservationSearchFormatter
|
|
@@ -53,6 +55,7 @@ class ObservationSearchSource(AsyncIteratorPageSource):
|
|
|
53
55
|
"""
|
|
54
56
|
self._observation_search_formatter = formatter
|
|
55
57
|
self.query_response = query_response
|
|
58
|
+
self._url = obs_url_from_v1(query_response.obs_args())
|
|
56
59
|
self.sort_by = sort_by
|
|
57
60
|
self.order = order
|
|
58
61
|
self._iterator = iterator
|
|
@@ -66,16 +69,6 @@ class ObservationSearchSource(AsyncIteratorPageSource):
|
|
|
66
69
|
def formatter(self) -> ObservationSearchFormatter:
|
|
67
70
|
return self._observation_search_formatter
|
|
68
71
|
|
|
69
|
-
def get_max_pages(self):
|
|
70
|
-
count = self._iterator.count()
|
|
71
|
-
if self.per_page:
|
|
72
|
-
pages, left_over = divmod(count, self.per_page)
|
|
73
|
-
if left_over:
|
|
74
|
-
pages += 1
|
|
75
|
-
else:
|
|
76
|
-
pages = 1
|
|
77
|
-
return pages
|
|
78
|
-
|
|
79
72
|
def format_page(
|
|
80
73
|
self,
|
|
81
74
|
page: Union[Observation, list[Observation]],
|
|
@@ -273,6 +273,10 @@ def _aiter(obj, *, _isasync=inspect.iscoroutinefunction):
|
|
|
273
273
|
return async_iter
|
|
274
274
|
|
|
275
275
|
|
|
276
|
+
def iscountable(iterator):
|
|
277
|
+
return hasattr(iterator, "count")
|
|
278
|
+
|
|
279
|
+
|
|
276
280
|
class AsyncIteratorPageSource(PageSource):
|
|
277
281
|
"""A data source for data backed by an asynchronous iterator.
|
|
278
282
|
|
|
@@ -289,10 +293,23 @@ class AsyncIteratorPageSource(PageSource):
|
|
|
289
293
|
|
|
290
294
|
def __init__(self, iterator, *, per_page):
|
|
291
295
|
self.iterator = _aiter(iterator)
|
|
296
|
+
self._count = None
|
|
297
|
+
if iscountable(iterator):
|
|
298
|
+
self.total = iterator.count()
|
|
299
|
+
else:
|
|
300
|
+
self.total = None
|
|
292
301
|
self.per_page = per_page
|
|
293
302
|
self._exhausted = False
|
|
294
303
|
self._cache = []
|
|
295
304
|
|
|
305
|
+
@property
|
|
306
|
+
def count(self):
|
|
307
|
+
return self._count if self._count is not None else self.total
|
|
308
|
+
|
|
309
|
+
@count.setter
|
|
310
|
+
def count(self, value):
|
|
311
|
+
self._count = value
|
|
312
|
+
|
|
296
313
|
async def _iterate(self, n):
|
|
297
314
|
it = self.iterator
|
|
298
315
|
cache = self._cache
|
|
@@ -313,6 +330,25 @@ class AsyncIteratorPageSource(PageSource):
|
|
|
313
330
|
""":class:`bool`: Whether pagination is required."""
|
|
314
331
|
return len(self._cache) > self.per_page
|
|
315
332
|
|
|
333
|
+
def get_max_pages(self):
|
|
334
|
+
"""Maximum number of pages.
|
|
335
|
+
|
|
336
|
+
Returns
|
|
337
|
+
--------
|
|
338
|
+
Optional[:class:`int`]
|
|
339
|
+
The maximum number of pages required to properly
|
|
340
|
+
paginate the elements.
|
|
341
|
+
"""
|
|
342
|
+
pages = None
|
|
343
|
+
if self.count is not None:
|
|
344
|
+
if self.per_page:
|
|
345
|
+
pages, left_over = divmod(self.count, self.per_page)
|
|
346
|
+
if left_over:
|
|
347
|
+
pages += 1
|
|
348
|
+
else:
|
|
349
|
+
pages = 1
|
|
350
|
+
return pages
|
|
351
|
+
|
|
316
352
|
async def _get_single_page(self, page_number):
|
|
317
353
|
if page_number < 0:
|
|
318
354
|
raise IndexError("Negative page number.")
|
|
@@ -325,6 +361,11 @@ class AsyncIteratorPageSource(PageSource):
|
|
|
325
361
|
if page_number < 0:
|
|
326
362
|
raise IndexError("Negative page number.")
|
|
327
363
|
|
|
364
|
+
if self.per_page == 0:
|
|
365
|
+
while not self._exhausted:
|
|
366
|
+
await self._iterate(100)
|
|
367
|
+
return self._cache
|
|
368
|
+
|
|
328
369
|
base = page_number * self.per_page
|
|
329
370
|
max_base = base + self.per_page
|
|
330
371
|
if not self._exhausted and len(self._cache) <= max_base:
|
|
@@ -332,6 +373,14 @@ class AsyncIteratorPageSource(PageSource):
|
|
|
332
373
|
|
|
333
374
|
entries = self._cache[base:max_base]
|
|
334
375
|
if not entries and max_base > len(self._cache):
|
|
376
|
+
if iscountable(self._iterator) and self._count is None:
|
|
377
|
+
# An attempt was made to read past the last page of a limited
|
|
378
|
+
# countable iterator. The count of entries is reduced to the
|
|
379
|
+
# number that were read before exhaustion and then
|
|
380
|
+
# StopAsyncIteration is signaled for the caller to handle,
|
|
381
|
+
# - e.g. notify the user that some results were omitted
|
|
382
|
+
self.count = len(self._cache)
|
|
383
|
+
raise StopAsyncIteration
|
|
335
384
|
raise IndexError("Went too far")
|
|
336
385
|
return entries
|
|
337
386
|
|
|
@@ -346,6 +395,17 @@ class AsyncIteratorPageSource(PageSource):
|
|
|
346
395
|
---------
|
|
347
396
|
Union[Any, List[Any]]
|
|
348
397
|
The data returned.
|
|
398
|
+
|
|
399
|
+
Raises
|
|
400
|
+
------
|
|
401
|
+
StopAsyncIteration
|
|
402
|
+
Fetching the page reached the end of a limited, countable iterator
|
|
403
|
+
for the first time. The source.count is adjusted to the cache
|
|
404
|
+
length before raising.
|
|
405
|
+
|
|
406
|
+
IndexError
|
|
407
|
+
StopAsyncIteration was not raised and the requested page_number is
|
|
408
|
+
outside of the range of available pages.
|
|
349
409
|
"""
|
|
350
410
|
if self.per_page == 1:
|
|
351
411
|
return await self._get_single_page(page_number)
|