Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Internal
--------
* Remove `align_decimals` preprocessor, which had no effect.
* Fix TLS deprecation warning in test suite.
* Convert importlib read_text and open_text uses to newer files() syntax


1.48.0 (2026/01/27)
Expand Down
15 changes: 8 additions & 7 deletions mycli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from os.path import exists
import struct
import sys
from typing import IO, BinaryIO, Literal, TextIO
from typing import IO, BinaryIO, Literal

from configobj import ConfigObj, ConfigObjError
from Cryptodome.Cipher import AES
Expand All @@ -23,7 +23,7 @@ def log(logger: logging.Logger, level: int, message: str) -> None:
logger.log(level, message)


def read_config_file(f: str | TextIO | TextIOWrapper, list_values: bool = True) -> ConfigObj | None:
def read_config_file(f: str | IO[str], list_values: bool = True) -> ConfigObj | None:
"""Read a config file.

*list_values* set to `True` is the default behavior of ConfigObj.
Expand All @@ -50,7 +50,7 @@ def read_config_file(f: str | TextIO | TextIOWrapper, list_values: bool = True)
return config


def get_included_configs(config_file: str | TextIOWrapper) -> list[str | TextIOWrapper]:
def get_included_configs(config_file: str | IO[str]) -> list[str | IO[str]]:
"""Get a list of configuration files that are included into config_path
with !includedir directive.

Expand All @@ -62,7 +62,7 @@ def get_included_configs(config_file: str | TextIOWrapper) -> list[str | TextIOW
"""
if not isinstance(config_file, str) or not os.path.isfile(config_file):
return []
included_configs: list[str | TextIOWrapper] = []
included_configs: list[str | IO[str]] = []

try:
with open(config_file) as f:
Expand All @@ -78,7 +78,7 @@ def get_included_configs(config_file: str | TextIOWrapper) -> list[str | TextIOW
return included_configs


def read_config_files(files: list[str | TextIOWrapper], list_values: bool = True) -> ConfigObj:
def read_config_files(files: list[str | IO[str]], list_values: bool = True) -> ConfigObj:
"""Read and merge a list of config files."""

config = create_default_config(list_values=list_values)
Expand All @@ -101,14 +101,15 @@ def read_config_files(files: list[str | TextIOWrapper], list_values: bool = True
def create_default_config(list_values: bool = True) -> ConfigObj:
import mycli

default_config_file = resources.open_text(mycli, "myclirc")
default_config_file = resources.files(mycli).joinpath("myclirc").open('r')
return read_config_file(default_config_file, list_values=list_values)


def write_default_config(destination: str, overwrite: bool = False) -> None:
import mycli

default_config = resources.read_text(mycli, "myclirc")
with resources.files(mycli).joinpath("myclirc").open('r') as f:
default_config = f.read()
destination = os.path.expanduser(destination)
if not overwrite and exists(destination):
return
Expand Down
17 changes: 11 additions & 6 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import threading
import traceback
from typing import Any, Generator, Iterable, Literal
from typing import IO, Any, Generator, Iterable, Literal

try:
from pwd import getpwuid
Expand Down Expand Up @@ -90,7 +90,7 @@ class MyCli:
defaults_suffix = None

# In order of being loaded. Files lower in list override earlier ones.
cnf_files: list[str | TextIOWrapper] = [
cnf_files: list[str | IO[str]] = [
"/etc/my.cnf",
"/etc/mysql/my.cnf",
"/usr/local/etc/my.cnf",
Expand All @@ -99,7 +99,7 @@ class MyCli:

# check XDG_CONFIG_HOME exists and not an empty string
xdg_config_home = os.environ.get("XDG_CONFIG_HOME", "~/.config")
system_config_files: list[str | TextIOWrapper] = [
system_config_files: list[str | IO[str]] = [
"/etc/myclirc",
os.path.join(os.path.expanduser(xdg_config_home), "mycli", "myclirc"),
]
Expand Down Expand Up @@ -134,7 +134,7 @@ def __init__(
self.cnf_files = [defaults_file]

# Load config.
config_files: list[str | TextIOWrapper] = self.system_config_files + [myclirc] + [self.pwd_config_file]
config_files: list[str | IO[str]] = self.system_config_files + [myclirc] + [self.pwd_config_file]
c = self.config = read_config_files(config_files)
self.multi_line = c["main"].as_bool("multi_line")
self.key_bindings = c["main"]["key_bindings"]
Expand Down Expand Up @@ -2005,10 +2005,15 @@ def is_select(status: str | None) -> bool:
def thanks_picker() -> str:
import mycli

lines = (resources.read_text(mycli, "AUTHORS") + resources.read_text(mycli, "SPONSORS")).split("\n")
lines: str = ""
with resources.files(mycli).joinpath("AUTHORS").open('r') as f:
lines += f.read()

with resources.files(mycli).joinpath("SPONSORS").open('r') as f:
lines += f.read()

contents = []
for line in lines:
for line in lines.split("\n"):
if m := re.match(r"^ *\* (.*)", line):
contents.append(m.group(1))
return choice(contents) if contents else 'our sponsors'
Expand Down