Skip to content

Commit b94523e

Browse files
authored
ruff format
1 parent 3fd8d72 commit b94523e

File tree

5 files changed

+88
-60
lines changed

5 files changed

+88
-60
lines changed

mkdocs_table_reader_plugin/markdown.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import pandas as pd
44
import textwrap
55

6+
67
def replace_unescaped_pipes(text: str) -> str:
78
"""
89
Replace unescaped pipes.
9-
10+
1011
For regex explanation, see https://regex101.com/r/s8H588/1
1112
1213
Args:
@@ -24,20 +25,24 @@ def convert_to_md_table(df: pd.DataFrame, markdown_kwargs: Dict) -> str:
2425
"""
2526
# Escape any pipe characters, | to \|
2627
# See https://github.com/astanin/python-tabulate/issues/241
27-
df.columns = [replace_unescaped_pipes(c) if isinstance(c, str) else c for c in df.columns]
28+
df.columns = [
29+
replace_unescaped_pipes(c) if isinstance(c, str) else c for c in df.columns
30+
]
2831

2932
# Avoid deprecated applymap warning on pandas>=2.0
3033
# See https://github.com/timvink/mkdocs-table-reader-plugin/issues/55
3134
if pd.__version__ >= "2.1.0":
3235
df = df.map(lambda s: replace_unescaped_pipes(s) if isinstance(s, str) else s)
3336
else:
34-
df = df.applymap(lambda s: replace_unescaped_pipes(s) if isinstance(s, str) else s)
37+
df = df.applymap(
38+
lambda s: replace_unescaped_pipes(s) if isinstance(s, str) else s
39+
)
3540

3641
if "index" not in markdown_kwargs:
3742
markdown_kwargs["index"] = False
3843
if "tablefmt" not in markdown_kwargs:
3944
markdown_kwargs["tablefmt"] = "pipe"
40-
45+
4146
return df.to_markdown(**markdown_kwargs)
4247

4348

@@ -56,7 +61,7 @@ def fix_indentation(leading_spaces: str, text: str) -> str:
5661
leading_spaces = int(len(leading_spaces) / 4) * " "
5762

5863
fixed_lines = []
59-
for line in text.split('\n'):
64+
for line in text.split("\n"):
6065
fixed_lines.append(textwrap.indent(line, leading_spaces))
6166
text = "\n".join(fixed_lines)
6267
return text

mkdocs_table_reader_plugin/plugin.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from mkdocs.plugins import BasePlugin, get_plugin_logger
3+
from mkdocs.plugins import BasePlugin, get_plugin_logger
44
from mkdocs.config import config_options
55
from mkdocs.exceptions import ConfigurationError
66

@@ -12,11 +12,16 @@
1212

1313

1414
class TableReaderPlugin(BasePlugin):
15-
1615
config_scheme = (
1716
("data_path", config_options.Type(str, default=".")),
1817
("allow_missing_files", config_options.Type(bool, default=False)),
19-
("select_readers", config_options.ListOfItems(config_options.Choice(list(READERS.keys())), default = list(READERS.keys()))),
18+
(
19+
"select_readers",
20+
config_options.ListOfItems(
21+
config_options.Choice(list(READERS.keys())),
22+
default=list(READERS.keys()),
23+
),
24+
),
2025
)
2126

