Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flake8 tests and some config files #85

Merged
merged 5 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
global-exclude *.py[cdo] __pycache__ *.so *.pyd
include LICENSE
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
click-repl
===

[![build](https://travis-ci.org/click-contrib/click-repl.svg?branch=master)](https://travis-ci.org/click-contrib/click-repl)
[![Tests](https://github.com/GhostOps77/click-repl/actions/workflows/tests.yml/badge.svg?branch=GhostOps77-patch-1)](https://github.com/GhostOps77/click-repl/actions/runs/4074019555/jobs/7018648133)
[![License](https://img.shields.io/pypi/l/click-repl?label=License)](https://github.com/GhostOps77/click-repl/blob/GhostOps77-patch-1/LICENSE)
![Python - version](https://img.shields.io/badge/python-3.6%20%7C%203.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue)
[![PyPi - version](https://img.shields.io/badge/pypi-v0.2.0-blue)](https://pypi.org/project/click-repl/)
![wheels](https://img.shields.io/piwheels/v/click-repl?label=wheel)
![PyPI - Status](https://img.shields.io/pypi/status/click)
![PyPI - Downloads](https://img.shields.io/pypi/dm/click-repl)

Installation
===

Installation is done via pip:
```
pip install click-repl
```
Usage
===

In your [click](http://click.pocoo.org/) app:

```py
import click
from click_repl import register_repl

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

@cli.command()
def hello():
click.echo("Hello world!")

register_repl(cli)
cli()
```
In the shell:
```
$ my_app repl
> hello
Hello world!
> ^C
$ echo hello | my_app repl
Hello world!
```
**Features not shown:**

- Tab-completion.
- The parent context is reused, which means `ctx.obj` persists between
subcommands. If you're keeping caches on that object (like I do), using the
app's repl instead of the shell is a huge performance win.
- `!` - prefix executes shell commands.

You can use the internal `:help` command to explain usage.

Advanced Usage
===

For more flexibility over how your REPL works you can use the `repl` function
directly instead of `register_repl`. For example, in your app:

```py
import click
from click_repl import repl
from prompt_toolkit.history import FileHistory

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

@cli.command()
def myrepl():
prompt_kwargs = {
'history': FileHistory('/etc/myrepl/myrepl-history'),
}
repl(click.get_current_context(), prompt_kwargs=prompt_kwargs)

cli()
```
And then your custom `myrepl` command will be available on your CLI, which
will start a REPL which has its history stored in
`/etc/myrepl/myrepl-history` and persist between sessions.

Any arguments that can be passed to the [`python-prompt-toolkit`](https://github.com/prompt-toolkit/python-prompt-toolkit) [Prompt](http://python-prompt-toolkit.readthedocs.io/en/stable/pages/reference.html?prompt_toolkit.shortcuts.Prompt#prompt_toolkit.shortcuts.Prompt) class
can be passed in the `prompt_kwargs` argument and will be used when
instantiating your `Prompt`.
98 changes: 0 additions & 98 deletions README.rst

This file was deleted.

12 changes: 8 additions & 4 deletions click_repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
from .exceptions import InternalCommandException, ExitReplException # noqa

# Handle backwards compatibility between Click 7.0 and 8.0
try:
try:
import click.shell_completion

HAS_C8 = True
except ImportError:
import click._bashcomplete

HAS_C8 = False

# Handle click.exceptions.Exit introduced in Click 7.0
try:
from click.exceptions import Exit as ClickExit
except ImportError:

class ClickExit(RuntimeError):
pass


PY2 = sys.version_info[0] == 2

if PY2:
Expand Down Expand Up @@ -116,9 +120,9 @@ def get_completions(self, document, complete_event=None):
# Resolve context based on click version
if HAS_C8:
ctx = click.shell_completion._resolve_context(self.cli, {}, "", args)
else:
else:
ctx = click._bashcomplete.resolve_ctx(self.cli, "", args)

if ctx is None:
return

Expand Down Expand Up @@ -164,7 +168,7 @@ def bootstrap_prompt(prompt_kwargs, group):
defaults = {
"history": InMemoryHistory(),
"completer": ClickCompleter(group),
"message": u"> ",
"message": "> ",
}

for key in defaults:
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = [
"--cov=click_repl"
Expand Down
47 changes: 47 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[metadata]
name = click-repl
version = attr: click_repl.__version__
description = REPL plugin for Click

url = https://github.com/untitaker/click-repl

author = Markus Unterwaditzer
author_email = markus@unterwaditzer.net
license = MIT

classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11

[options]
packages=
click_repl

install_requires =
click>=7.0
prompt_toolkit>=3.0.36

python_requires = >=3.6
zip_safe = no

[options.extras_require]
testing =
pytest>=7.2.1
pytest-cov>=4.0.0
; mypy>=0.991
tox>=4.4.3

; [options.package_data]
; click_repl = py.typed

[flake8]
ignore = E203, E266, W503, E402, E731
max-line-length = 90
max-complexity = 18
select = B,C,E,F,W,T4,B9
24 changes: 2 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
#!/usr/bin/env python

import ast
import re
from setuptools import setup


_version_re = re.compile(r"__version__\s+=\s+(.*)")


with open("click_repl/__init__.py", "rb") as f:
version = str(
ast.literal_eval(_version_re.search(f.read().decode("utf-8")).group(1))
)


setup(
name="click-repl",
version=version,
description="REPL plugin for Click",
author="Markus Unterwaditzer",
author_email="markus@unterwaditzer.net",
url="https://github.com/untitaker/click-repl",
license="MIT",
packages=["click_repl"],
install_requires=["click", "prompt_toolkit"],
)
if __name__ == '__main__':
setup()
4 changes: 2 additions & 2 deletions tests/test_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def arg_cmd():
pass

c = ClickCompleter(root_command)
completions = list(c.get_completions(Document(u"arg-cmd ")))
completions = list(c.get_completions(Document("arg-cmd ")))

assert set(x.text for x in completions) == set([u"foo", u"bar"])
assert set(x.text for x in completions) == set(["foo", "bar"])
4 changes: 2 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def second_level_command_two():
pass

c = ClickCompleter(root_command)
completions = list(c.get_completions(Document(u"first-level-command ")))
completions = list(c.get_completions(Document("first-level-command ")))

assert set(x.text for x in completions) == set(
[u"second-level-command-one", u"second-level-command-two"]
["second-level-command-one", "second-level-command-two"]
)
5 changes: 2 additions & 3 deletions tests/test_command_collection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click
from click_repl import ClickCompleter
from prompt_toolkit.document import Document
Expand All @@ -22,6 +21,6 @@ def foobar_cmd():
pass

c = ClickCompleter(click.CommandCollection(sources=[foo_group, foobar_group]))
completions = list(c.get_completions(Document(u"foo")))
completions = list(c.get_completions(Document("foo")))

assert set(x.text for x in completions) == set([u"foo-cmd", u"foobar-cmd"])
assert set(x.text for x in completions) == set(["foo-cmd", "foobar-cmd"])
3 changes: 0 additions & 3 deletions tests/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ def test_repl():
def cli():
pass


@cli.command()
@click.option("--baz", is_flag=True)
def foo(baz):
print("Foo!")


@cli.command()
@click.option("--foo", is_flag=True)
def bar(foo):
print("Bar!")


register_repl(cli)

with pytest.raises(SystemExit):
Expand Down