Просмотр исходного кода

feat: add support for command line completion

tags/v1.0.0rc1
Jérôme Deuchnord 3 месяцев назад
committed by Deuchnord
Родитель
Сommit
151c3a7a5f
7 измененных файлов: 98 добавлений и 24 удалений
  1. +24
    -3
      kosmorro/__main__.py
  2. +27
    -19
      kosmorro/locales/messages.pot
  3. +3
    -0
      manpage/kosmorro.1.md
  4. +17
    -2
      poetry.lock
  5. +1
    -0
      pyproject.toml
  6. +20
    -0
      tests/completion.py
  7. +6
    -0
      tests/general.py

+ 24
- 3
kosmorro/__main__.py Просмотреть файл

@@ -17,7 +17,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.


import argparse import argparse
import datetime
import argcomplete
import sys import sys
import os.path import os.path


@@ -27,8 +27,6 @@ from kosmorrolib import Position, get_ephemerides, get_events, get_moon_phase
from kosmorrolib.exceptions import OutOfRangeDateError from kosmorrolib.exceptions import OutOfRangeDateError
from datetime import date from datetime import date


from pytz import timezone

from . import dumper, environment, debug from . import dumper, environment, debug
from .date import parse_date from .date import parse_date
from .geolocation import get_position from .geolocation import get_position
@@ -57,6 +55,9 @@ def run():


set_colors_activated(args.colors) set_colors_activated(args.colors)


if args.completion is not None:
return 0 if output_completion(args.completion) else 1

if args.special_action is not None: if args.special_action is not None:
return 0 if args.special_action() else 1 return 0 if args.special_action() else 1


@@ -352,8 +353,28 @@ def get_args(output_formats: [str]):
help=_("Show debugging messages"), help=_("Show debugging messages"),
) )


argcomplete.autocomplete(parser)

parser.add_argument(
"--completion",
type=str,
help=_("Print a script allowing completion for your shell"),
)

return parser.parse_args() return parser.parse_args()




def output_completion(shell: str) -> bool:
shellcode = argcomplete.shellcode([sys.argv[0]], shell=shell)
if shellcode == "":
print_stderr(
colored(_("No completion script available for this shell."), "red")
)
return False

print(shellcode)
return True


def main(): def main():
sys.exit(run()) sys.exit(run())

+ 27
- 19
kosmorro/locales/messages.pot Просмотреть файл

@@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PROJECT VERSION\n" "Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2025-09-22 17:24+0200\n"
"POT-Creation-Date: 2025-10-05 10:30+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,80 +17,80 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.17.0\n" "Generated-By: Babel 2.17.0\n"


#: kosmorro/__main__.py:89
#: kosmorro/__main__.py:90
msgid "" msgid ""
"Output file will not contain the ephemerides, because you didn't provide " "Output file will not contain the ephemerides, because you didn't provide "
"the observation coordinates." "the observation coordinates."
msgstr "" msgstr ""


#: kosmorro/__main__.py:107
#: kosmorro/__main__.py:108
msgid "" msgid ""
"Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, " "Environment variable KOSMORRO_TIMEZONE is deprecated. Use TZ instead, "
"which is more standard." "which is more standard."
msgstr "" msgstr ""


#: kosmorro/__main__.py:116
#: kosmorro/__main__.py:117
#, python-brace-format #, python-brace-format
msgid "Unknown timezone: {timezone}" msgid "Unknown timezone: {timezone}"
msgstr "" msgstr ""


#: kosmorro/__main__.py:159
#: kosmorro/__main__.py:160
#, python-brace-format #, python-brace-format
msgid "The file could not be saved in \"{path}\": {error}" msgid "The file could not be saved in \"{path}\": {error}"
msgstr "" msgstr ""


#: kosmorro/__main__.py:173
#: kosmorro/__main__.py:174
msgid "Please provide a file path to export in this format (--output)." msgid "Please provide a file path to export in this format (--output)."
msgstr "" msgstr ""


#: kosmorro/__main__.py:205
#: kosmorro/__main__.py:206
#, python-brace-format #, python-brace-format
msgid "Moon phase can only be computed between {min_date} and {max_date}" msgid "Moon phase can only be computed between {min_date} and {max_date}"
msgstr "" msgstr ""


#: kosmorro/__main__.py:252
#: kosmorro/__main__.py:253
#, python-brace-format #, python-brace-format
msgid "Running on Python {python_version} with Kosmorrolib v{kosmorrolib_version}" msgid "Running on Python {python_version} with Kosmorrolib v{kosmorrolib_version}"
msgstr "" msgstr ""


