Skip to content

Commit

Permalink
Rewrite the CLI using click
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 6, 2024
1 parent d0cd4f3 commit 1b86f8a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 55 deletions.
4 changes: 1 addition & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ The name, token, and URL are taken from your Traduire installation. The path is
the folder where your local checkout of the project resides. The `trd` client
uses the configured path to automatically find the correct settings. Right now
there's no support forspecifying the necessary token and URL parameters
directly. (Contributions would be very welcome for this! I think the CLI should
be rewritten to use [Click](https://click.palletsprojects.com/) under the
hood.)
directly. (Contributions would be very welcome for this!)

Then, assuming you have your gettext `.po` files inside `project/locale` you
you should first ensure that they are up-to-date:
Expand Down
2 changes: 1 addition & 1 deletion cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[build-system]
build-backend = "setuptools.build_meta"

requires = [
"setuptools>=61",
]
Expand All @@ -20,6 +19,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dependencies = [
"click",
"requests",
]
urls."Bug Tracker" = "https://github.com/matthiask/traduire/issues"
Expand Down
122 changes: 71 additions & 51 deletions cli/trd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,82 @@
from pathlib import Path
from urllib.parse import urljoin

import click
import requests
import tomllib


def _terminate(msg):
print(msg, file=sys.stderr)
sys.exit(1)
def _session(project):
session = requests.Session()
session.headers = {
"x-token": project["token"],
"x-cli-version": _version(),
}
return session


@click.group()
def cli():
pass


@click.command()
@click.argument("folder", type=click.Path(exists=True))
def get(folder):
"""Fetch all pofiles from the server"""
project = current_project()
session = _session(project)
pofiles = find_pofiles(folder)
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.get(url, timeout=10)
if r.ok:
pofile.write_text(r.content.decode("utf-8"))
click.echo(f"Updated {pofile}")
else:
_terminate(r.text)


@click.command()
@click.argument("folder", type=click.Path(exists=True))
def submit(folder):
"""Submit updated pofiles to the server for translation"""
project = current_project()
session = _session(project)
pofiles = find_pofiles(sys.argv[2])
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.post(url, data=pofile.read_bytes())
if r.ok:
click.echo(f"Submitted {pofile} to the server for translation")
else:
_terminate(r.text)


@click.command()
@click.argument("folder", type=click.Path(exists=True))
def replace(folder):
"""Replace pofiles on the server"""
project = current_project()
session = _session(project)
pofiles = find_pofiles(sys.argv[2])
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.put(url, data=pofile.read_bytes())
if r.ok:
click.echo(f"Replaced {pofile} on the server")
else:
_terminate(r.text)


def _progress(msg):
print(msg)
cli.add_command(get)
cli.add_command(submit)
cli.add_command(replace)


def _terminate(msg):
click.echo(msg, file=sys.stderr)
sys.exit(1)


def _version():
Expand Down Expand Up @@ -53,50 +118,5 @@ def url_from_pofile(project, pofile):
)


def main():
if len(sys.argv) != 3 or sys.argv[1] not in {"get", "submit", "replace"}:
_terminate(
f"traduire-cli {_version()}\nUsage: {Path(sys.argv[0]).name} [get,submit,replace] path"
)

project = current_project()
session = requests.Session()
session.headers = {
"x-token": project["token"],
"x-cli-version": _version(),
}

if sys.argv[1] == "get":
pofiles = find_pofiles(sys.argv[2])
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.get(url, timeout=10)
if r.ok:
pofile.write_text(r.content.decode("utf-8"))
_progress(f"Updated {pofile}")
else:
_terminate(r.text)

elif sys.argv[1] in "submit":
pofiles = find_pofiles(sys.argv[2])
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.post(url, data=pofile.read_bytes())
if r.ok:
_progress(f"Submitted {pofile} to the server for translation")
else:
_terminate(r.text)

elif sys.argv[1] in "replace":
pofiles = find_pofiles(sys.argv[2])
for pofile in pofiles:
url = url_from_pofile(project, pofile)
r = session.put(url, data=pofile.read_bytes())
if r.ok:
_progress(f"Replaced {pofile} on the server")
else:
_terminate(r.text)


if __name__ == "__main__":
main()
cli()

0 comments on commit 1b86f8a

Please sign in to comment.