Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
feat: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
eshepelyuk committed Jul 24, 2023
1 parent 45078c1 commit cec3201
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# pykli
# pyKLI

ksqlDB command line client written in Python.
[ksqlDB](https://ksqldb.io/) command line client written in Python.

This project is in a very early stage.

Please try and report bugs :)

This project is in a very early stage. Please try and report bugs :)
PRs are welcome.

## Installation
Expand All @@ -15,5 +18,5 @@ pip install -U git+https://github.com/eshepelyuk/pykli@main

* `SHOW ...`
* `LIST ...`
* `DESCRIBE ...`
* `DESCRIBE ...`, without `EXTENDED`
* `DROP ...`
10 changes: 9 additions & 1 deletion pykli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from xdg_base_dirs import xdg_config_home
from pygments.styles import get_style_by_name

import logging

CONFIG_DIR = xdg_config_home() / "pykli"

Expand All @@ -11,3 +11,11 @@
MONOKAI_STYLE = get_style_by_name('monokai')

CONFIG_DIR.mkdir(exist_ok=True, parents=True)

logging.basicConfig(level=logging.INFO,
filename=LOG_FILE, filemode='w',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logging.getLogger(__name__).setLevel(logging.DEBUG)

LOG = logging.getLogger(__name__)
12 changes: 8 additions & 4 deletions pykli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import click
from pprint import pformat

from . import LOG
from .ksqldb import KsqlDBClient, is_stmt
from .printer import print_stmt, perr, pok
from .repl_print import print_stmt, perr, pok
from .repl_read import pykli_prompt


Expand All @@ -16,13 +17,14 @@ def repl(ksqldb: KsqlDBClient):
text = session.prompt("pykli> ")
if is_stmt(text):
resp = ksqldb.stmt(text)
LOG.debug(f"SQL={text}, ksqlDB={pformat(resp)}")
print_stmt(resp)
elif text == "quit" or text == "exit":
break
except httpx.HTTPStatusError as e:
perr(e.response.json()["message"])
perr(e.response.json()["message"], e)
except httpx.TransportError as e:
perr(f"Transport error: {pformat(e)}")
perr(f"Transport error: {pformat(e)}", e)
except KeyboardInterrupt:
continue
except EOFError:
Expand All @@ -39,13 +41,15 @@ def main(server):
\b SERVER The address of the ksqlDB server."""

LOG.info(f"pyKLI started: server={server}")

ksqldb = KsqlDBClient(server)

try:
info = ksqldb.info()
pok(f"Connected to {server}, version: {info['version']}, status: {info['serverStatus']}")
except httpx.HTTPError as e:
perr(f"Transport error: {pformat(e)}")
perr("Transport error", e)
sys.exit(1)

sys.exit(repl(ksqldb))
Expand Down
18 changes: 6 additions & 12 deletions pykli/completer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pprint import pprint
from prompt_toolkit.completion import NestedCompleter, Completer, Completion, CompleteEvent
from prompt_toolkit.completion import NestedCompleter, Completer #, Completion, CompleteEvent
from prompt_toolkit.completion.word_completer import WordCompleter
from prompt_toolkit.document import Document
from prompt_toolkit.application import get_app, run_in_terminal
from typing import Any, Iterable, Mapping, Set, Union
# from prompt_toolkit.application import get_app, run_in_terminal
# from typing import Any, Iterable, Mapping, Set, Union

COMPLETIONS = {
"describe": {
Expand Down Expand Up @@ -34,20 +33,15 @@ def get_completions(self, document, complete_event):
yield from self._nested.get_completions(document, complete_event)
else:
text = document.current_line_before_cursor.lstrip()
stripped_len = len(document.current_line_before_cursor) - len(text)
# stripped_len = len(document.current_line_before_cursor) - len(text)

first_term = [ln for ln in document.lines if ln][0]
completer = self._nested.options.get(first_term)

if completer is not None:
remaining_text = text
move_cursor = len(text) - len(remaining_text) + stripped_len

# run_in_terminal(lambda : print("111", f"##{text}##", f"@@{first_term}@@",
# f"!!{remaining_text}!!", f"=>{document.cursor_position} {move_cursor}"))
new_document = Document(
remaining_text,
cursor_position=len(remaining_text),
text,
cursor_position=len(text),
)
yield from completer.get_completions(new_document, complete_event)

Expand Down
2 changes: 1 addition & 1 deletion pykli/keybindgings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.filters import completion_is_selected, is_searching, has_completions, Condition
from prompt_toolkit.application import get_app, run_in_terminal
from prompt_toolkit.application import get_app # , run_in_terminal

def _is_complete(sql):
# A complete command is an sql statement that ends with a semicolon, unless
Expand Down
20 changes: 10 additions & 10 deletions pykli/printer.py → pykli/repl_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cli_helpers.tabular_output import format_output
from cli_helpers.tabular_output.preprocessors import style_output

from . import MONOKAI_STYLE
from . import MONOKAI_STYLE, LOG

DESCRIBE_SRC_HEADERS = ("Field", "Type")

Expand Down Expand Up @@ -68,12 +68,14 @@ def pok(text):
click.secho(text)


def pwarn(text):
def pwarn(text, data=None):
click.secho(text)
LOG.warning(f"{text}, data={pformat(data)}")


def perr(text):
def perr(text, data=None):
click.secho(text, fg="red")
LOG.error(f"{text}, data={pformat(data)}")


def format_ksql_type(type_def) -> str:
Expand Down Expand Up @@ -105,8 +107,7 @@ def print_show(data_type, json):

pok("\n".join(ff))
else:
pwarn(f"`show` not implemented for: {data_type}")
pwarn(pformat(json))
pwarn(f"`show` not implemented for: {data_type}", json)


def print_describe_src(data):
Expand Down Expand Up @@ -160,7 +161,7 @@ def print_stmt(json_arr):
print_show(json["@type"], json)
elif stmt.startswith("describe"):
match json:
case {"@type": "source_description", "sourceDescription": data}:
case {"@type": "sourceDescription", "sourceDescription": data}:
print_describe_src(data)
case {"@type": "source_descriptions", "sourceDescriptions": data_arr}:
for data in data_arr:
Expand All @@ -170,7 +171,7 @@ def print_stmt(json_arr):
case {"@type": "describe_function"}:
print_describe_func(json)
case _:
pwarn(pformat(json))
pwarn(f"unknown format {stmt}", json)
elif stmt.startswith(("drop", "DROP")):
match json:
case {"@type": "drop_connector"}:
Expand All @@ -180,8 +181,7 @@ def print_stmt(json_arr):
case {"@type": "currentStatus", "commandStatus": {"message": msg}}:
click.secho(msg)
case _:
pwarn(pformat(json))
pwarn(f"unknown format {stmt}", json)
else:
perr(f"output not yet implemented: {stmt}")
pok(pformat(json))
perr(f"output not yet implemented: {stmt}", json)

2 changes: 1 addition & 1 deletion tests/test_printer.py → tests/test_repl_print.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pprint import pformat
from pykli.printer import format_ksql_type
from pykli.repl_print import format_ksql_type

STRING_COLUMN = {
'name': 'StrFld',
Expand Down

0 comments on commit cec3201

Please sign in to comment.