11import pandas as pd
22import yaml
3+ import os
4+ from pathlib import Path
5+ import logging
6+
7+ import functools
38
49from mkdocs_table_reader_plugin .utils import kwargs_in_func , kwargs_not_in_func
510from mkdocs_table_reader_plugin .markdown import convert_to_md_table
611
7- def read_csv (* args , ** kwargs ):
8-
12+ logger = logging .getLogger ("mkdocs.plugins" )
13+
14+
15+ class ParseArgs :
16+ def __init__ (self , func ):
17+ functools .update_wrapper (self , func )
18+ self .func = func
19+ self .mkdocs_config = None
20+ self .plugin_config = None
21+
22+ def set_config_context (self , mkdocs_config , plugin_config ):
23+ self .mkdocs_config = mkdocs_config
24+ self .plugin_config = plugin_config
25+ return self
26+
27+ def __call__ (self , * args , ** kwargs ):
28+ assert self .mkdocs_config is not None , "mkdocs_config is not set"
29+ assert self .plugin_config is not None , "plugin_config is not set"
30+
31+ # Extract the filepath,
32+ # which is the first positional argument
33+ # or a named argument when there are no positional arguments
34+ args = list (args )
35+ if len (args ) > 0 :
36+ input_file_name = args .pop (0 )
37+ else :
38+ input_file_name = kwargs .pop ("filepath_or_buffer" )
39+
40+ 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
44+ ]
45+ valid_file_paths = [path for path in possible_file_paths if path .exists ()]
46+ if len (valid_file_paths ) == 0 :
47+ msg = f"[table-reader-plugin]: Cannot find table file '{ input_file_name } '. The following directories were searched: { * possible_file_paths ,} "
48+ if self .plugin_config .get ("allow_missing_files" ):
49+ logger .warning (msg )
50+ return f"{{{{ Cannot find '{ input_file_name } ' }}}}"
51+ else :
52+ raise FileNotFoundError (msg )
53+
54+ return self .func (valid_file_paths [0 ], * args , ** kwargs )
55+
56+
57+
58+ @ParseArgs
59+ def read_csv (* args , ** kwargs ) -> str :
960 read_kwargs = kwargs_in_func (kwargs , pd .read_csv )
1061 df = pd .read_csv (* args , ** read_kwargs )
1162
1263 markdown_kwargs = kwargs_not_in_func (kwargs , pd .read_csv )
1364 return convert_to_md_table (df , markdown_kwargs )
1465
1566
16- def read_table (* args , ** kwargs ):
67+ @ParseArgs
68+ def read_table (* args , ** kwargs ) -> str :
1769
1870 read_kwargs = kwargs_in_func (kwargs , pd .read_table )
1971 df = pd .read_table (* args , ** read_kwargs )
@@ -22,30 +74,35 @@ def read_table(*args, **kwargs):
2274 return convert_to_md_table (df , markdown_kwargs )
2375
2476
25- def read_fwf (* args , ** kwargs ):
77+ @ParseArgs
78+ def read_fwf (* args , ** kwargs ) -> str :
2679 read_kwargs = kwargs_in_func (kwargs , pd .read_fwf )
2780 df = pd .read_fwf (* args , ** read_kwargs )
2881
2982 markdown_kwargs = kwargs_not_in_func (kwargs , pd .read_fwf )
3083 return convert_to_md_table (df , markdown_kwargs )
3184
32- def read_json (* args , ** kwargs ):
85+
86+ @ParseArgs
87+ def read_json (* args , ** kwargs ) -> str :
3388 read_kwargs = kwargs_in_func (kwargs , pd .read_json )
3489 df = pd .read_json (* args , ** read_kwargs )
3590
3691 markdown_kwargs = kwargs_not_in_func (kwargs , pd .read_json )
3792 return convert_to_md_table (df , markdown_kwargs )
3893
3994
40- def read_excel (* args , ** kwargs ):
95+ @ParseArgs
96+ def read_excel (* args , ** kwargs ) -> str :
4197 read_kwargs = kwargs_in_func (kwargs , pd .read_excel )
4298 df = pd .read_excel (* args , ** read_kwargs )
4399
44100 markdown_kwargs = kwargs_not_in_func (kwargs , pd .read_excel )
45101 return convert_to_md_table (df , markdown_kwargs )
46102
47103
48- def read_yaml (* args , ** kwargs ):
104+ @ParseArgs
105+ def read_yaml (* args , ** kwargs ) -> str :
49106
50107 json_kwargs = kwargs_in_func (kwargs , pd .json_normalize )
51108 with open (args [0 ], "r" ) as f :
@@ -54,14 +111,18 @@ def read_yaml(*args, **kwargs):
54111 markdown_kwargs = kwargs_not_in_func (kwargs , pd .json_normalize )
55112 return convert_to_md_table (df , markdown_kwargs )
56113
57- def read_feather (* args , ** kwargs ):
114+
115+ @ParseArgs
116+ def read_feather (* args , ** kwargs ) -> str :
58117 read_kwargs = kwargs_in_func (kwargs , pd .read_feather )
59118 df = pd .read_feather (* args , ** read_kwargs )
60119
61120 markdown_kwargs = kwargs_not_in_func (kwargs , pd .read_feather )
62121 return convert_to_md_table (df , markdown_kwargs )
63122
64- def read_raw (* args , ** kwargs ):
123+
124+ @ParseArgs
125+ def read_raw (* args , ** kwargs ) -> str :
65126 """Read a file as-is.
66127
67128 Returns:
0 commit comments