#: kosmorro/__main__.py:265
#: kosmorro/__main__.py:266
msgid "" msgid ""
"Compute the ephemerides and the events for a given date and a given " "Compute the ephemerides and the events for a given date and a given "
"position on Earth." "position on Earth."
msgstr "" msgstr ""


#: kosmorro/__main__.py:268
#: kosmorro/__main__.py:269
msgid "" msgid ""
"By default, only the events will be computed for today.\n" "By default, only the events will be computed for today.\n"
"To compute also the ephemerides, latitude and longitude arguments are " "To compute also the ephemerides, latitude and longitude arguments are "
"needed." "needed."
msgstr "" msgstr ""


#: kosmorro/__main__.py:280
#: kosmorro/__main__.py:281
msgid "Show the program version" msgid "Show the program version"
msgstr "" msgstr ""


#: kosmorro/__main__.py:289
#: kosmorro/__main__.py:290
msgid "" msgid ""
"The format to output the information to. If not provided, the output " "The format to output the information to. If not provided, the output "
"format will be inferred from the file extension of the output file." "format will be inferred from the file extension of the output file."
msgstr "" msgstr ""


#: kosmorro/__main__.py:299
#: kosmorro/__main__.py:300
#, python-brace-format #, python-brace-format
msgid "" msgid ""
"The observer's position on Earth, in the \"{latitude},{longitude}\" " "The observer's position on Earth, in the \"{latitude},{longitude}\" "
"format. Can also be set in the KOSMORRO_POSITION environment variable." "format. Can also be set in the KOSMORRO_POSITION environment variable."
msgstr "" msgstr ""


#: kosmorro/__main__.py:309
#: kosmorro/__main__.py:310
msgid "" msgid ""
"The date for which the ephemerides must be calculated. Can be in the " "The date for which the ephemerides must be calculated. Can be in the "
"YYYY-MM-DD format or an interval in the \"[+-]YyMmDd\" format (with Y, M," "YYYY-MM-DD format or an interval in the \"[+-]YyMmDd\" format (with Y, M,"
" and D numbers). Defaults to current date." " and D numbers). Defaults to current date."
msgstr "" msgstr ""


#: kosmorro/__main__.py:320
#: kosmorro/__main__.py:321
msgid "" msgid ""
"The timezone to use to display the hours. It can be either a number (e.g." "The timezone to use to display the hours. It can be either a number (e.g."
" 1 for UTC+1) or a timezone name (e.g. Europe/Paris). See " " 1 for UTC+1) or a timezone name (e.g. Europe/Paris). See "
@@ -98,24 +98,32 @@ msgid ""
" timezone. Can also be set in the TZ environment variable." " timezone. Can also be set in the TZ environment variable."
msgstr "" msgstr ""


#: kosmorro/__main__.py:329
#: kosmorro/__main__.py:330
msgid "Disable the colors in the console." msgid "Disable the colors in the console."
msgstr "" msgstr ""


#: kosmorro/__main__.py:337
#: kosmorro/__main__.py:338
msgid "A file to export the output to. If not given, the standard output is used." msgid "A file to export the output to. If not given, the standard output is used."
msgstr "" msgstr ""


#: kosmorro/__main__.py:345
#: kosmorro/__main__.py:346
msgid "" msgid ""
"Do not generate a graph to represent the rise and set times in the LaTeX " "Do not generate a graph to represent the rise and set times in the LaTeX "
"file." "file."
msgstr "" msgstr ""


#: kosmorro/__main__.py:352
#: kosmorro/__main__.py:353
msgid "Show debugging messages" msgid "Show debugging messages"
msgstr "" msgstr ""


#: kosmorro/__main__.py:361
msgid "Print a script allowing completion for your shell"
msgstr ""

#: kosmorro/__main__.py:371
msgid "No completion script available for this shell."
msgstr ""

#: kosmorro/date.py:17 #: kosmorro/date.py:17
#, python-brace-format #, python-brace-format
msgid "The date {date} is not valid: {error}" msgid "The date {date} is not valid: {error}"


+ 3
- 0
manpage/kosmorro.1.md Просмотреть файл

@@ -39,6 +39,9 @@
`--no-graph` `--no-graph`
present the ephemerides in a table instead of a graph; LaTeX output format only present the ephemerides in a table instead of a graph; LaTeX output format only


`--completion [SHELL]`
generate completion scripts for the specified shell (bash, zsh, fish, powershell)

## ENVIRONMENT VARIABLES ## ENVIRONMENT VARIABLES


The environment variable listed below may be used instead of the options. The environment variable listed below may be used instead of the options.


+ 17
- 2
poetry.lock Просмотреть файл

@@ -1,4 +1,19 @@
# This file is automatically @generated by Poetry 2.2.0 and should not be changed by hand.
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.