2227
def on_config(self, config, **kwargs):
@@ -30,34 +35,47 @@ def on_config(self, config, **kwargs):
3035
Config
3136
"""
3237
if "search_page_directory" in self.config:
33-
logger.warning("[table-reader]: The 'search_page_directory' configuration option is deprecated, it will always be searched. Please remove it from your mkdocs.yml.")
38+
logger.warning(
39+
"[table-reader]: The 'search_page_directory' configuration option is deprecated, it will always be searched. Please remove it from your mkdocs.yml."
40+
)
3441
if "base_path" in self.config:
35-
logger.warning("[table-reader]: The 'base_path' configuration option is deprecated. Both the config_dir and docs_dir will be searched. Please remove it from your mkdocs.yml.")
36-
37-
self.readers = {reader: READERS[reader].set_config_context(mkdocs_config=config, plugin_config=self.config) for reader in self.config.get('select_readers') if reader in self.config.get('select_readers',[])}
42+
logger.warning(
43+
"[table-reader]: The 'base_path' configuration option is deprecated. Both the config_dir and docs_dir will be searched. Please remove it from your mkdocs.yml."
44+
)
45+
46+
self.readers = {
47+
reader: READERS[reader].set_config_context(
48+
mkdocs_config=config, plugin_config=self.config
49+
)
50+
for reader in self.config.get("select_readers")
51+
if reader in self.config.get("select_readers", [])
52+
}
3853

3954
plugins = [p for p in config.get("plugins")]
4055

4156
# Plugins required before table-reader
4257
for post_load_plugin in ["markdownextradata"]:
4358
if post_load_plugin in plugins:
4459
if plugins.index("table-reader") > plugins.index(post_load_plugin):
45-
raise ConfigurationError(f"[table-reader]: Incompatible plugin order: Define 'table-reader' before '{post_load_plugin}' in your mkdocs.yml.")
46-
60+
raise ConfigurationError(
61+
f"[table-reader]: Incompatible plugin order: Define 'table-reader' before '{post_load_plugin}' in your mkdocs.yml."
62+
)
63+
4764
# Plugins required after table-reader
4865
for post_load_plugin in ["macros"]:
4966
if post_load_plugin in plugins:
5067
if plugins.index("table-reader") < plugins.index(post_load_plugin):
51-
raise ConfigurationError(f"[table-reader]: Incompatible plugin order: Define 'table-reader' after '{post_load_plugin}' in your mkdocs.yml.")
68+
raise ConfigurationError(
69+
f"[table-reader]: Incompatible plugin order: Define 'table-reader' after '{post_load_plugin}' in your mkdocs.yml."
70+
)
5271

5372
if "macros" in config.plugins:
54-
config.plugins['macros'].macros.update(self.readers)
55-
config.plugins['macros'].variables['macros'].update(self.readers)
56-
config.plugins['macros'].env.globals.update(self.readers)
73+
config.plugins["macros"].macros.update(self.readers)
74+
config.plugins["macros"].variables["macros"].update(self.readers)
75+
config.plugins["macros"].env.globals.update(self.readers)
5776
self.external_jinja_engine = True
5877
else:
5978
self.external_jinja_engine = False
60-
6179

6280
def on_pre_page(self, page, config, **kwargs):
6381
"""
@@ -102,26 +120,25 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
102120
for reader in self.readers:
103121
function = self.readers[reader]
104122
# Regex pattern for tags like {{ read_csv(..) }}
105-
# match group 0: to extract any leading whitespace
123+
# match group 0: to extract any leading whitespace
106124
# match group 1: to extract the arguments (positional and keywords)
107125
tag_pattern = re.compile(
108126
r"( *)\{\{\s+%s\((.+)\)\s+\}\}" % reader, flags=re.IGNORECASE
109127
)
110128
matches = re.findall(tag_pattern, markdown)
111-
112-
for result in matches:
113129

130+
for result in matches:
114131
# Deal with indentation
115132
# So we can fix inserting tables.
116133
# f.e. relevant when used inside content tabs
117134
leading_spaces = result[0]
118135

119136
# Safely parse the arguments
120137
pd_args, pd_kwargs = parse_argkwarg(result[1])
121-
138+
122139
# Load the table
123140
markdown_table = function(*pd_args, **pd_kwargs)
124-
141+
125142
# Insert markdown table
126143
# By replacing only the first occurrence of the regex pattern
127144
# You might insert multiple CSVs with a single reader like read_csv
@@ -131,5 +148,3 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
131148
markdown = tag_pattern.sub(markdown_table, markdown, count=1)
132149

