dronefly-core 0.5.0.dev1__tar.gz → 0.5.2__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.2}/PKG-INFO +11 -13
- dronefly_core-0.5.2/dronefly/core/__init__.py +1 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/clients/inat.py +2 -1
- dronefly_core-0.5.2/dronefly/core/commands/__init__.py +4 -0
- dronefly_core-0.5.2/dronefly/core/commands/base.py +125 -0
- dronefly_core-0.5.0.dev1/dronefly/core/commands/__init__.py → dronefly_core-0.5.2/dronefly/core/commands/cli.py +43 -131
- dronefly_core-0.5.2/dronefly/core/commands/constants.py +12 -0
- dronefly_core-0.5.2/dronefly/core/commands/exceptions.py +6 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/constants.py +4 -4
- dronefly_core-0.5.2/dronefly/core/controllers/inat/__init__.py +3 -0
- dronefly_core-0.5.2/dronefly/core/controllers/inat/observation_controller.py +28 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/formatters/generic.py +147 -19
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/__init__.py +1 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/count.py +1 -2
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/counts.py +3 -3
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/menu.py +14 -1
- dronefly_core-0.5.2/dronefly/core/menus/observation_search.py +98 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/taxon_list.py +44 -24
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/__init__.py +1 -0
- dronefly_core-0.5.2/dronefly/core/models/user_count.py +8 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/query/query.py +22 -13
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/query/taxon.py +1 -1
- dronefly_core-0.5.2/pyproject.toml +56 -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.2}/README.md +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/clients/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/controllers/inat/user_controller.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/formatters/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/formatters/constants.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/source.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/menus/taxon.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/base_formatters.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/config.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/context.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/controlled_terms.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/place_count.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/taxon_list.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/models/user.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/paginator.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/parsers/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/parsers/constants.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/parsers/natural.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/parsers/unixlike.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/parsers/url.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/query/__init__.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/query/base.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/dronefly/core/query/formatters.py +0 -0
- {dronefly_core-0.5.0.dev1 → dronefly_core-0.5.2}/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.2
|
|
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,5 @@
|
|
|
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
|
-
from pyinaturalist import UserCount
|
|
10
2
|
|
|
11
|
-
from ..clients.inat import iNatClient
|
|
12
3
|
from ..constants import (
|
|
13
4
|
CONFIG_PATH,
|
|
14
5
|
RANK_EQUIVALENTS,
|
|
@@ -16,8 +7,6 @@ from ..constants import (
|
|
|
16
7
|
RANK_KEYWORDS,
|
|
17
8
|
RANK_LEVELS,
|
|
18
9
|
)
|
|
19
|
-
|
|
20
|
-
from ..parsers import NaturalParser
|
|
21
10
|
from ..parsers.constants import VALID_OBS_SORT_BY
|
|
22
11
|
from ..query import (
|
|
23
12
|
get_place_count,
|
|
@@ -27,11 +16,13 @@ from ..query import (
|
|
|
27
16
|
match_user,
|
|
28
17
|
prepare_query,
|
|
29
18
|
QueryResponse,
|
|
19
|
+
prepare_query_for_search_obs,
|
|
30
20
|
)
|
|
31
21
|
from ..query.formatters import get_query_taxon_formatter
|
|
32
22
|
from ..formatters.generic import (
|
|
33
23
|
format_obs_spp_count,
|
|
34
24
|
ObservationFormatter,
|
|
25
|
+
ObservationSearchFormatter,
|
|
35
26
|
TaxonListFormatter,
|
|
36
27
|
CountsFormatter,
|
|
37
28
|
UserFormatter,
|
|
@@ -39,26 +30,12 @@ from ..formatters.generic import (
|
|
|
39
30
|
)
|
|
40
31
|
from ..menus.counts import CountsSource
|
|
41
32
|
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."""
|
|
33
|
+
from ..menus.observation_search import ObservationSearchSource
|
|
34
|
+
from ..models import Context, UserCount
|
|
53
35
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
class Format(Enum):
|
|
60
|
-
discord_markdown = 1
|
|
61
|
-
rich = 2
|
|
36
|
+
from .base import Commands
|
|
37
|
+
from .constants import Format
|
|
38
|
+
from .exceptions import ArgumentError, CommandError
|
|
62
39
|
|
|
63
40
|
|
|
64
41
|
def _check_obs_query_fields(query_response):
|
|
@@ -69,107 +46,7 @@ def _check_obs_query_fields(query_response):
|
|
|
69
46
|
)
|
|
70
47
|
|
|
71
48
|
|
|
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
|
-
|
|
49
|
+
class CLICommands(Commands):
|
|
173
50
|
async def life(self, ctx: Context, *args):
|
|
174
51
|
"""List lifelist of taxa"""
|
|
175
52
|
_args = " ".join(args) or "by me"
|
|
@@ -524,6 +401,41 @@ class Commands:
|
|
|
524
401
|
|
|
525
402
|
return self._format_markdown(response)
|
|
526
403
|
|
|
404
|
+
async def obs_search(self, ctx: Context, *args):
|
|
405
|
+
"""Search for observations"""
|
|
406
|
+
_args = " ".join(args) or "by me"
|
|
407
|
+
query = self._parse(_args)
|
|
408
|
+
|
|
409
|
+
with self.inat_client.set_ctx(ctx) as client:
|
|
410
|
+
query_response = await prepare_query_for_search_obs(client, query)
|
|
411
|
+
obs_args = query_response.obs_args()
|
|
412
|
+
observations = client.observations.search(**obs_args)
|
|
413
|
+
if not observations:
|
|
414
|
+
raise LookupError(
|
|
415
|
+
f"No observations {query_response.obs_query_description()}"
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
per_page = ctx.per_page
|
|
419
|
+
with_index = self.format == Format.rich
|
|
420
|
+
formatter = ObservationSearchFormatter(
|
|
421
|
+
with_index=with_index,
|
|
422
|
+
)
|
|
423
|
+
source = ObservationSearchSource(
|
|
424
|
+
iterator=observations,
|
|
425
|
+
query_response=query_response,
|
|
426
|
+
formatter=formatter,
|
|
427
|
+
per_page=per_page,
|
|
428
|
+
)
|
|
429
|
+
formatter.source = source
|
|
430
|
+
ctx.page_formatter = formatter
|
|
431
|
+
ctx.page_number = 0
|
|
432
|
+
ctx.selected = 0
|
|
433
|
+
title = formatter.format_title()
|
|
434
|
+
formatted_page = await self._get_formatted_page(
|
|
435
|
+
ctx.page_formatter, ctx.page_number, ctx.selected, header=title
|
|
436
|
+
)
|
|
437
|
+
return self._format_markdown(formatted_page)
|
|
438
|
+
|
|
527
439
|
async def user(self, ctx: Context, user_str: str):
|
|
528
440
|
"""Show user"""
|
|
529
441
|
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
|
+
)
|