diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 57b62ee..b366183 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,11 +13,11 @@ repos: - id: check-added-large-files args: [--maxkb=5120] # 2 MB - repo: https://github.com/psf/black - rev: 21.12b0 + rev: 22.10.0 hooks: - id: black name: Formatting - args: ["./oxo"] + args: ["./oxo.py"] - repo: https://github.com/pycqa/isort rev: 5.10.1 hooks: diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5170cdd --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +SHELL := /bin/bash +.PHONY: all venv + +VENV_DIR=$$(pwd)/venv +VENV_PYTHON=$(VENV_DIR)/bin/python +VENV_BIN=$(VENV_DIR)/bin + +venv: + @echo making venv at $(VENV_DIR) + @python -m venv $(VENV_DIR) + @$(VENV_PYTHON) -m pip install --upgrade pip + +install: venv + @$(VENV_PYTHON) -m pip install --upgrade -e '.[dev]' + @$(VENV_PYTHON) -m pre_commit install diff --git a/oxo/cli.py b/oxo.py similarity index 55% rename from oxo/cli.py rename to oxo.py index 2f07802..2435a58 100644 --- a/oxo/cli.py +++ b/oxo.py @@ -1,15 +1,47 @@ +import io import typing as t from pathlib import Path +import httpx import typer -from oxo import __version__, api +try: + from importlib.metadata import version +except ImportError: + from importlib_metadata import version + +__version__ = version("oxo") app = typer.Typer() BASE_URL = typer.Option("https://0x0.st", envvar="OXO_BASE_URL") +def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str: + retval = [] + with httpx.Client() as client: + for d in data: + if isinstance(d.get("file"), io.BufferedReader): + res = client.post(base_url, files=d) + else: + res = client.post(base_url, data=d) + res.raise_for_status() + retval.append(res.text.strip()) + return " ".join(retval) + + +def post_files(base_url, files: t.List[Path]): + return post_to(base_url, data=({"file": f.open("rb")} for f in files)) + + +def post_repost(base_url, urls: t.List[str]): + return post_to(base_url, data=({"url": u.strip()} for u in urls)) + + +def post_shorten(base_url, urls: t.List[str]): + return post_to(base_url, data=({"shorten": u.strip()} for u in urls)) + + def version_callback(value: bool): if value: typer.echo(f"{__version__}") @@ -41,13 +73,13 @@ def files( base_url: str = BASE_URL, ): """Upload one or more files.""" - typer.echo(api.files(base_url, files)) + typer.echo(post_files(base_url, files)) @app.command() def repost(urls: t.List[str] = typer.Argument(..., min=1), base_url=BASE_URL): """Repost one or more urls.""" - typer.echo(api.repost(base_url, urls)) + typer.echo(post_repost(base_url, urls)) @app.command() @@ -60,4 +92,4 @@ def shorten(urls: t.List[str] = typer.Argument(..., min=1), base_url=BASE_URL): fg=typer.colors.RED, err=True, ) - typer.echo(api.shorten(base_url, urls)) + typer.echo(post_shorten(base_url, urls)) diff --git a/oxo/__init__.py b/oxo/__init__.py deleted file mode 100644 index faa326f..0000000 --- a/oxo/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from oxo._version import __version__ diff --git a/oxo/api.py b/oxo/api.py deleted file mode 100644 index ffe80d0..0000000 --- a/oxo/api.py +++ /dev/null @@ -1,30 +0,0 @@ -import io -import typing as t -from pathlib import Path - -import httpx - - -def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str: - retval = [] - with httpx.Client() as client: - for d in data: - if isinstance(d.get("file"), io.BufferedReader): - res = client.post(base_url, files=d) - else: - res = client.post(base_url, data=d) - res.raise_for_status() - retval.append(res.text.strip()) - return " ".join(retval) - - -def files(base_url, files: t.List[Path]): - return post_to(base_url, data=({"file": f.open("rb")} for f in files)) - - -def repost(base_url, urls: t.List[str]): - return post_to(base_url, data=({"url": u.strip()} for u in urls)) - - -def shorten(base_url, urls: t.List[str]): - return post_to(base_url, data=({"shorten": u.strip()} for u in urls)) diff --git a/pyproject.toml b/pyproject.toml index 329e042..386090d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,23 @@ [build-system] -requires = ["setuptools", "wheel", "versioningit~=1.0.0"] +requires = ["setuptools>=61.0.0", "wheel", "versioningit>=2.1.0"] build-backend = "setuptools.build_meta" +[project] +name = "oxo" +description = "A python interface to 0x0.st and mirrors" +requires-python = ">=3.7" +dynamic = ["version"] +authors=[{name="grym", email="grym@ctrl-c.club"}] +dependencies = ["typer[all]", + "httpx", + 'importlib-metadata; python_version<"3.8"'] + +[project.optional-dependencies] +dev = ["pre-commit","pytest", "ipython"] + +[project.scripts] +oxo = "oxo:app" + [tool.versioningit] default-version = "0+unknown" @@ -10,30 +26,3 @@ default-version = "0+unknown" distance = "{version}+{distance}.{vcs}{rev}" dirty = "{version}+{distance}.{vcs}{rev}.dirty" distance-dirty = "{version}+{distance}.{vcs}{rev}.dirty" - -[tool.versioningit.write] -file = "oxo/_version.py" - -[tool.isort] -skip='venv' -profile='black' - -[tool.black] -target-version = ['py36','py37', 'py38'] -exclude = ''' -( - /( - \.eggs # exclude a few common directories in the - | \.git # root of the project - | \.mypy_cache - | \.tox - | \.venv - | \.env - | \venv - | _build - | buck-out - | build - | dist - )/ -) -''' diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 1d4dae6..0000000 --- a/setup.cfg +++ /dev/null @@ -1,27 +0,0 @@ -[metadata] -name=oxo -description="A python interface to 0x0.st" -author= "grym" -author_email = "grym@ctrl-c.club" -url="https://tildegit.org/grym/oxo" - -[options] -python_requires = >=3.8 -packages=find: -install_requires= - httpx>=0.16.1 - typer[all] - -[options.entry_points] -console_scripts = - oxo = oxo.cli:app - -[options.extras_require] -dev = - black>=20.8b1 - coverage>=4.5.4 - ipython - isort>=4.3.21 - pre-commit - pudb - pytest diff --git a/tests/test_oxo.py b/tests/test_oxo.py index 6c6d4cd..a1be491 100644 --- a/tests/test_oxo.py +++ b/tests/test_oxo.py @@ -1,7 +1,6 @@ from typer.testing import CliRunner -from oxo import __version__ -from oxo.cli import app +from oxo import __version__, app runner = CliRunner()