dronefly-core 0.5.0.dev1__tar.gz → 0.5.1__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.0.dev1 → dronefly_core-0.5.1}/PKG-INFO +11 -13
- dronefly_core-0.5.1/dronefly/core/__init__.py +1 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/clients/inat.py +2 -1
- dronefly_core-0.5.1/dronefly/core/commands/__init__.py +4 -0
- dronefly_core-0.5.1/dronefly/core/commands/base.py +125 -0
- dronefly_core-0.5.0.dev1/dronefly/core/commands/__init__.py → dronefly_core-0.5.1/dronefly/core/commands/cli.py +43 -130
- dronefly_core-0.5.1/dronefly/core/commands/constants.py +12 -0
- dronefly_core-0.5.1/dronefly/core/commands/exceptions.py +6 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/constants.py +4 -4
- dronefly_core-0.5.1/dronefly/core/controllers/inat/__init__.py +3 -0
- dronefly_core-0.5.1/dronefly/core/controllers/inat/observation_controller.py +28 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/formatters/generic.py +136 -8
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/__init__.py +1 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/menu.py +14 -1
- dronefly_core-0.5.1/dronefly/core/menus/observation_search.py +102 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/taxon_list.py +44 -24
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/query/query.py +6 -2
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/query/taxon.py +1 -1
- dronefly_core-0.5.1/pyproject.toml +53 -0
- dronefly_core-0.5.0.dev1/LICENSE +0 -1132
- dronefly_core-0.5.0.dev1/dronefly/core/__init__.py +0 -1
- dronefly_core-0.5.0.dev1/dronefly/core/controllers/inat/__init__.py +0 -1
- dronefly_core-0.5.0.dev1/pyproject.toml +0 -37
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/README.md +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/clients/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/controllers/inat/user_controller.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/formatters/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/formatters/constants.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/count.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/counts.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/source.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/menus/taxon.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/base_formatters.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/config.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/context.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/controlled_terms.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/place_count.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/taxon_list.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/models/user.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/paginator.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/parsers/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/parsers/constants.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/parsers/natural.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/parsers/unixlike.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/parsers/url.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/query/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/query/base.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/query/formatters.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.1}/dronefly/core/utils/__init__.py +0 -0
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: dronefly-core
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: Core dronefly components
|
|
5
|
-
License: AGPL-3.0-or-later
|
|
6
5
|
Author: Ben Armstrong
|
|
7
|
-
Author-email: synrg@debian.org
|
|
8
|
-
|
|
9
|
-
Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
|
|
6
|
+
Author-email: Ben Armstrong <synrg@debian.org>
|
|
7
|
+
License-Expression: AGPL-3.0-or-later
|
|
10
8
|
Classifier: Programming Language :: Python :: 3
|
|
11
9
|
Classifier: Programming Language :: Python :: 3.11
|
|
12
10
|
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
-
Requires-Dist:
|
|
14
|
-
Requires-Dist:
|
|
15
|
-
Requires-Dist:
|
|
16
|
-
Requires-Dist: inflect
|
|
17
|
-
Requires-Dist:
|
|
18
|
-
Requires-Dist: rich
|
|
11
|
+
Requires-Dist: pyinaturalist>=0.21.1,<0.22
|
|
12
|
+
Requires-Dist: dateparser>=1.1.1,<2
|
|
13
|
+
Requires-Dist: filelock>=3.13.3,<4
|
|
14
|
+
Requires-Dist: inflect>=5.3.0,<6
|
|
15
|
+
Requires-Dist: html2markdown>=0.1.7,<0.2
|
|
16
|
+
Requires-Dist: rich>=13.4
|
|
17
|
+
Requires-Python: >=3.11, <3.13
|
|
19
18
|
Description-Content-Type: text/markdown
|
|
20
19
|
|
|
21
20
|
# Dronefly core
|
|
@@ -37,4 +36,3 @@ subset of Dronefly Discord bot's capabilities, built solely with Dronefly core.
|
|
|
37
36
|
Dronefly Discord bot brings [iNaturalist](https://www.inaturalist.org) taxa,
|
|
38
37
|
observations, and other data from the site into conversations on the
|
|
39
38
|
[Discord](https://discord.com) chat platform.
|
|
40
|
-
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .commands import * # noqa: F401, F403
|
|
@@ -14,7 +14,7 @@ from pyinaturalist.constants import RequestParams
|
|
|
14
14
|
from pyinaturalist.models import T
|
|
15
15
|
|
|
16
16
|
from ..constants import INAT_DEFAULTS, RATELIMIT_FILE, RATELIMIT_LOCK_FILE, CACHE_FILE
|
|
17
|
-
from ..controllers.inat import UserController
|
|
17
|
+
from ..controllers.inat import ObservationController, UserController
|
|
18
18
|
from ..paginator import Paginator
|
|
19
19
|
|
|
20
20
|
|
|
@@ -47,6 +47,7 @@ class iNatClient(pyiNatClient):
|
|
|
47
47
|
}
|
|
48
48
|
super().__init__(*args, **_kwargs)
|
|
49
49
|
self.users = UserController(self)
|
|
50
|
+
self.observations = ObservationController(self)
|
|
50
51
|
self.annotations.async_all = asyncify(self, self.annotations.all)
|
|
51
52
|
self.taxa.populate = asyncify(self, self.taxa.populate)
|
|
52
53
|
self.observations.taxon_summary = asyncify(
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from inspect import signature
|
|
3
|
+
import re
|
|
4
|
+
from typing import Union
|
|
5
|
+
|
|
6
|
+
from rich.markdown import Markdown
|
|
7
|
+
from ..parsers import NaturalParser
|
|
8
|
+
from ..clients.inat import iNatClient
|
|
9
|
+
from ..models import BaseFormatter, load_config, ListFormatter
|
|
10
|
+
from .constants import (
|
|
11
|
+
Format,
|
|
12
|
+
RICH_BQ_END_PAT,
|
|
13
|
+
RICH_BQ_NEWLINE_PAT,
|
|
14
|
+
RICH_NO_BQ_NEWLINE_PAT,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Command:
|
|
19
|
+
def execute():
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class CommandResponse:
|
|
24
|
+
def format_message():
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
def start_menu():
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Commands:
|
|
32
|
+
"""A Dronefly command processor."""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
loop: asyncio.BaseEventLoop,
|
|
37
|
+
format: Format = Format.discord_markdown,
|
|
38
|
+
):
|
|
39
|
+
self.loop = loop
|
|
40
|
+
# TODO: platform: dronefly.Platform
|
|
41
|
+
# - e.g. discord, commandline, web
|
|
42
|
+
self.format = format
|
|
43
|
+
self.inat_client = iNatClient(loop=loop)
|
|
44
|
+
self.parser = NaturalParser()
|
|
45
|
+
self.dronefly_config = load_config()
|
|
46
|
+
|
|
47
|
+
def _parse(self, query_str):
|
|
48
|
+
return self.parser.parse(query_str)
|
|
49
|
+
|
|
50
|
+
async def _get_formatted_page(
|
|
51
|
+
self,
|
|
52
|
+
formatter: Union[ListFormatter, BaseFormatter],
|
|
53
|
+
page_number: int = 0,
|
|
54
|
+
selected: int = 0,
|
|
55
|
+
header: str = None,
|
|
56
|
+
footer: str = None,
|
|
57
|
+
):
|
|
58
|
+
source = getattr(formatter, "source", None)
|
|
59
|
+
if getattr(formatter, "format_page", None):
|
|
60
|
+
if source:
|
|
61
|
+
page = await source.get_page(page_number)
|
|
62
|
+
sig = signature(formatter.format_page)
|
|
63
|
+
if len(sig.parameters) == 3:
|
|
64
|
+
markdown_text = formatter.format_page(page, page_number, selected)
|
|
65
|
+
last_page = formatter.last_page()
|
|
66
|
+
else:
|
|
67
|
+
markdown_text = formatter.format_page(page)
|
|
68
|
+
last_page = 0
|
|
69
|
+
else:
|
|
70
|
+
markdown_text = formatter.format_page(page_number, selected)
|
|
71
|
+
last_page = formatter.last_page()
|
|
72
|
+
if last_page > 0:
|
|
73
|
+
markdown_text = "\n\n".join(
|
|
74
|
+
[markdown_text, f"Page {page_number + 1}/{last_page + 1}"]
|
|
75
|
+
)
|
|
76
|
+
else:
|
|
77
|
+
markdown_text = formatter.format()
|
|
78
|
+
if header or footer:
|
|
79
|
+
markdown_text = "\n\n".join(
|
|
80
|
+
[item for item in (header, markdown_text, footer) if item is not None]
|
|
81
|
+
)
|
|
82
|
+
return markdown_text
|
|
83
|
+
|
|
84
|
+
def _format_markdown(self, markdown_text: str):
|
|
85
|
+
"""Format Rich vs. Discord markdown."""
|
|
86
|
+
if self.format == Format.rich:
|
|
87
|
+
# Richify the markdown:
|
|
88
|
+
# - In Discord markdown, all newlines are rendered as line breaks
|
|
89
|
+
# - In Rich:
|
|
90
|
+
# - Before every newline, emit " \" to force a line break, except
|
|
91
|
+
# for these exceptions to handle blockquotes:
|
|
92
|
+
# - Don't do this for a line preceding a blockquote
|
|
93
|
+
# - Also don't do this on the last line of a blockquote
|
|
94
|
+
# - Ensure the last line of each blockquote has two newlines to
|
|
95
|
+
# end it
|
|
96
|
+
|
|
97
|
+
# Replace all but last newline of blockquote with line break sequence:
|
|
98
|
+
rich_markdown = re.sub(RICH_BQ_NEWLINE_PAT, r"\1 \\\n", markdown_text)
|
|
99
|
+
# Add extra newline at end of blockquote so following text won't be
|
|
100
|
+
# tacked on:
|
|
101
|
+
rich_markdown = re.sub(RICH_BQ_END_PAT, r"\1\n\n", rich_markdown)
|
|
102
|
+
# Finally, on any line that isn't part of a blockquote, isn't empty,
|
|
103
|
+
# and isn't at the end of the string, emit a line break:
|
|
104
|
+
rich_markdown = re.sub(RICH_NO_BQ_NEWLINE_PAT, r"\1 \\\n", rich_markdown)
|
|
105
|
+
response = Markdown(rich_markdown)
|
|
106
|
+
else:
|
|
107
|
+
# Return the literal markdown for Discord to render
|
|
108
|
+
response = markdown_text
|
|
109
|
+
return response
|
|
110
|
+
|
|
111
|
+
def _simple_format_markdown(self, markdown_text: str):
|
|
112
|
+
"""Simplified formatter for Rich vs. Discord markdown.
|
|
113
|
+
|
|
114
|
+
Discord vs. Rich linebreak rendering is harder than we thought, e.g.
|
|
115
|
+
`_format_markdown()` doesn't give correct results with point-form
|
|
116
|
+
or numbered lists. If special handling of newlines isn't needed, then
|
|
117
|
+
use this helper instead.
|
|
118
|
+
"""
|
|
119
|
+
if self.format == Format.rich:
|
|
120
|
+
# Richify the markdown
|
|
121
|
+
response = Markdown(markdown_text)
|
|
122
|
+
else:
|
|
123
|
+
# Return the literal markdown for Discord to render
|
|
124
|
+
response = markdown_text
|
|
125
|
+
return response
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
from enum import Enum
|
|
3
|
-
from inspect import signature
|
|
4
|
-
import re
|
|
5
|
-
from typing import Union
|
|
6
|
-
|
|
7
1
|
from requests import HTTPError
|
|
8
|
-
from rich.markdown import Markdown
|
|
9
2
|
from pyinaturalist import UserCount
|
|
10
3
|
|
|
11
|
-
from ..clients.inat import iNatClient
|
|
12
4
|
from ..constants import (
|
|
13
5
|
CONFIG_PATH,
|
|
14
6
|
RANK_EQUIVALENTS,
|
|
@@ -16,8 +8,6 @@ from ..constants import (
|
|
|
16
8
|
RANK_KEYWORDS,
|
|
17
9
|
RANK_LEVELS,
|
|
18
10
|
)
|
|
19
|
-
|
|
20
|
-
from ..parsers import NaturalParser
|
|
21
11
|
from ..parsers.constants import VALID_OBS_SORT_BY
|
|
22
12
|
from ..query import (
|
|
23
13
|
get_place_count,
|
|
@@ -27,11 +17,13 @@ from ..query import (
|
|
|
27
17
|
match_user,
|
|
28
18
|
prepare_query,
|
|
29
19
|
QueryResponse,
|
|
20
|
+
prepare_query_for_search_obs,
|
|
30
21
|
)
|
|
31
22
|
from ..query.formatters import get_query_taxon_formatter
|
|
32
23
|
from ..formatters.generic import (
|
|
33
24
|
format_obs_spp_count,
|
|
34
25
|
ObservationFormatter,
|
|
26
|
+
ObservationSearchFormatter,
|
|
35
27
|
TaxonListFormatter,
|
|
36
28
|
CountsFormatter,
|
|
37
29
|
UserFormatter,
|
|
@@ -39,26 +31,12 @@ from ..formatters.generic import (
|
|
|
39
31
|
)
|
|
40
32
|
from ..menus.counts import CountsSource
|
|
41
33
|
from ..menus.taxon_list import TaxonListSource
|
|
42
|
-
from ..
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
RICH_BQ_NEWLINE_PAT = re.compile(r"^(\> .*?)\n(?=\> )", re.MULTILINE)
|
|
46
|
-
RICH_BQ_END_PAT = re.compile(r"^((\> .*?\n)+)(?!> )", re.MULTILINE)
|
|
47
|
-
RICH_NO_BQ_NEWLINE_PAT = re.compile(r"^(?!\> )(.+?)(\n)(?!$|\> )", re.MULTILINE)
|
|
48
|
-
RICH_NEWLINE = " \\\n"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
class ArgumentError(ValueError):
|
|
52
|
-
"""Command arguments are not valid."""
|
|
34
|
+
from ..menus.observation_search import ObservationSearchSource
|
|
35
|
+
from ..models import Context
|
|
53
36
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
class Format(Enum):
|
|
60
|
-
discord_markdown = 1
|
|
61
|
-
rich = 2
|
|
37
|
+
from .base import Commands
|
|
38
|
+
from .constants import Format
|
|
39
|
+
from .exceptions import ArgumentError, CommandError
|
|
62
40
|
|
|
63
41
|
|
|
64
42
|
def _check_obs_query_fields(query_response):
|
|
@@ -69,107 +47,7 @@ def _check_obs_query_fields(query_response):
|
|
|
69
47
|
)
|
|
70
48
|
|
|
71
49
|
|
|
72
|
-
|
|
73
|
-
# handling each thing:
|
|
74
|
-
# - Context
|
|
75
|
-
# - user, channel, etc.
|
|
76
|
-
# - affects which settings are passed to inat (e.g. home place for conservation status)
|
|
77
|
-
class Commands:
|
|
78
|
-
"""A Dronefly command processor."""
|
|
79
|
-
|
|
80
|
-
def __init__(
|
|
81
|
-
self,
|
|
82
|
-
loop: asyncio.BaseEventLoop,
|
|
83
|
-
format: Format = Format.discord_markdown,
|
|
84
|
-
):
|
|
85
|
-
self.loop = loop
|
|
86
|
-
# TODO: platform: dronefly.Platform
|
|
87
|
-
# - e.g. discord, commandline, web
|
|
88
|
-
self.format = format
|
|
89
|
-
self.inat_client = iNatClient(loop=loop)
|
|
90
|
-
self.parser = NaturalParser()
|
|
91
|
-
self.dronefly_config = load_config()
|
|
92
|
-
|
|
93
|
-
def _parse(self, query_str):
|
|
94
|
-
return self.parser.parse(query_str)
|
|
95
|
-
|
|
96
|
-
async def _get_formatted_page(
|
|
97
|
-
self,
|
|
98
|
-
formatter: Union[ListFormatter, BaseFormatter],
|
|
99
|
-
page_number: int = 0,
|
|
100
|
-
selected: int = 0,
|
|
101
|
-
header: str = None,
|
|
102
|
-
footer: str = None,
|
|
103
|
-
):
|
|
104
|
-
source = getattr(formatter, "source", None)
|
|
105
|
-
if getattr(formatter, "format_page", None):
|
|
106
|
-
if source:
|
|
107
|
-
page = await source.get_page(page_number)
|
|
108
|
-
sig = signature(formatter.format_page)
|
|
109
|
-
if len(sig.parameters) == 3:
|
|
110
|
-
markdown_text = formatter.format_page(page, page_number, selected)
|
|
111
|
-
last_page = formatter.last_page()
|
|
112
|
-
else:
|
|
113
|
-
markdown_text = formatter.format_page(page)
|
|
114
|
-
last_page = 0
|
|
115
|
-
else:
|
|
116
|
-
markdown_text = formatter.format_page(page_number, selected)
|
|
117
|
-
last_page = formatter.last_page()
|
|
118
|
-
if last_page > 0:
|
|
119
|
-
markdown_text = "\n\n".join(
|
|
120
|
-
[markdown_text, f"Page {page_number + 1}/{last_page + 1}"]
|
|
121
|
-
)
|
|
122
|
-
else:
|
|
123
|
-
markdown_text = formatter.format()
|
|
124
|
-
if header or footer:
|
|
125
|
-
markdown_text = "\n\n".join(
|
|
126
|
-
[item for item in (header, markdown_text, footer) if item is not None]
|
|
127
|
-
)
|
|
128
|
-
return markdown_text
|
|
129
|
-
|
|
130
|
-
def _format_markdown(self, markdown_text: str):
|
|
131
|
-
"""Format Rich vs. Discord markdown."""
|
|
132
|
-
if self.format == Format.rich:
|
|
133
|
-
# Richify the markdown:
|
|
134
|
-
# - In Discord markdown, all newlines are rendered as line breaks
|
|
135
|
-
# - In Rich:
|
|
136
|
-
# - Before every newline, emit " \" to force a line break, except
|
|
137
|
-
# for these exceptions to handle blockquotes:
|
|
138
|
-
# - Don't do this for a line preceding a blockquote
|
|
139
|
-
# - Also don't do this on the last line of a blockquote
|
|
140
|
-
# - Ensure the last line of each blockquote has two newlines to
|
|
141
|
-
# end it
|
|
142
|
-
|
|
143
|
-
# Replace all but last newline of blockquote with line break sequence:
|
|
144
|
-
rich_markdown = re.sub(RICH_BQ_NEWLINE_PAT, r"\1 \\\n", markdown_text)
|
|
145
|
-
# Add extra newline at end of blockquote so following text won't be
|
|
146
|
-
# tacked on:
|
|
147
|
-
rich_markdown = re.sub(RICH_BQ_END_PAT, r"\1\n\n", rich_markdown)
|
|
148
|
-
# Finally, on any line that isn't part of a blockquote, isn't empty,
|
|
149
|
-
# and isn't at the end of the string, emit a line break:
|
|
150
|
-
rich_markdown = re.sub(RICH_NO_BQ_NEWLINE_PAT, r"\1 \\\n", rich_markdown)
|
|
151
|
-
response = Markdown(rich_markdown)
|
|
152
|
-
else:
|
|
153
|
-
# Return the literal markdown for Discord to render
|
|
154
|
-
response = markdown_text
|
|
155
|
-
return response
|
|
156
|
-
|
|
157
|
-
def _simple_format_markdown(self, markdown_text: str):
|
|
158
|
-
"""Simplified formatter for Rich vs. Discord markdown.
|
|
159
|
-
|
|
160
|
-
Discord vs. Rich linebreak rendering is harder than we thought, e.g.
|
|
161
|
-
`_format_markdown()` doesn't give correct results with point-form
|
|
162
|
-
or numbered lists. If special handling of newlines isn't needed, then
|
|
163
|
-
use this helper instead.
|
|
164
|
-
"""
|
|
165
|
-
if self.format == Format.rich:
|
|
166
|
-
# Richify the markdown
|
|
167
|
-
response = Markdown(markdown_text)
|
|
168
|
-
else:
|
|
169
|
-
# Return the literal markdown for Discord to render
|
|
170
|
-
response = markdown_text
|
|
171
|
-
return response
|
|
172
|
-
|
|
50
|
+
class CLICommands(Commands):
|
|
173
51
|
async def life(self, ctx: Context, *args):
|
|
174
52
|
"""List lifelist of taxa"""
|
|
175
53
|
_args = " ".join(args) or "by me"
|
|
@@ -524,6 +402,41 @@ class Commands:
|
|
|
524
402
|
|
|
525
403
|
return self._format_markdown(response)
|
|
526
404
|
|
|
405
|
+
async def obs_search(self, ctx: Context, *args):
|
|
406
|
+
"""Search for observations"""
|
|
407
|
+
_args = " ".join(args) or "by me"
|
|
408
|
+
query = self._parse(_args)
|
|
409
|
+
|
|
410
|
+
with self.inat_client.set_ctx(ctx) as client:
|
|
411
|
+
query_response = await prepare_query_for_search_obs(client, query)
|
|
412
|
+
obs_args = query_response.obs_args()
|
|
413
|
+
observations = client.observations.search(**obs_args)
|
|
414
|
+
if not observations:
|
|
415
|
+
raise LookupError(
|
|
416
|
+
f"No observations {query_response.obs_query_description()}"
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
per_page = ctx.per_page
|
|
420
|
+
with_index = self.format == Format.rich
|
|
421
|
+
formatter = ObservationSearchFormatter(
|
|
422
|
+
with_index=with_index,
|
|
423
|
+
)
|
|
424
|
+
source = ObservationSearchSource(
|
|
425
|
+
iterator=observations,
|
|
426
|
+
query_response=query_response,
|
|
427
|
+
formatter=formatter,
|
|
428
|
+
per_page=per_page,
|
|
429
|
+
)
|
|
430
|
+
formatter.source = source
|
|
431
|
+
ctx.page_formatter = formatter
|
|
432
|
+
ctx.page_number = 0
|
|
433
|
+
ctx.selected = 0
|
|
434
|
+
title = formatter.format_title()
|
|
435
|
+
formatted_page = await self._get_formatted_page(
|
|
436
|
+
ctx.page_formatter, ctx.page_number, ctx.selected, header=title
|
|
437
|
+
)
|
|
438
|
+
return self._format_markdown(formatted_page)
|
|
439
|
+
|
|
527
440
|
async def user(self, ctx: Context, user_str: str):
|
|
528
441
|
"""Show user"""
|
|
529
442
|
with self.inat_client.set_ctx(ctx) as client:
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
RICH_BQ_NEWLINE_PAT = re.compile(r"^(\> .*?)\n(?=\> )", re.MULTILINE)
|
|
5
|
+
RICH_BQ_END_PAT = re.compile(r"^((\> .*?\n)+)(?!> )", re.MULTILINE)
|
|
6
|
+
RICH_NO_BQ_NEWLINE_PAT = re.compile(r"^(?!\> )(.+?)(\n)(?!$|\> )", re.MULTILINE)
|
|
7
|
+
RICH_NEWLINE = " \\\n"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Format(Enum):
|
|
11
|
+
discord_markdown = 1
|
|
12
|
+
rich = 2
|
|
@@ -24,16 +24,16 @@ PLANTAE_ID = 47126
|
|
|
24
24
|
TRACHEOPHYTA_ID = 211194
|
|
25
25
|
RANK_KEYWORDS = tuple(RANK_LEVELS.keys()) + tuple(RANK_EQUIVALENTS.keys())
|
|
26
26
|
RANK_LEVEL_NAMES = {}
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
|
|
27
|
+
# TODO: import from pyinaturalist.constants instead of defining here:
|
|
28
|
+
# - see https://github.com/pyinat/pyinaturalist/commit/82cee9d642538a8afb3add8809cca161345bdc68
|
|
29
|
+
RANK_LEVEL_REPRESENTATIVE_RANKS = {
|
|
30
30
|
5: "subspecies",
|
|
31
31
|
10: "species",
|
|
32
32
|
20: "genus",
|
|
33
33
|
}
|
|
34
34
|
RANKS_FOR_LEVEL = {}
|
|
35
35
|
for (rank, level) in RANK_LEVELS.items():
|
|
36
|
-
RANK_LEVEL_NAMES[level] =
|
|
36
|
+
RANK_LEVEL_NAMES[level] = RANK_LEVEL_REPRESENTATIVE_RANKS.get(level) or rank
|
|
37
37
|
if level not in RANKS_FOR_LEVEL:
|
|
38
38
|
RANKS_FOR_LEVEL[level] = [rank]
|
|
39
39
|
else:
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from pyinaturalist.constants import V1_OBS_ORDER_BY_PROPERTIES
|
|
2
|
+
from pyinaturalist.controllers import (
|
|
3
|
+
ObservationController as pyiNatObservationController,
|
|
4
|
+
)
|
|
5
|
+
from pyinaturalist.docs import copy_doc_signature
|
|
6
|
+
from pyinaturalist.docs import templates as docs
|
|
7
|
+
from pyinaturalist.models import Observation
|
|
8
|
+
from pyinaturalist.request_params import validate_multiple_choice_param
|
|
9
|
+
from pyinaturalist.v1 import get_observations
|
|
10
|
+
|
|
11
|
+
from ...paginator import Paginator
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ObservationController(pyiNatObservationController):
|
|
15
|
+
@copy_doc_signature(*docs._get_observations, docs._only_id)
|
|
16
|
+
def search(self, **params) -> Paginator[Observation]:
|
|
17
|
+
params = validate_multiple_choice_param(
|
|
18
|
+
params, "order_by", V1_OBS_ORDER_BY_PROPERTIES
|
|
19
|
+
)
|
|
20
|
+
params = self.client.add_defaults(get_observations, params)
|
|
21
|
+
|
|
22
|
+
return Paginator(
|
|
23
|
+
get_observations,
|
|
24
|
+
Observation,
|
|
25
|
+
loop=self.client.loop,
|
|
26
|
+
annotation_callback=self.client.annotations.lookup,
|
|
27
|
+
**params,
|
|
28
|
+
)
|
|
@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Optional, Union
|
|
|
12
12
|
if TYPE_CHECKING:
|
|
13
13
|
from ..query import QueryResponse
|
|
14
14
|
from ..menus.taxon_list import TaxonListSource
|
|
15
|
+
from ..menus.observation_search import ObservationSearchSource
|
|
15
16
|
from ..menus.count import CountSource
|
|
16
17
|
from ..menus.counts import CountsSource
|
|
17
18
|
from ..models import PlaceCount
|
|
@@ -541,6 +542,140 @@ def format_obs_spp_summary(
|
|
|
541
542
|
return summary
|
|
542
543
|
|
|
543
544
|
|
|
545
|
+
class ObservationSearchFormatter(ListFormatter):
|
|
546
|
+
"""
|
|
547
|
+
Attributes
|
|
548
|
+
----------
|
|
549
|
+
source: ObservationSearchSource
|
|
550
|
+
Source of observations. The source must be set before any format methods
|
|
551
|
+
can be called.
|
|
552
|
+
"""
|
|
553
|
+
|
|
554
|
+
source: ObservationSearchSource
|
|
555
|
+
|
|
556
|
+
def __init__(
|
|
557
|
+
self,
|
|
558
|
+
with_index: bool = False,
|
|
559
|
+
with_url: bool = True,
|
|
560
|
+
short_description: str = "Search: Observations",
|
|
561
|
+
):
|
|
562
|
+
"""
|
|
563
|
+
Parameters
|
|
564
|
+
----------
|
|
565
|
+
with_index: bool, optional
|
|
566
|
+
When True, output index per observation in page.
|
|
567
|
+
with_url: bool, optional
|
|
568
|
+
When True, link the title to the observations matching the query.
|
|
569
|
+
short_description: str, optional [default: `Search: Observations`]
|
|
570
|
+
Short description of observation search that appears in the title.
|
|
571
|
+
"""
|
|
572
|
+
self.with_index = with_index
|
|
573
|
+
self.with_url = with_url
|
|
574
|
+
self.short_description = short_description
|
|
575
|
+
|
|
576
|
+
def format(
|
|
577
|
+
self,
|
|
578
|
+
page: Union[Observation, list[Observation]] = None,
|
|
579
|
+
page_number: Optional[int] = None,
|
|
580
|
+
selected: Optional[int] = None,
|
|
581
|
+
with_title: bool = True,
|
|
582
|
+
):
|
|
583
|
+
"""Format the observation page as markdown."""
|
|
584
|
+
description = self.format_page(page, page_number, selected)
|
|
585
|
+
if with_title:
|
|
586
|
+
description = "\n\n".join([self.format_title(), description])
|
|
587
|
+
return description
|
|
588
|
+
|
|
589
|
+
def format_title(self):
|
|
590
|
+
"""Format observation page title as Discord-like markdown.
|
|
591
|
+
|
|
592
|
+
Returns
|
|
593
|
+
-------
|
|
594
|
+
str
|
|
595
|
+
- Describe an observations search page in terms of its query parameters.
|
|
596
|
+
"""
|
|
597
|
+
title = f"{self.short_description} {self.source.query_response.obs_query_description()}"
|
|
598
|
+
if self.with_url:
|
|
599
|
+
url = obs_url_from_v1(self.source.query_response.obs_args())
|
|
600
|
+
title = format_link(title, url)
|
|
601
|
+
return title
|
|
602
|
+
|
|
603
|
+
def format_page(
|
|
604
|
+
self,
|
|
605
|
+
page: Union[Taxon, list[Taxon]] = None,
|
|
606
|
+
page_number: int = 0,
|
|
607
|
+
selected: int = 0,
|
|
608
|
+
):
|
|
609
|
+
"""Format the observation list page."""
|
|
610
|
+
|
|
611
|
+
def format_page_of_obs(page: list[Observation]):
|
|
612
|
+
formatted_obs = []
|
|
613
|
+
for obs in page:
|
|
614
|
+
taxon_name = format_taxon_name(obs.taxon, with_common=False)
|
|
615
|
+
obs_url = f"{WWW_BASE_URL}/observations/{obs.id}"
|
|
616
|
+
formatted_name = format_link(taxon_name, obs_url)
|
|
617
|
+
if obs.observed_on:
|
|
618
|
+
date = format_datetime(obs.observed_on, compact=True)
|
|
619
|
+
else:
|
|
620
|
+
date = "no date"
|
|
621
|
+
formatted_obs.append(
|
|
622
|
+
{
|
|
623
|
+
"date": date,
|
|
624
|
+
"name": formatted_name,
|
|
625
|
+
}
|
|
626
|
+
)
|
|
627
|
+
return formatted_obs
|
|
628
|
+
|
|
629
|
+
def make_page_content(page: list[Observation], with_summary: bool = False):
|
|
630
|
+
"""Format all parts of the page content."""
|
|
631
|
+
structured_page = {
|
|
632
|
+
"header": None,
|
|
633
|
+
"entries_header": None,
|
|
634
|
+
"entries": [],
|
|
635
|
+
"footer": None,
|
|
636
|
+
}
|
|
637
|
+
if page:
|
|
638
|
+
formatted_obs = format_page_of_obs(page)
|
|
639
|
+
structured_page["entries"] = formatted_obs
|
|
640
|
+
return structured_page
|
|
641
|
+
|
|
642
|
+
def assemble_page(content: dict, selected: int = 0):
|
|
643
|
+
"""Assemble page content into a formatted page."""
|
|
644
|
+
sections = []
|
|
645
|
+
if content["header"]:
|
|
646
|
+
sections.append(content["header"])
|
|
647
|
+
if content["entries_header"]:
|
|
648
|
+
sections.append(content["entries_header"])
|
|
649
|
+
if content["entries"]:
|
|
650
|
+
entries = []
|
|
651
|
+
for (index, entry) in enumerate(content["entries"]):
|
|
652
|
+
_i = f"**`{str(index + 1).zfill(2)}) `**" if self.with_index else ""
|
|
653
|
+
if selected == index:
|
|
654
|
+
_s = ">"
|
|
655
|
+
_n = "**__"
|
|
656
|
+
_e = "__**"
|
|
657
|
+
else:
|
|
658
|
+
_s = "\N{EN SPACE}"
|
|
659
|
+
_n = ""
|
|
660
|
+
_e = ""
|
|
661
|
+
entries.append(
|
|
662
|
+
f"{_i}`{entry['date']}`" f"{_s}{_n}{entry['name']}{_e}"
|
|
663
|
+
)
|
|
664
|
+
sections.append("\n".join(entries))
|
|
665
|
+
if content["footer"]:
|
|
666
|
+
sections.append(content["footer"])
|
|
667
|
+
return "\n\n".join(sections)
|
|
668
|
+
|
|
669
|
+
_page = [page] if isinstance(page, Observation) else page
|
|
670
|
+
page_content = make_page_content(_page)
|
|
671
|
+
return assemble_page(page_content, selected)
|
|
672
|
+
|
|
673
|
+
def last_page(self):
|
|
674
|
+
if not (self.source.per_page > 0 and self.source._cache):
|
|
675
|
+
return 0
|
|
676
|
+
return self.source.get_max_pages() - 1
|
|
677
|
+
|
|
678
|
+
|
|
544
679
|
class TaxonListFormatter(ListFormatter):
|
|
545
680
|
"""
|
|
546
681
|
Attributes
|
|
@@ -1305,11 +1440,4 @@ class UserFormatter(BaseFormatter):
|
|
|
1305
1440
|
with_link: bool, optional
|
|
1306
1441
|
Link to user's profile.
|
|
1307
1442
|
"""
|
|
1308
|
-
|
|
1309
|
-
if name:
|
|
1310
|
-
name += f" ({self.user.login})"
|
|
1311
|
-
else:
|
|
1312
|
-
name = self.user.login
|
|
1313
|
-
if with_link:
|
|
1314
|
-
name = f"[{name}]({self.user.url})"
|
|
1315
|
-
return name
|
|
1443
|
+
return format_user_link(self.user) if with_link else format_user_name(self.user)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from .source import ListPageSource, PageSource
|
|
1
|
+
from .source import ListPageSource, PageSource, AsyncIteratorPageSource
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class BaseMenu:
|
|
@@ -21,3 +21,16 @@ class BaseListMenu(BaseMenu):
|
|
|
21
21
|
self.current_page = current_page
|
|
22
22
|
self.selected = selected
|
|
23
23
|
super().__init__(source)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class BaseSearchMenu(BaseMenu):
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
source: AsyncIteratorPageSource,
|
|
30
|
+
current_page: int = 0,
|
|
31
|
+
selected: int = 0,
|
|
32
|
+
) -> None:
|
|
33
|
+
self.source = source
|
|
34
|
+
self.current_page = current_page
|
|
35
|
+
self.selected = selected
|
|
36
|
+
super().__init__(source)
|