Skip to content

Commit 4205f70

Browse files
committed
Type hints to config.py key_bindings.py and lexer.py
1 parent 80c6873 commit 4205f70

3 files changed

Lines changed: 39 additions & 21 deletions

File tree

litecli/config.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1+
"""LiteCLI configuration helpers with typing."""
2+
13
# mypy: ignore-errors
24

5+
from __future__ import annotations
6+
37
import errno
48
import shutil
59
import os
610
import platform
711
from os.path import expanduser, exists, dirname
12+
from typing import Optional
13+
814
from configobj import ConfigObj
915

1016

11-
def config_location():
17+
def config_location() -> str:
1218
if "XDG_CONFIG_HOME" in os.environ:
1319
return "%s/litecli/" % expanduser(os.environ["XDG_CONFIG_HOME"])
1420
elif platform.system() == "Windows":
15-
return os.getenv("USERPROFILE") + "\\AppData\\Local\\dbcli\\litecli\\"
21+
userprofile = os.getenv("USERPROFILE", "")
22+
return userprofile + "\\AppData\\Local\\dbcli\\litecli\\"
1623
else:
1724
return expanduser("~/.config/litecli/")
1825

1926

20-
def load_config(usr_cfg, def_cfg=None):
27+
def load_config(usr_cfg: str, def_cfg: Optional[str] = None) -> ConfigObj:
2128
cfg = ConfigObj()
22-
cfg.merge(ConfigObj(def_cfg, interpolation=False))
29+
if def_cfg:
30+
cfg.merge(ConfigObj(def_cfg, interpolation=False))
2331
cfg.merge(ConfigObj(expanduser(usr_cfg), interpolation=False, encoding="utf-8"))
2432
cfg.filename = expanduser(usr_cfg)
25-
2633
return cfg
2734

2835

29-
def ensure_dir_exists(path):
36+
def ensure_dir_exists(path: str) -> None:
3037
parent_dir = expanduser(dirname(path))
3138
try:
3239
os.makedirs(parent_dir)
@@ -36,27 +43,25 @@ def ensure_dir_exists(path):
3643
raise
3744

3845

39-
def write_default_config(source, destination, overwrite=False):
46+
def write_default_config(source: str, destination: str, overwrite: bool = False) -> None:
4047
destination = expanduser(destination)
4148
if not overwrite and exists(destination):
4249
return
43-
4450
ensure_dir_exists(destination)
45-
4651
shutil.copyfile(source, destination)
4752

4853

49-
def upgrade_config(config, def_config):
54+
def upgrade_config(config: str, def_config: str) -> None:
5055
cfg = load_config(config, def_config)
5156
cfg.write()
5257

5358

54-
def get_config(liteclirc_file=None):
59+
def get_config(liteclirc_file: Optional[str] = None) -> ConfigObj:
5560
from litecli import __file__ as package_root
5661

5762
package_root = os.path.dirname(package_root)
5863

59-
liteclirc_file = liteclirc_file or "%sconfig" % config_location()
64+
liteclirc_file = liteclirc_file or f"{config_location()}config"
6065

6166
default_config = os.path.join(package_root, "liteclirc")
6267
try:

litecli/key_bindings.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1+
"""Key bindings with type hints."""
2+
13
# mypy: ignore-errors
24

3-
from __future__ import unicode_literals
5+
from __future__ import annotations
46
import logging
7+
from typing import Any
8+
59
from prompt_toolkit.enums import EditingMode
610
from prompt_toolkit.filters import completion_is_selected
711
from prompt_toolkit.key_binding import KeyBindings
12+
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
813

914
_logger = logging.getLogger(__name__)
1015

1116

12-
def cli_bindings(cli):
17+
def cli_bindings(cli: Any) -> KeyBindings:
1318
"""Custom key bindings for cli."""
1419
kb = KeyBindings()
1520

1621
@kb.add("f3")
17-
def _(event):
22+
def _(_event: KeyPressEvent) -> None:
1823
"""Enable/Disable Multiline Mode."""
1924
_logger.debug("Detected F3 key.")
2025
cli.multi_line = not cli.multi_line
2126

2227
@kb.add("f4")
23-
def _(event):
28+
def _(event: KeyPressEvent) -> None:
2429
"""Toggle between Vi and Emacs mode."""
2530
_logger.debug("Detected F4 key.")
2631
if cli.key_bindings == "vi":
@@ -31,7 +36,7 @@ def _(event):
3136
cli.key_bindings = "vi"
3237

3338
@kb.add("tab")
34-
def _(event):
39+
def _(event: KeyPressEvent) -> None:
3540
"""Force autocompletion at cursor."""
3641
_logger.debug("Detected <Tab> key.")
3742
b = event.app.current_buffer
@@ -41,7 +46,7 @@ def _(event):
4146
b.start_completion(select_first=True)
4247

4348
@kb.add("s-tab")
44-
def _(event):
49+
def _(event: KeyPressEvent) -> None:
4550
"""Force autocompletion at cursor."""
4651
_logger.debug("Detected <Tab> key.")
4752
b = event.app.current_buffer
@@ -51,7 +56,7 @@ def _(event):
5156
b.start_completion(select_last=True)
5257

5358
@kb.add("c-space")
54-
def _(event):
59+
def _(event: KeyPressEvent) -> None:
5560
"""
5661
Initialize autocompletion at cursor.
5762
@@ -69,7 +74,7 @@ def _(event):
6974
b.start_completion(select_first=False)
7075

7176
@kb.add("enter", filter=completion_is_selected)
72-
def _(event):
77+
def _(event: KeyPressEvent) -> None:
7378
"""Makes the enter key work as the tab key only when showing the menu.
7479
7580
In other words, don't execute query when enter is pressed in

litecli/lexer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# mypy: ignore-errors
22

3+
from __future__ import annotations
4+
35
from pygments.lexer import inherit
46
from pygments.lexers.sql import MySqlLexer
57
from pygments.token import Keyword
@@ -8,4 +10,10 @@
810
class LiteCliLexer(MySqlLexer):
911
"""Extends SQLite lexer to add keywords."""
1012

11-
tokens = {"root": [(r"\brepair\b", Keyword), (r"\boffset\b", Keyword), inherit]}
13+
tokens = {
14+
"root": [
15+
(r"\brepair\b", Keyword),
16+
(r"\boffset\b", Keyword),
17+
inherit,
18+
]
19+
}

0 commit comments

Comments
 (0)