133150
return markdown
134-
135-

mkdocs_table_reader_plugin/readers.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import functools
88

9-
from mkdocs_table_reader_plugin.utils import kwargs_in_func, kwargs_not_in_func
9+
from mkdocs_table_reader_plugin.utils import kwargs_in_func, kwargs_not_in_func
1010
from mkdocs_table_reader_plugin.markdown import convert_to_md_table
1111

1212
logger = logging.getLogger("mkdocs.plugins")
@@ -18,7 +18,7 @@ def __init__(self, func):
1818
self.func = func
1919
self.mkdocs_config = None
2020
self.plugin_config = None
21-
21+
2222
def set_config_context(self, mkdocs_config, plugin_config):
2323
self.mkdocs_config = mkdocs_config
2424
self.plugin_config = plugin_config
@@ -38,9 +38,15 @@ def __call__(self, *args, **kwargs):
3838
input_file_name = kwargs.pop("filepath_or_buffer")
3939

4040
possible_file_paths = [
41-
Path(os.path.dirname(os.path.abspath(self.mkdocs_config["config_file_path"]))) / Path(self.plugin_config.get("data_path")) / input_file_name,
42-
Path(os.path.abspath(self.mkdocs_config["docs_dir"])) / Path(self.plugin_config.get("data_path")) / input_file_name,
43-
Path(self.plugin_config._current_page).parent / input_file_name
41+
Path(
42+
os.path.dirname(os.path.abspath(self.mkdocs_config["config_file_path"]))
43+
)
44+
/ Path(self.plugin_config.get("data_path"))
45+
/ input_file_name,
46+
Path(os.path.abspath(self.mkdocs_config["docs_dir"]))
47+
/ Path(self.plugin_config.get("data_path"))
48+
/ input_file_name,
49+
Path(self.plugin_config._current_page).parent / input_file_name,
4450
]
4551
valid_file_paths = [path for path in possible_file_paths if path.exists()]
4652
if len(valid_file_paths) == 0:
@@ -50,9 +56,8 @@ def __call__(self, *args, **kwargs):
5056
return f"{{{{ Cannot find '{input_file_name}' }}}}"
5157
else:
5258
raise FileNotFoundError(msg)
53-
54-
return self.func(valid_file_paths[0], *args, **kwargs)
5559

60+
return self.func(valid_file_paths[0], *args, **kwargs)
5661

5762

5863
@ParseArgs
@@ -66,7 +71,6 @@ def read_csv(*args, **kwargs) -> str:
6671

6772
@ParseArgs
6873
def read_table(*args, **kwargs) -> str:
69-
7074
read_kwargs = kwargs_in_func(kwargs, pd.read_table)
7175
df = pd.read_table(*args, **read_kwargs)
7276

@@ -103,7 +107,6 @@ def read_excel(*args, **kwargs) -> str:
103107

104108
@ParseArgs
105109
def read_yaml(*args, **kwargs) -> str:
106-
107110
json_kwargs = kwargs_in_func(kwargs, pd.json_normalize)
108111
with open(args[0], "r") as f:
109112
df = pd.json_normalize(yaml.safe_load(f), **json_kwargs)
@@ -142,4 +145,3 @@ def read_raw(*args, **kwargs) -> str:
142145
"read_feather": read_feather,
143146
"read_raw": read_raw,
144147
}
145-

