dronefly-core 0.3.6.dev7__tar.gz → 0.3.6.dev9__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 (24) hide show
  1. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/PKG-INFO +2 -2
  2. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/commands/__init__.py +41 -9
  3. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/formatters/generic.py +59 -22
  4. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/parsers/url.py +7 -11
  5. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/pyproject.toml +2 -2
  6. dronefly_core-0.3.6.dev7/setup.py +0 -41
  7. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/LICENSE +0 -0
  8. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/README.md +0 -0
  9. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/__init__.py +0 -0
  10. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/clients/__init__.py +0 -0
  11. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/clients/inat.py +0 -0
  12. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/constants.py +0 -0
  13. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/formatters/__init__.py +0 -0
  14. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/formatters/constants.py +0 -0
  15. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/models/__init__.py +0 -0
  16. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/models/controlled_terms.py +0 -0
  17. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/models/user.py +0 -0
  18. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/parsers/__init__.py +0 -0
  19. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/parsers/constants.py +0 -0
  20. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/parsers/natural.py +0 -0
  21. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/parsers/unixlike.py +0 -0
  22. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/query/__init__.py +0 -0
  23. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/query/query.py +0 -0
  24. {dronefly_core-0.3.6.dev7 → dronefly_core-0.3.6.dev9}/dronefly/core/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dronefly-core
3
- Version: 0.3.6.dev7
3
+ Version: 0.3.6.dev9
4
4
  Summary: Core dronefly components
5
5
  License: AGPL-3.0-or-later
6
6
  Author: Ben Armstrong
@@ -15,7 +15,7 @@ Requires-Dist: dateparser (>=1.1.1,<2.0.0)
15
15
  Requires-Dist: html2markdown (>=0.1.7,<0.2.0)
16
16
  Requires-Dist: inflect (>=5.3.0,<6.0.0)
17
17
  Requires-Dist: pyinaturalist (>=0.19.0.dev3,<0.20)
18
- Requires-Dist: rich (==13.4.2)
18
+ Requires-Dist: rich (>=13.4)
19
19
  Description-Content-Type: text/markdown
20
20
 
21
21
  # Dronefly core
@@ -26,6 +26,14 @@ RICH_NO_BQ_NEWLINE_PAT = re.compile(r"^(?!\> )(.+?)(\n)(?!$|\> )", re.MULTILINE)
26
26
  RICH_NEWLINE = " \\\n"
27
27
 
28
28
 
29
+ class ArgumentError(ValueError):
30
+ """Command arguments are not valid."""
31
+
32
+
33
+ class CommandError(NameError):
34
+ """Command is not known."""
35
+
36
+
29
37
  class Format(Enum):
30
38
  discord_markdown = 1
31
39
  rich = 2
@@ -47,6 +55,7 @@ class Context:
47
55
  page_formatter: Union[BaseFormatter, ListFormatter] = None
48
56
  page: int = 0
49
57
  per_page: int = 0
58
+ selected: int = 0
50
59
 
51
60
  def get_inat_user_default(self, inat_param: str):
52
61
  """Return iNat API default for user param default, if any, otherwise global default."""
@@ -89,9 +98,9 @@ class Commands:
89
98
  def _parse(self, query_str):
90
99
  return self.parser.parse(query_str)
91
100
 
92
- def _get_formatted_page(self, formatter, page: int = 0):
101
+ def _get_formatted_page(self, formatter, page: int = 0, selected: int = 0):
93
102
  if getattr(formatter, "format_page", None):
94
- markdown_text = formatter.format_page(page)
103
+ markdown_text = formatter.format_page(page, selected)
95
104
  last_page = formatter.last_page()
96
105
  if last_page > 0:
97
106
  markdown_text = "\n\n".join(
@@ -164,17 +173,24 @@ class Commands:
164
173
  return f"No life list {query_response.obs_query_description()}"
165
174
 
166
175
  per_page = ctx.per_page
176
+ with_index = self.format == Format.rich
167
177
  formatter = LifeListFormatter(
168
- life_list, per_rank, query_response, with_indent=True, per_page=per_page
178
+ life_list,
179
+ per_rank,
180
+ query_response,
181
+ with_indent=True,
182
+ per_page=per_page,
183
+ with_index=with_index,
169
184
  )
170
185
  ctx.page_formatter = formatter
171
186
  ctx.page = 0
187
+ ctx.selected = 0
172
188
  title = formatter.format_title()
173
189
  first_page = formatter.get_first_page() or ""
174
190
  if first_page:
175
- first_page = "\n".join([title, "", first_page])
176
- formatter.pages[0] = first_page
177
- return self._get_formatted_page(formatter, 0)
191
+ # TODO: Provide a method in the formatter to set the title:
192
+ formatter.pages[0]["header"] = title
193
+ return self._get_formatted_page(formatter, 0, 0)
178
194
 
179
195
  def next(self, ctx: Context):
180
196
  if not ctx.page_formatter:
@@ -182,7 +198,8 @@ class Commands:
182
198
  ctx.page += 1
183
199
  if ctx.page > ctx.page_formatter.last_page():
184
200
  ctx.page = 0
185
- return self._get_formatted_page(ctx.page_formatter, ctx.page)
201
+ ctx.selected = 0
202
+ return self._get_formatted_page(ctx.page_formatter, ctx.page, ctx.selected)
186
203
 
187
204
  def page(self, ctx: Context, page: int = 1):
188
205
  if not ctx.page_formatter:
@@ -194,7 +211,21 @@ class Commands:
194
211
  msg += f" through {last_page}"
195
212
  return msg
196
213
  ctx.page = page - 1
197
- return self._get_formatted_page(ctx.page_formatter, ctx.page)
214
+ ctx.selected = 0
215
+ return self._get_formatted_page(ctx.page_formatter, ctx.page, ctx.selected)
216
+
217
+ def sel(self, ctx: Context, sel: int = 1):
218
+ if not ctx.page_formatter:
219
+ return "Type a command that has pages first"
220
+ _page = ctx.page_formatter.get_page_of_taxa(ctx.page)
221
+ page_len = len(_page)
222
+ if sel > page_len or sel < 1:
223
+ msg = "Specify entry 1"
224
+ if page_len > 1:
225
+ msg += f" through {page_len}"
226
+ return msg
227
+ ctx.selected = sel - 1
228
+ return self._get_formatted_page(ctx.page_formatter, ctx.page, ctx.selected)
198
229
 
199
230
  def prev(self, ctx: Context):
200
231
  if not ctx.page_formatter:
@@ -202,7 +233,8 @@ class Commands:
202
233
  ctx.page -= 1
203
234
  if ctx.page < 0:
204
235
  ctx.page = ctx.page_formatter.last_page()
205
- return self._get_formatted_page(ctx.page_formatter, ctx.page)
236
+ ctx.selected = 0
237
+ return self._get_formatted_page(ctx.page_formatter, ctx.page, ctx.selected)
206
238
 
207
239
  def taxon(self, ctx: Context, *args):
208
240
  query = self._parse(" ".join(args))
@@ -8,7 +8,7 @@ import copy
8
8
  from datetime import datetime as dt
9
9
  from math import ceil
10
10
  import re
11
- from typing import TYPE_CHECKING, Union
11
+ from typing import TYPE_CHECKING, Optional, Union
12
12
 
13
13
  if TYPE_CHECKING:
14
14
  from dronefly.core.query.query import QueryResponse
@@ -615,6 +615,7 @@ class LifeListFormatter(ListFormatter):
615
615
  with_url: bool = True,
616
616
  with_taxa: bool = True,
617
617
  with_indent: bool = False,
618
+ with_index: bool = False,
618
619
  with_direct: bool = False,
619
620
  with_common: bool = False,
620
621
  per_page: int = 20,
@@ -681,6 +682,7 @@ class LifeListFormatter(ListFormatter):
681
682
  self.with_url = with_url
682
683
  self.with_taxa = with_taxa
683
684
  self.with_indent = with_indent
685
+ self.with_index = with_index
684
686
  self.with_direct = with_direct
685
687
  self.with_common = with_common
686
688
  self.pages = []
@@ -697,9 +699,11 @@ class LifeListFormatter(ListFormatter):
697
699
  self.life_list, self.per_rank, self.query_response.taxon, self.root_taxon_id
698
700
  )
699
701
 
700
- def format(self, with_title: bool = True, page: int = 0):
702
+ def format(
703
+ self, with_title: bool = True, page: int = 0, selected: Optional[int] = None
704
+ ):
701
705
  """Format the life list as markdown."""
702
- description = self.format_page(page)
706
+ description = self.format_page(page, selected)
703
707
  if with_title:
704
708
  description = "\n\n".join([self.format_title(), description])
705
709
  return description
@@ -723,7 +727,7 @@ class LifeListFormatter(ListFormatter):
723
727
  title = format_link(title, url)
724
728
  return title
725
729
 
726
- def format_page(self, page: int = 0):
730
+ def format_page(self, page: int = 0, selected: Optional[int] = None):
727
731
  """Format the life list description."""
728
732
 
729
733
  def indent_level(taxon: Taxon):
@@ -799,33 +803,38 @@ class LifeListFormatter(ListFormatter):
799
803
  if self.with_common and taxon.preferred_common_name:
800
804
  formatted_name = f"{formatted_name} ({taxon.preferred_common_name})"
801
805
  formatted_taxa.append(
802
- f"`{formatted_count}{formatted_direct}` {indent_child(taxon)}{formatted_name}"
806
+ {
807
+ "count": formatted_count,
808
+ "direct": formatted_direct,
809
+ "indent": indent_child(taxon),
810
+ "name": formatted_name,
811
+ }
803
812
  )
804
813
  return formatted_taxa
805
814
 
806
- def make_page(page):
807
- sections = []
808
- query_response = self.query_response
815
+ def make_structured_page(page):
816
+ structured_page = {
817
+ "header": None,
818
+ "entries_header": None,
819
+ "entries": [],
820
+ "footer": None,
821
+ }
809
822
  with_page_headers = self.per_rank in ("main", "any")
810
- if page == 0:
811
- obs_link = format_link(
812
- "Observations",
813
- obs_url_from_v1(query_response.obs_args()),
814
- )
815
- sections.append(obs_link)
816
823
  if self.taxa and self.with_taxa:
817
824
  page_of_taxa = self.get_page_of_taxa(page)
818
825
  if page_of_taxa:
819
826
  formatted_taxa = format_page_of_taxa(page_of_taxa)
820
827
  if with_page_headers and indent_level(page_of_taxa[0]) > 1:
821
- formatted_taxa.insert(0, make_page_header(page_of_taxa[0]))
822
- sections.append("\n".join(formatted_taxa))
828
+ structured_page["entries_header"] = make_page_header(
829
+ page_of_taxa[0]
830
+ )
831
+ structured_page["entries"] = formatted_taxa
823
832
  if page == self.last_page():
824
833
  life_list_summary = format_life_list_summary(
825
834
  self.taxa, self.ranks, self.rank_totals
826
835
  )
827
- sections.append(life_list_summary)
828
- return "\n\n".join(sections)
836
+ structured_page["footer"] = life_list_summary
837
+ return structured_page
829
838
 
830
839
  def taxon_obs_url(query_response, taxon):
831
840
  obs_args = query_response.obs_args()
@@ -844,9 +853,37 @@ class LifeListFormatter(ListFormatter):
844
853
  # requested page. Normally the user advances a page at a time, so this
845
854
  # formats and caches new pages efficiently for for the normal case,
846
855
  # while still supporting random access.
847
- for new_page in range(last_cached_page + 1, page + 1):
848
- self.pages.append(make_page(new_page))
849
- return self.pages[page]
856
+ for new_page in range(last_cached_page, page):
857
+ self.pages.append(make_structured_page(new_page + 1))
858
+
859
+ # Return string built up from structured page parts:
860
+ structured_page = self.pages[page]
861
+ sections = []
862
+ if structured_page["header"]:
863
+ sections.append(structured_page["header"])
864
+ if structured_page["entries_header"]:
865
+ sections.append(structured_page["entries_header"])
866
+ if structured_page["entries"]:
867
+ entries = []
868
+ for (index, entry) in enumerate(structured_page["entries"]):
869
+ _i = f"**`{str(index + 1).zfill(2)}) `**" if self.with_index else ""
870
+ if selected == index:
871
+ _s = ">"
872
+ _n = "**__"
873
+ _e = "__**"
874
+ else:
875
+ _s = "\N{EN SPACE}"
876
+ _n = ""
877
+ _e = ""
878
+ entries.append(
879
+ f"{_i}`{entry['count']}{entry['direct']}`"
880
+ f"{_s}{entry['indent']}{_n}{entry['name']}{_e}"
881
+ )
882
+ sections.append("\n".join(entries))
883
+ if structured_page["footer"]:
884
+ sections.append(structured_page["footer"])
885
+
886
+ return "\n\n".join(sections)
850
887
 
851
888
  def generate_pages(self):
852
889
  for page in range(0, self.last_page() + 1):
@@ -854,7 +891,7 @@ class LifeListFormatter(ListFormatter):
854
891
  yield formatted_page
855
892
 
856
893
  def get_first_page(self):
857
- first_page = self.format_page(0)
894
+ first_page = self.format_page(0, 0)
858
895
  return first_page
859
896
 
860
897
  def get_page_of_taxa(self, page: int):
