Skip to content

Commit

Permalink
feat/config (#341)
Browse files Browse the repository at this point in the history
* feat: config files

* feat: add config app

* feat: style config app

* fix: change script name to harlequin:config

* fix: revert script name for windows compat

* refactor: move wizard from harlequin_config to harlequin --config

* chore: update changelog

* feat: add harlequin theme, refactor colors, style wizard

* fix: please mypy with tomli version check

* fix: make limit type check more robust

* fix: remove python_version config from mypy

* fix: use posix paths in cli option tests

* fix: revert python_version change; run linting on older python
  • Loading branch information
tconbeer authored Nov 30, 2023
1 parent b29ed17 commit 10c98cd
Show file tree
Hide file tree
Showing 30 changed files with 1,182 additions and 146 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ concurrency:

jobs:
static:
name: Static Analysis - 3.11
name: Static Analysis - 3.8
runs-on: ubuntu-latest
steps:
- name: Check out Repo
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Set up Python 3.11
- name: Set up Python 3.8
uses: actions/setup-python@v4
id: setup-python
with:
python-version: "3.11"
python-version: "3.8"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
!tests/data/**/*.sql
Pipfile
snapshot_report.html
.harlequin.toml

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
10 changes: 6 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
repos:
- repo: https://github.com/psf/black
rev: 23.10.0
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.1
rev: v1.7.1
hooks:
- id: mypy
additional_dependencies:
Expand All @@ -22,6 +22,8 @@ repos:
- pytest
- types-pygments
- rich-click>=1.7.1
- questionary
- tomlkit
args:
- "--disallow-untyped-calls"
- "--disallow-untyped-defs"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Features

- Harlequin can now be configured using a TOML file. The config file can both specify options for Harlequin (like the theme and row limit) and also for installed adapters (like the host, username, and password for a database connection). The config file can define multiple "profiles" (sets of configuration), and you can select the profile to use when starting Harlequin with the `--profile` option (alias `-P`). By default, Harlequin searches the current directory and home directories for files called either `.harlequin.toml` or `pyproject.toml`, and merges the config it finds in them. You can specify a different path using the `--config-path` option. Values loaded from config files can be overridden by passing CLI options ([#206](https://github.com/tconbeer/harlequin/issues/206)).
- Harlequin now ships with a wizard to make it easy to create or update config files. Simply run Harlequin with the `--config` option.
- Adds a `harlequin` theme. You can use it with `harlequin -t harlequin`.

## [1.5.0] - 2023-11-28

### Breaking Changes
Expand Down
68 changes: 59 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ shandy-sqlfmt = ">=0.19.0"
platformdirs = "^3.10.0"
pyperclip = "^1.8.2"
importlib_metadata = { version = ">=4.6.0", python = "<3.10.0" }
tomli = { version = "^2.0.1", python = "<3.11.0" }
tomlkit = "^0.12.3"

# database adapters (optional installs for extras)
harlequin-postgres = { version = "^0.1", optional = true }
questionary = "^2.0.1"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.1"
textual-dev = "^1.0.1"
harlequin-postgres = "^0.1.3"

[tool.poetry.group.static.dependencies]
black = "^23.3.0"
Expand All @@ -66,6 +70,9 @@ postgres = ["harlequin-postgres"]
duckdb = "harlequin_duckdb:DuckDbAdapter"
sqlite = "harlequin_sqlite:HarlequinSqliteAdapter"

[tool.poetry.plugins."pygments.styles"]
harlequin = "harlequin.colors:HarlequinPygmentsStyle"

[tool.ruff]
select = ["A", "B", "E", "F", "I"]
target-version = "py38"
Expand Down
5 changes: 4 additions & 1 deletion src/harlequin/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def __init__(self, conn_str: Sequence[str], **options: Any) -> None:
- **options (Any): Options received from the command line, config file,
or env variables. Adapters should be robust to receiving both subsets
and supersets of their declared options. They should disregard any
extra (unexpected) kwargs.
extra (unexpected) kwargs. Adapters should check the types of options,
as they may not be cast to the correct types.
Raises: HarlequinConfigError if a received option is the wrong value or type.
"""
pass

Expand Down
52 changes: 19 additions & 33 deletions src/harlequin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from functools import partial
from typing import Dict, List, Optional, Type, Union

from rich import print
from rich.panel import Panel
from textual import work
from textual.app import App, ComposeResult
from textual.binding import Binding
Expand Down Expand Up @@ -38,9 +36,11 @@
export_callback,
)
from harlequin.exception import (
HarlequinConfigError,
HarlequinConnectionError,
HarlequinQueryError,
HarlequinThemeError,
pretty_print_error,
)


Expand Down Expand Up @@ -93,55 +93,41 @@ def __init__(
self,
adapter: HarlequinAdapter,
theme: str = "monokai",
max_results: int = 100_000,
max_results: int | str = 100_000,
driver_class: Union[Type[Driver], None] = None,
css_path: Union[CSSPathType, None] = None,
watch_css: bool = False,
):
super().__init__(driver_class, css_path, watch_css)
self.adapter = adapter
self.theme = theme
self.max_results = max_results
self.limit = min(500, max_results) if max_results > 0 else 500
try:
self.max_results = int(max_results)
except ValueError:
pretty_print_error(
HarlequinConfigError(
f"limit={max_results!r} was set by config file but is not "
"a valid integer."
)
)
self.exit(return_code=2)
else:
self.limit = min(500, self.max_results) if self.max_results > 0 else 500
self.query_timer: Union[float, None] = None
try:
self.connection = self.adapter.connect()
except HarlequinConnectionError as e:
print(
Panel.fit(
str(e),
title=e.title
if e.title
else (
"Harlequin encountered an error "
"while connecting to the database."
),
title_align="left",
border_style="red",
)
)
self.exit()
pretty_print_error(e)
self.exit(return_code=2)
else:
if self.connection.init_message:
self.notify(self.connection.init_message)

try:
self.app_colors = HarlequinColors.from_theme(theme)
except HarlequinThemeError as e:
print(
Panel.fit(
(
f"No theme found with the name {e}.\n"
"Theme must be the name of a Pygments Style. "
"You can browse the supported styles here:\n"
"https://pygments.org/styles/"
),
title="Harlequin couldn't load your theme.",
title_align="left",
border_style="red",
)
)
self.exit()
pretty_print_error(e)
self.exit(return_code=2)
else:
self.design = self.app_colors.design_system
self.stylesheet = Stylesheet(variables=self.get_css_variables())
Expand Down
Loading

0 comments on commit 10c98cd

Please sign in to comment.