mkdocs_table_reader_plugin/safe_eval.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
`ast.literal_eval()` is not a drop-in replacement however, the function `safe_eval.safe_eval()` will catch some edge cases.
55
66
A downside of literal_eval() is that is cannot parse
7-
special characters like newlines (\r\t or \n). We need those kind of characters because pandas.read_csv() accepts
7+
special characters like newlines (\r\t or \n). We need those kind of characters because pandas.read_csv() accepts
88
a parameter 'sep' that could contain all sorts of regex.
99
1010
As an example, if we have this in our markdown file:
@@ -36,15 +36,15 @@ def safe_eval(string):
3636
"""
3737
A downside of literal_eval() is that is cannot parse
3838
special characters like newlines (\r\t or \n).
39-
40-
We need this because pandas.read_csv() accepts
39+
40+
We need this because pandas.read_csv() accepts
4141
a parameter 'sep' that could contain all sorts of regex.
4242
4343
Args:
4444
string (str): string to parse to literal python
4545
4646
Returns:
47-
str: The parsed literal python structure
47+
str: The parsed literal python structure
4848
"""
4949
if "\n" in string or "\\" in string or "\r" in string:
5050
# remove quotes
@@ -58,46 +58,46 @@ def safe_eval(string):
5858
def parse_argkwarg(input_str: str):
5959
"""
6060
Parses a string to detect both args and kwargs.
61-
62-
Adapted code from
61+
62+
Adapted code from
6363
https://stackoverflow.com/questions/9305387/string-of-kwargs-to-kwargs
6464
6565
Args:
6666
input_str (str): string with positional and keyword arguments
67-
67+
6868
Returns:
6969
args[List], kwargs[Dict]
7070
"""
7171
# below generated by copilot, validated by unit tests
7272
segments = []
73-
current_segment = ''
73+
current_segment = ""
7474
in_quotes = False
75-
quote_char = ''
75+
quote_char = ""
7676
bracket_count = 0
7777
tuple_count = 0
78-
78+
7979
for char in input_str:
8080
if char in "\"'" and not in_quotes:
8181
in_quotes = True
8282
quote_char = char
8383
elif char == quote_char and in_quotes:
8484
in_quotes = False
85-
quote_char = ''
86-
elif char == '[':
85+
quote_char = ""
86+
elif char == "[":
8787
bracket_count += 1
88-
elif char == ']':
88+
elif char == "]":
8989
bracket_count -= 1
90-
elif char == '(':
90+
elif char == "(":
9191
tuple_count += 1
92-
elif char == ')':
92+
elif char == ")":
9393
tuple_count -= 1
94-
elif char == ',' and not in_quotes and bracket_count == 0 and tuple_count == 0:
94+
elif char == "," and not in_quotes and bracket_count == 0 and tuple_count == 0:
9595
segments.append(current_segment.strip())
96-
current_segment = ''
96+
current_segment = ""
9797
continue
98-
98+
9999
current_segment += char
100-
100+
101101
segments.append(current_segment.strip()) # Add the last segment
102102
# end code generated by copilot, validated by unit tests
103103

mkdocs_table_reader_plugin/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import os
2-
from inspect import signature
2+
from inspect import signature
3+
34

45
def get_keywords(func):
5-
return [p.name for p in signature(func).parameters.values() if p.kind == p.POSITIONAL_OR_KEYWORD or p.kind == p.KEYWORD_ONLY]
6+
return [
7+
p.name
8+
for p in signature(func).parameters.values()
9+
if p.kind == p.POSITIONAL_OR_KEYWORD or p.kind == p.KEYWORD_ONLY
10+
]
11+
612

713
def kwargs_in_func(keywordargs, func):
8-
return {k:v for k, v in keywordargs.items() if k in get_keywords(func)}
14+
return {k: v for k, v in keywordargs.items() if k in get_keywords(func)}
15+
916

1017
def kwargs_not_in_func(keywordargs, func):
11-
return {k:v for k, v in keywordargs.items() if k not in get_keywords(func)}
18+
return {k: v for k, v in keywordargs.items() if k not in get_keywords(func)}
1219

1320

1421
class cd:
@@ -26,4 +33,3 @@ def __enter__(self):
2633

2734
def __exit__(self, etype, value, traceback):
2835
os.chdir(self.savedPath)
29-

0 commit comments

Comments
 (0)