Skip to content

Commit

Permalink
Revise to look for case in jupysql and close_match for SqlMagic
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejb committed Jan 25, 2024
1 parent ab7786b commit 68504bb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
61 changes: 57 additions & 4 deletions src/sql/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,33 @@ def get_user_configs(primary_path, alternate_path):
section_found = False
if file_path and file_path.exists():
data = load_toml(file_path)
section_names = ["tool", "jupysql", "SqlMagic"]

# Look for SqlMagic section in toml file
data = get_nested(data, section_names)
data = data.get("tool")

# Look for jupysql section under tool
if data:
keys = data.keys()
data = data.get("jupysql")
if data is None:
similar_key = case_insensitive_match("jupysql", keys)
if similar_key:
display.message(
f"Hint: We found 'tool.{similar_key}' in {file_path}. "
f"Did you mean 'tool.jupysql'?"
)

# Look for SqlMagic section under jupysql
if data:
keys = data.keys()
data = data.get("SqlMagic")
if data is None:
similar_key_list = find_close_match("SqlMagic", keys)
if similar_key_list:
raise exceptions.ConfigurationError(
f"[tool.jupysql.{similar_key_list[0]}] is an invalid section "
f"name in {file_path}. "
f"Did you mean [tool.jupysql.SqlMagic]?"
)

if data is None:
if display_tip:
Expand Down Expand Up @@ -561,6 +584,7 @@ def enclose_table_with_double_quotations(table, conn):

return _table


def is_rendering_required(line):
"""Function to check possibility of line
text containing expandable arguments"""
Expand Down Expand Up @@ -614,4 +638,33 @@ def expand_args(args, user_ns):
setattr(args, attribute, substituted_value)
else:
rendered_value = render_string_using_namespace(value, user_ns)
setattr(args, attribute, rendered_value)
setattr(args, attribute, rendered_value)


def case_insensitive_match(target, string_list):
"""
Perform a case-insensitive match of a target string against a list of strings.
Parameters
----------
target : str
The target string to match.
string_list : list of str
The list of strings to search through.
Returns
-------
str or None
The first matching string from the list, preserving its original case,
or None if there is no match.
Examples
--------
>>> case_insensitive_match('foo', ['bar', 'FOO'])
'FOO'
"""
target_lower = target.lower()
for string in string_list:
if string.lower() == target_lower:
return string
return None
14 changes: 14 additions & 0 deletions src/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ def test_load_toml_user_configurations_not_specified(
),
(
"""
[tool.jupysql.SQLMagics]
autocommit = true
""",
"[tool.jupysql.SQLMagics] is an invalid section name in {path}. Did you mean [tool.jupysql.SqlMagic]?",
),
(
"""
[tool.jupysql.SqlMagic]
autocommit = True
""",
Expand Down Expand Up @@ -577,6 +584,13 @@ def test_toml_optional_message(tmp_empty, monkeypatch, ip, capsys):
"[tool.jupysql.SqlMagic] present in {pyproject_path} but empty.",
],
),
(
"[tool.JupySQL.SqlMagic]",
"",
[
"Hint: We found 'tool.JupySQL' in {pyproject_path}. Did you mean 'tool.jupysql'?",
],
),
],
)
def test_user_config_load_sequence_and_messages(
Expand Down

0 comments on commit 68504bb

Please sign in to comment.