[[package]]
name = "argcomplete"
version = "3.6.2"
description = "Bash tab completion for argparse"
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = [
{file = "argcomplete-3.6.2-py3-none-any.whl", hash = "sha256:65b3133a29ad53fb42c48cf5114752c7ab66c1c38544fdf6460f450c09b42591"},
{file = "argcomplete-3.6.2.tar.gz", hash = "sha256:d0519b1bc867f5f4f4713c41ad0aba73a4a5f007449716b16f385f2166dc6adf"},
]

[package.extras]
test = ["coverage", "mypy", "pexpect", "ruff", "wheel"]


[[package]] [[package]]
name = "aurornis" name = "aurornis"
@@ -541,4 +556,4 @@ tests = ["pytest", "pytest-cov"]
[metadata] [metadata]
lock-version = "2.1" lock-version = "2.1"
python-versions = "^3.12" python-versions = "^3.12"
content-hash = "12bf9cdda9cfaf9616bf3d899450e105991f788267601a41a79fed1b5cbd73a9"
content-hash = "a020653a41707f2ae483abe05ce06b55d09c8a3ddbe4b4c4fc60c9ccd34380ed"

+ 1
- 0
pyproject.toml Просмотреть файл

@@ -23,6 +23,7 @@ python-dateutil = "^2.8"
Babel = "^2.9" Babel = "^2.9"
openlocationcode = "^1.0" openlocationcode = "^1.0"
pytz = "^2025.2" pytz = "^2025.2"
argcomplete = "^3.6.2"


[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
black = "^24.4" black = "^24.4"


+ 20
- 0
tests/completion.py Просмотреть файл

@@ -0,0 +1,20 @@
#!/usr/bin/env python3

from .utils import execute


def test_completion_script_is_generated():
for shell in ["bash", "zsh", "fish", "powershell"]:
result = execute(["kosmorro", f"--completion={shell}"])

assert result.successful
assert result.stdout != ""
assert result.stderr == ""


def test_completion_script_returns_error_for_unsupported_shell():
result = execute(["kosmorro", "--completion=deuchshell"])

assert not result.successful
assert result.stderr == "No completion script available for this shell.\n"
assert result.stdout == ""

+ 6
- 0
tests/general.py Просмотреть файл

@@ -42,6 +42,7 @@ def test_help_message():
== """usage: kosmorro [-h] [--version] [--format {txt,json,tex}] == """usage: kosmorro [-h] [--version] [--format {txt,json,tex}]
[--position POSITION] [--date DATE] [--timezone TIMEZONE] [--position POSITION] [--date DATE] [--timezone TIMEZONE]
[--no-colors] [--output OUTPUT] [--no-graph] [--debug] [--no-colors] [--output OUTPUT] [--no-graph] [--debug]
[--completion COMPLETION]


Compute the ephemerides and the events for a given date and a given position Compute the ephemerides and the events for a given date and a given position
on Earth. on Earth.
@@ -74,6 +75,8 @@ options:
--no-graph Do not generate a graph to represent the rise and set --no-graph Do not generate a graph to represent the rise and set
times in the LaTeX file. times in the LaTeX file.
--debug Show debugging messages --debug Show debugging messages
--completion COMPLETION
Print a script allowing completion for your shell


By default, only the events will be computed for today. To compute also the By default, only the events will be computed for today. To compute also the
ephemerides, latitude and longitude arguments are needed. ephemerides, latitude and longitude arguments are needed.
@@ -85,6 +88,7 @@ ephemerides, latitude and longitude arguments are needed.
== """usage: kosmorro [-h] [--version] [--format {txt,json,tex}] == """usage: kosmorro [-h] [--version] [--format {txt,json,tex}]
[--position POSITION] [--date DATE] [--timezone TIMEZONE] [--position POSITION] [--date DATE] [--timezone TIMEZONE]
[--no-colors] [--output OUTPUT] [--no-graph] [--debug] [--no-colors] [--output OUTPUT] [--no-graph] [--debug]
[--completion COMPLETION]


Compute the ephemerides and the events for a given date and a given position Compute the ephemerides and the events for a given date and a given position
on Earth. on Earth.
@@ -116,6 +120,8 @@ options:
--no-graph Do not generate a graph to represent the rise and set --no-graph Do not generate a graph to represent the rise and set
times in the LaTeX file. times in the LaTeX file.
--debug Show debugging messages --debug Show debugging messages
--completion COMPLETION
Print a script allowing completion for your shell


By default, only the events will be computed for today. To compute also the By default, only the events will be computed for today. To compute also the
ephemerides, latitude and longitude arguments are needed. ephemerides, latitude and longitude arguments are needed.


Загрузка…
Отмена
Сохранить