@@ -44,18 +44,14 @@ STATIC_URL_PAT = (
44
44
  PAT_OBS_LINK = re.compile(
45
45
  r"\b(?P<url>" + WWW_URL_PAT + r"/observations/(?P<obs_id>\d+))\b", re.I
46
46
  )
47
- # Match observation URL from `obs` embed generated for observations matching a
48
- # specific taxon_id and filtered by optional place_id and/or user_id.
49
- PAT_OBS_TAXON_LINK = re.compile(
50
- r"\b(?P<url>" + WWW_URL_PAT + r"/observations"
51
- r"\?taxon_id=(?P<taxon_id>\d+)(&place_id=(?P<place_id>\d+))?(&user_id=(?P<user_id>\d+))?)\b",
52
- re.I,
53
- )
54
47
 
55
- QUERY_PAT = r"\??(?:&?[^=&]*=[^=&]*)*"
56
- PAT_OBS_QUERY = re.compile(
57
- r"(?P<url>" + WWW_URL_PAT + r"/observations" + QUERY_PAT + ")"
58
- )
48
+ QUERY_PAT = r"\??(?:&?[^\n=&]*=[^\n=&]*)*"
49
+ _OBS_QUERY_PAT = r"(?P<url>" + WWW_URL_PAT + r"/observations" + QUERY_PAT + ")"
50
+ PAT_OBS_QUERY = re.compile(_OBS_QUERY_PAT)
51
+ # - the ">" character is markdown for block-quote, but only at the start of a
52
+ # line, so in the middle of a line we use it to indicate a selected entry
53
+ _SELECTED_OBS_LINK_PAT = r"^(?!> ).*?>.*?\[.*?\]\(" + _OBS_QUERY_PAT + r"\)"
54
+ PAT_SELECTED_OBS_LINK = re.compile(_SELECTED_OBS_LINK_PAT, re.MULTILINE)
59
55
  MARKDOWN_LINK = re.compile(r"\[.*?\]\((?P<url>.*?)\)")
60
56
 
61
57
  # Match place link from any partner site.
@@ -6,7 +6,7 @@ max-line-length = 100
6
6
 
7
7
  [tool.poetry]
8
8
  name = "dronefly-core"
9
- version = "0.3.6.dev7"
9
+ version = "0.3.6.dev9"
10
10
  description = "Core dronefly components"
11
11
  authors = ["Ben Armstrong <synrg@debian.org>"]
12
12
  license = "AGPL-3.0-or-later"
@@ -21,7 +21,7 @@ pyinaturalist = ">=0.19.0.dev3,<0.20"
21
21
  dateparser = "^1.1.1"
22
22
  inflect = "^5.3.0"
23
23
  html2markdown = "^0.1.7"
24
- rich = "13.4.2"
24
+ rich = ">=13.4"
25
25
 
26
26
  [tool.poetry.dev-dependencies]
27
27
  black = "^22.12.0"
@@ -1,41 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- from setuptools import setup
3
-
4
- packages = \
5
- ['core',
6
- 'core.clients',
7
- 'core.commands',
8
- 'core.formatters',
9
- 'core.models',
10
- 'core.parsers',
11
- 'core.query',
12
- 'core.utils']
13
-
14
- package_data = \
15
- {'': ['*']}
16
-
17
- install_requires = \
18
- ['dateparser>=1.1.1,<2.0.0',
19
- 'html2markdown>=0.1.7,<0.2.0',
20
- 'inflect>=5.3.0,<6.0.0',
21
- 'pyinaturalist>=0.19.0.dev3,<0.20',
22
- 'rich==13.4.2']
23
-
24
- setup_kwargs = {
25
- 'name': 'dronefly-core',
26
- 'version': '0.3.6.dev7',
27
- 'description': 'Core dronefly components',
28
- 'long_description': "# Dronefly core\n\nThis is an incomplete rewrite of [Dronefly](https://dronefly.readthedocs.io)\nDiscord bot's core components. We're not yet making version guarantees until\nit is more usable.\n\n# Related packages\n\n## Dronefly command-line client\n\nThe [dronefly-cli](https://github.com/dronefly-garden/dronefly-cli) command-line\nclient will provide a standalone text user interface that can perform a usable\nsubset of Dronefly Discord bot's capabilities, built solely with Dronefly core.\n\n## Dronefly Discord bot\n\nDronefly Discord bot brings [iNaturalist](https://www.inaturalist.org) taxa,\nobservations, and other data from the site into conversations on the\n[Discord](https://discord.com) chat platform.\n",
29
- 'author': 'Ben Armstrong',
30
- 'author_email': 'synrg@debian.org',
31
- 'maintainer': 'None',
32
- 'maintainer_email': 'None',
33
- 'url': 'None',
34
- 'packages': packages,
35
- 'package_data': package_data,
36
- 'install_requires': install_requires,
37
- 'python_requires': '>=3.8.1,<3.12',
38
- }
39
-
40
-
41
- setup(**setup_kwargs)