Skip to content

Commit 170e82c

Browse files
authored
Update arg kwargs parsing to support komma-separated lists also
1 parent 5d3b741 commit 170e82c

File tree

7 files changed

+72
-8
lines changed

7 files changed

+72
-8
lines changed

mkdocs_table_reader_plugin/safe_eval.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,59 @@ def safe_eval(string):
5555
return literal_eval(string)
5656

5757

58-
def parse_argkwarg(string: str):
58+
def parse_argkwarg(input_str: str):
5959
"""
6060
Parses a string to detect both args and kwargs.
6161
6262
Adapted code from
6363
https://stackoverflow.com/questions/9305387/string-of-kwargs-to-kwargs
6464
6565
Args:
66-
string (str): string with positional and keyword arguments
66+
input_str (str): string with positional and keyword arguments
6767
6868
Returns:
6969
args[List], kwargs[Dict]
7070
"""
71-
72-
argkwargs = re.split(r"(?<!\=)(?:,{1} )(?!\=)", string)
71+
# below generated by copilot, validated by unit tests
72+
segments = []
73+
current_segment = ''
74+
in_quotes = False
75+
quote_char = ''
76+
bracket_count = 0
77+
78+
for char in input_str:
79+
if char in "\"'" and not in_quotes:
80+
in_quotes = True
81+
quote_char = char
82+
elif char == quote_char and in_quotes:
83+
in_quotes = False
84+
quote_char = ''
85+
elif char == '[':
86+
bracket_count += 1
87+
elif char == ']':
88+
bracket_count -= 1
89+
elif char == ',' and not in_quotes and bracket_count == 0:
90+
segments.append(current_segment.strip())
91+
current_segment = ''
92+
continue
93+
94+
current_segment += char
95+
96+
segments.append(current_segment.strip()) # Add the last segment
97+
# end code generated by copilot, validated by unit tests
7398

7499
args = []
75100
kwargs = []
76101

77-
for i in argkwargs:
102+
for i in segments:
78103
i = i.strip()
79104
if "=" in i:
80105
kwargs.append(i)
81106
else:
82107
if len(kwargs) != 0:
83108
raise AssertionError(
84109
"[table-reader-plugin] Make sure the python in your reader tag is correct: Positional arguments follow keyword arguments in '%s'"
85-
% string
110+
% input_str
86111
)
87112
args.append(literal_eval(i))
88113

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-table-reader-plugin",
8-
version="2.1.0",
8+
version="2.2.0",
99
description="MkDocs plugin to directly insert tables from files into markdown.",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A,B,C,D,E
2+
10,23,45,78,99
3+
34,56,87,12,37
4+
93,67,34,23,11%
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Test page
2+
3+
This test is related to this issue: https://github.com/timvink/mkdocs-table-reader-plugin/issues/29
4+
5+
## Table with mixed quote usage
6+
7+
{{ read_csv("file.csv", usecols=['A', 'B']) }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test git_table_reader site
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- table-reader:
7+
data_path: "docs"

tests/test_build.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ def test_wrong_path(tmp_path):
346346
)
347347

348348
result = build_docs_setup(tmp_proj)
349-
assert result.exit_code == 1, "'mkdocs build' command failed"
349+
assert result.exit_code == 1, "'mkdocs build' command succeeded but should have failed"
350350
assert "[table-reader-plugin]: Cannot find table file" in result.output
351351
assert "non_existing_table.csv" in result.output
352+
353+
354+
def test_mixed_quotation_marks(tmp_path):
355+
tmp_proj = setup_clean_mkdocs_folder(
356+
"tests/fixtures/mixed_quotation_marks/mkdocs.yml", tmp_path
357+
)
358+
result = build_docs_setup(tmp_proj)
359+
assert result.exit_code == 0, "'mkdocs build' command failed"
360+
361+
# Make sure the file.csv is inserted
362+
page_with_tag = tmp_proj / "site/index.html"
363+
contents = page_with_tag.read_text()
364+
assert re.search(r"56", contents)

tests/test_kwargs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import pandas as pd
33
from mkdocs_table_reader_plugin.utils import get_keywords, kwargs_in_func, kwargs_not_in_func
4+
from mkdocs_table_reader_plugin.safe_eval import parse_argkwarg
45

56

67
def test_kwargs():
@@ -12,3 +13,10 @@ def test_kwargs():
1213
assert kwargs_in_func(keywords, pd.read_csv) == {'sep' : ';'}
1314
assert kwargs_not_in_func(keywords, pd.read_csv) == {'hi' : 'there'}
1415

16+
17+
def test_parse_argkwarg():
18+
19+
assert parse_argkwarg("sep=';'") == ([], {'sep': ';'})
20+
assert parse_argkwarg('"file.csv", usecols=["A", "B"]') == (['file.csv'], {'usecols': ['A', 'B']})
21+
assert parse_argkwarg('"file.csv", usecols=[\'A\',\'B\']') == (['file.csv'], {'usecols': ['A', 'B']})
22+
assert parse_argkwarg("'assets/tables/table_with_carriage_return.csv', sep = ','") == (['assets/tables/table_with_carriage_return.csv'], {'sep': ','})

0 commit comments

Comments
 (0)