Skip to content

Commit

Permalink
Update arg kwargs parsing to support komma-separated lists also
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink authored May 4, 2024
1 parent 5d3b741 commit 170e82c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
37 changes: 31 additions & 6 deletions mkdocs_table_reader_plugin/safe_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,59 @@ def safe_eval(string):
return literal_eval(string)


def parse_argkwarg(string: str):
def parse_argkwarg(input_str: str):
"""
Parses a string to detect both args and kwargs.
Adapted code from
https://stackoverflow.com/questions/9305387/string-of-kwargs-to-kwargs
Args:
string (str): string with positional and keyword arguments
input_str (str): string with positional and keyword arguments
Returns:
args[List], kwargs[Dict]
"""

argkwargs = re.split(r"(?<!\=)(?:,{1} )(?!\=)", string)
# below generated by copilot, validated by unit tests
segments = []
current_segment = ''
in_quotes = False
quote_char = ''
bracket_count = 0

for char in input_str:
if char in "\"'" and not in_quotes:
in_quotes = True
quote_char = char
elif char == quote_char and in_quotes:
in_quotes = False
quote_char = ''
elif char == '[':
bracket_count += 1
elif char == ']':
bracket_count -= 1
elif char == ',' and not in_quotes and bracket_count == 0:
segments.append(current_segment.strip())
current_segment = ''
continue

current_segment += char

segments.append(current_segment.strip()) # Add the last segment
# end code generated by copilot, validated by unit tests

args = []
kwargs = []

for i in argkwargs:
for i in segments:
i = i.strip()
if "=" in i:
kwargs.append(i)
else:
if len(kwargs) != 0:
raise AssertionError(
"[table-reader-plugin] Make sure the python in your reader tag is correct: Positional arguments follow keyword arguments in '%s'"
% string
% input_str
)
args.append(literal_eval(i))

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="mkdocs-table-reader-plugin",
version="2.1.0",
version="2.2.0",
description="MkDocs plugin to directly insert tables from files into markdown.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/mixed_quotation_marks/docs/file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A,B,C,D,E
10,23,45,78,99
34,56,87,12,37
93,67,34,23,11%
7 changes: 7 additions & 0 deletions tests/fixtures/mixed_quotation_marks/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Test page

This test is related to this issue: https://github.com/timvink/mkdocs-table-reader-plugin/issues/29

## Table with mixed quote usage

{{ read_csv("file.csv", usecols=['A', 'B']) }}
7 changes: 7 additions & 0 deletions tests/fixtures/mixed_quotation_marks/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
site_name: test git_table_reader site
use_directory_urls: true

plugins:
- search
- table-reader:
data_path: "docs"
15 changes: 14 additions & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ def test_wrong_path(tmp_path):
)

result = build_docs_setup(tmp_proj)
assert result.exit_code == 1, "'mkdocs build' command failed"
assert result.exit_code == 1, "'mkdocs build' command succeeded but should have failed"
assert "[table-reader-plugin]: Cannot find table file" in result.output
assert "non_existing_table.csv" in result.output


def test_mixed_quotation_marks(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/mixed_quotation_marks/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"

# Make sure the file.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"56", contents)
8 changes: 8 additions & 0 deletions tests/test_kwargs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import pandas as pd
from mkdocs_table_reader_plugin.utils import get_keywords, kwargs_in_func, kwargs_not_in_func
from mkdocs_table_reader_plugin.safe_eval import parse_argkwarg


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


def test_parse_argkwarg():

assert parse_argkwarg("sep=';'") == ([], {'sep': ';'})
assert parse_argkwarg('"file.csv", usecols=["A", "B"]') == (['file.csv'], {'usecols': ['A', 'B']})
assert parse_argkwarg('"file.csv", usecols=[\'A\',\'B\']') == (['file.csv'], {'usecols': ['A', 'B']})
assert parse_argkwarg("'assets/tables/table_with_carriage_return.csv', sep = ','") == (['assets/tables/table_with_carriage_return.csv'], {'sep': ','})

0 comments on commit 170e82c

Please sign in to comment.