Skip to content

Commit

Permalink
Merge pull request #11 from hugovk/add-bpo
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored May 9, 2022
2 parents 6f81aa3 + 623c2ee commit 28c8666
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 36 deletions.
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
version: 2
updates:
- package-ecosystem: pip
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: monthly
time: "03:00"
open-pull-requests-limit: 10
labels:
- "changelog: skip"
- dependencies
- package-ecosystem: github-actions
- "dependencies"
- package-ecosystem: pip
directory: "/"
schedule:
interval: monthly
time: "03:00"
open-pull-requests-limit: 10
labels:
- dependencies
- "changelog: skip"
- "dependencies"
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,30 @@ https://pep-previews--2440.org.readthedocs.build

<!-- [[[end]]] -->

### Open a BPO issue in the browser

Issues from [bugs.python.org](https://bugs.python.org/) have been migrated to
[GitHub issues](https://github.com/python/cpython/issues) and have new numbers. This
command will open the redirect page to take you to the new issue.

<!-- [[[cog run("bpo 46208") ]]] -->

```console
$ bpo 46208
https://bugs.python.org/issue?@action=redirect&bpo=46208
```

<!-- [[[end]]] -->

This redirects to https://github.com/python/cpython/issues/90366

### Help

<!-- [[[cog run("pep --help") ]]] -->

```console
$ pep --help
usage: pep [-h] [-u URL] [-p PR] [-n] [--clear-cache] [-v] [-V] [search ...]
usage: pep [-h] [-u URL] [-p PR] [--clear-cache] [-n] [-v] [-V] [search ...]

pepotron: CLI to open PEPs in your browser

Expand All @@ -128,8 +145,8 @@ options:
-h, --help show this help message and exit
-u URL, --url URL Base URL for PEPs (default: https://peps.python.org)
-p PR, --pr PR Open preview for python/peps PR
-n, --dry-run Don't open in browser
--clear-cache Clear cache before running
-n, --dry-run Don't open in browser
-v, --verbose Verbose logging
-V, --version show program's version number and exit
```
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ classifiers =
Programming Language :: Python :: Implementation :: PyPy
keywords =
pep
bpo
cli
project_urls =
Source=https://github.com/hugovk/pepotron
Expand All @@ -49,6 +50,7 @@ where = src
[options.entry_points]
console_scripts =
pep = pepotron.cli:main
bpo = pepotron.cli:bpo

[options.extras_require]
tests =
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup


def local_scheme(version) -> str:
def local_scheme(version: str) -> str:
"""Skip the local version (e.g. +xyz of 0.6.1.dev4+gdf99fe2)
to be able to upload to Test PyPI"""
return ""
Expand Down
25 changes: 19 additions & 6 deletions src/pepotron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _download_peps_json() -> Path:

_cache.save(cache_file, res)

logging.info("")
return cache_file


Expand Down Expand Up @@ -96,7 +97,7 @@ def word_search(search: str | None) -> int:
return int(result[0][0][0])


def url(search: str | None, base_url: str = BASE_URL, pr: int | None = None) -> str:
def pep_url(search: str | None, base_url: str = BASE_URL, pr: int | None = None) -> str:
"""Get PEP URL"""
if pr:
base_url = f"https://pep-previews--{pr}.org.readthedocs.build"
Expand All @@ -120,13 +121,25 @@ def url(search: str | None, base_url: str = BASE_URL, pr: int | None = None) ->
return result


def pep(
def open_pep(
search: str, base_url: str = BASE_URL, pr: int | None = None, dry_run: bool = False
) -> None:
) -> str:
"""Open this PEP in the browser"""
pep_url = url(search, base_url, pr)
url = pep_url(search, base_url, pr)
if not dry_run:
import webbrowser

webbrowser.open_new_tab(pep_url)
print(pep_url)
webbrowser.open_new_tab(url)
print(url)
return url


def open_bpo(number: int, dry_run: bool = False) -> str:
"""Open this BPO in the browser"""
url = f"https://bugs.python.org/issue?@action=redirect&bpo={number}"
if not dry_run:
import webbrowser

webbrowser.open_new_tab(url)
print(url)
return url
54 changes: 36 additions & 18 deletions src/pepotron/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@
import atexit
import logging

from pepotron import BASE_URL, __version__, _cache, pep
from pepotron import BASE_URL, __version__, _cache, open_bpo, open_pep

atexit.register(_cache.clear)


def add_common_arguments(parser):
parser.add_argument(
"-n", "--dry-run", action="store_true", help="Don't open in browser"
)
parser.add_argument(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
default=logging.WARNING,
help="Verbose logging",
)
parser.add_argument(
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
)
return parser


def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand All @@ -23,33 +42,32 @@ def main() -> None:
"-u", "--url", default=BASE_URL, help=f"Base URL for PEPs (default: {BASE_URL})"
)
parser.add_argument("-p", "--pr", type=int, help="Open preview for python/peps PR")
parser.add_argument(
"-n", "--dry-run", action="store_true", help="Don't open in browser"
)
parser.add_argument(
"--clear-cache", action="store_true", help="Clear cache before running"
)
parser.add_argument(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
default=logging.WARNING,
help="Verbose logging",
)
parser.add_argument(
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
)
parser = add_common_arguments(parser)
args = parser.parse_args()

logging.basicConfig(level=args.loglevel)
logging.basicConfig(level=args.loglevel, format="%(message)s")
if args.search:
args.search = " ".join(args.search)
if args.clear_cache:
_cache.clear(clear_all=True)

pep(search=args.search, base_url=args.url, pr=args.pr, dry_run=args.dry_run)
open_pep(search=args.search, base_url=args.url, pr=args.pr, dry_run=args.dry_run)


def bpo() -> None:
parser = argparse.ArgumentParser(
description="Open this BPO in the browser",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("bpo", type=int, help="BPO number")
parser = add_common_arguments(parser)
args = parser.parse_args()

logging.basicConfig(level=args.loglevel, format="%(message)s")
open_bpo(number=args.bpo, dry_run=args.dry_run)


if __name__ == "__main__":
Expand Down
14 changes: 10 additions & 4 deletions tests/test_pepotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
def test_url(search: str, expected_url: str) -> None:
# Act
pep_url = pepotron.url(search)
pep_url = pepotron.pep_url(search)
# Assert
assert pep_url == expected_url

Expand Down Expand Up @@ -50,7 +50,7 @@ def test_url(search: str, expected_url: str) -> None:
)
def test_url_base_url(search: str, base_url: str, expected_url: str) -> None:
# Act
pep_url = pepotron.url(search, base_url)
pep_url = pepotron.pep_url(search, base_url)
# Assert
assert pep_url == expected_url

Expand All @@ -66,10 +66,16 @@ def test_url_pr(search, expected_url) -> None:
# Arrange
pr = 2440
# Act
pep_url = pepotron.url(search, pr=pr)
pep_url = pepotron.pep_url(search, pr=pr)
# Assert
assert pep_url == expected_url


def test_pep() -> None:
pepotron.pep("8", dry_run=True)
url = pepotron.open_pep("8", dry_run=True)
assert url == "https://peps.python.org/pep-0008/"


def test_open_bpo() -> None:
url = pepotron.open_bpo(38374, dry_run=True)
assert url == "https://bugs.python.org/issue?@action=redirect&bpo=38374"
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ skip_install = true
deps =
pre-commit
commands =
pre-commit run --all-files
pre-commit run --all-files --show-diff-on-failure

0 comments on commit 28c8666

Please sign in to comment.