@@ -39,9 +39,19 @@ def parse_args() -> argparse.Namespace:
39
39
)
40
40
parser .add_argument ('--ignore' , metavar = 'S' , type = str , nargs = '+' ,
41
41
help = 'Directory names to ignore. E.g --ignore _snippets images' )
42
+ parser .add_argument (
43
+ "--verbose" ,
44
+ action = "store_true" ,
45
+ help = "Enable verbose logging"
46
+ )
42
47
return parser .parse_args ()
43
48
44
- def extract_title_description_slug (filename ):
49
+ def log (message , verbose = False ):
50
+ """Print message only if verbose mode is enabled"""
51
+ if verbose :
52
+ print (message )
53
+
54
+ def extract_title_description_slug (filename , verbose = False ):
45
55
data = defaultdict (str )
46
56
missing_fields = []
47
57
frontmatter_data = {}
@@ -63,29 +73,30 @@ def extract_title_description_slug(filename):
63
73
64
74
data .update (frontmatter_data )
65
75
66
- if missing_fields :
67
- print (f"Warning: { filename } is missing some fields:" )
76
+ if missing_fields and verbose :
77
+ log (f"Warning: { filename } is missing some fields:" , verbose )
68
78
for field in missing_fields :
69
- print (f"- { field } " )
79
+ log (f"- { field } " , verbose )
70
80
71
81
return frontmatter_data
72
82
except OSError as e :
73
- print (f"Ran into a problem reading frontmatter: { e } " )
83
+ log (f"Ran into a problem reading frontmatter: { e } " , verbose )
74
84
sys .exit (1 )
75
- def walk_dirs (root_dir , ignore_dirs = []):
85
+
86
+ def walk_dirs (root_dir , ignore_dirs = [], verbose = False ):
76
87
for root , dirs , files in os .walk (root_dir ):
77
88
# Modify the 'dirs' list in-place to remove ignored directories
78
89
if (ignore_dirs is not None ):
79
90
dirs [:] = [d for d in dirs if d not in ignore_dirs
80
- and not any (d .startswith (ig ) for ig in ignore_dirs )]
91
+ and not any (d .startswith (ig ) for ig in ignore_dirs )]
81
92
yield root
82
93
83
- def write_md_to_file (json_items , path_to_md_file ):
94
+ def write_md_to_file (json_items , path_to_md_file , verbose = False ):
84
95
try :
85
96
with open (path_to_md_file , encoding = 'utf-8' ) as pre_check :
86
97
existing_content = pre_check .read ()
87
98
if "| Page | Description |" in existing_content :
88
- print (f"Markdown table already exists in { path_to_md_file } . Skipping." )
99
+ log (f"Markdown table already exists in { path_to_md_file } . Skipping." , verbose )
89
100
return
90
101
91
102
with open (path_to_md_file , 'a' , encoding = 'utf-8' ) as f :
@@ -100,12 +111,12 @@ def write_md_to_file(json_items, path_to_md_file):
100
111
link = f"[{ title } ]({ slug } )" if slug else title
101
112
f .write (f"| { link } | { description } |\n " )
102
113
103
- print (f"Markdown table appended to { path_to_md_file } " )
114
+ log (f"Markdown table appended to { path_to_md_file } " , verbose )
104
115
105
116
except Exception as e :
106
- print (f"An error occurred: { e } " )
107
- def write_to_file (json_items , directory , output = None ):
117
+ log (f"An error occurred: { e } " , verbose )
108
118
119
+ def write_to_file (json_items , directory , output = None , verbose = False ):
109
120
if output is not None :
110
121
# output to the given path the toc.json file
111
122
# If dir='docs/en/interfaces/formats' the file is called docs_en_interfaces_formats_toc.json
@@ -117,20 +128,21 @@ def write_to_file(json_items, directory, output=None):
117
128
with open (output_path , "w" ) as f :
118
129
json .dump (json_items , f , indent = 4 , default = str )
119
130
f .write ('\n ' )
120
- print (f"Wrote { output_path } " )
131
+ log (f"Wrote { output_path } " , verbose )
121
132
except OSError as e :
122
133
if e .errno == 21 :
123
- print (f"Directory already exists: { e } " )
134
+ log (f"Directory already exists: { e } " , verbose )
124
135
elif e .errno != 17 :
125
- print (f"An error occurred creating directory: { e } " )
136
+ log (f"An error occurred creating directory: { e } " , verbose )
137
+
126
138
def write_file (json_items , args , directory ):
127
- print (args )
128
139
if (args .out is not None ) and (args .md is None ):
129
- write_to_file (json_items , directory + "/toc.json" , args .out )
140
+ write_to_file (json_items , directory + "/toc.json" , args .out , args . verbose )
130
141
elif (args .out is None ) and (args .md is None ):
131
- write_to_file (json_items , directory + "/toc.json" )
142
+ write_to_file (json_items , directory + "/toc.json" , verbose = args . verbose )
132
143
elif (args .out is None ) and (args .md is not None ):
133
- write_md_to_file (json_items , args .md )
144
+ write_md_to_file (json_items , args .md , args .verbose )
145
+
134
146
def sort_by_title_before_underscore (json_items ):
135
147
def sort_key (item ):
136
148
title = item .get ("title" , "" )
@@ -142,21 +154,19 @@ def sort_key(item):
142
154
return sorted (json_items , key = sort_key )
143
155
144
156
def main ():
145
-
146
157
# Extract script arguments
147
158
args = parse_args ()
148
159
root_dir = args .dir
149
160
if root_dir is None :
150
- print ("Please provide a directory with argument --dir" )
161
+ log ("Please provide a directory with argument --dir" , True ) # Always show critical errors
151
162
sys .exit (1 )
152
163
if os .path .lexists (root_dir ) is False :
153
- print ("Path provided does not exist" )
164
+ log ("Path provided does not exist" , True ) # Always show critical errors
154
165
sys .exit (1 )
155
166
if args .single_toc is True :
156
167
json_items = [] # single list for all directories
157
168
158
- for directory in walk_dirs (root_dir , args .ignore ): # Walk directories
159
-
169
+ for directory in walk_dirs (root_dir , args .ignore , args .verbose ): # Walk directories
160
170
if args .single_toc is False :
161
171
json_items = [] # new list for each directory
162
172
@@ -167,7 +177,7 @@ def main():
167
177
else :
168
178
# index.md is ignored as we expect this to be the page for the table of contents
169
179
if (filename .endswith (".md" ) or filename .endswith (".mdx" )) and filename != "index.md" :
170
- result = extract_title_description_slug (full_path )
180
+ result = extract_title_description_slug (full_path , args . verbose )
171
181
if result is not None :
172
182
json_items .append (result )
173
183
if args .single_toc is False :
@@ -177,7 +187,7 @@ def main():
177
187
# output to the specified directory if arg --out is provided
178
188
write_file (json_items , args , directory )
179
189
else :
180
- print ("Ran into an issue trying to extract YAML: empty result" )
190
+ log ("Ran into an issue trying to extract YAML: empty result" , args . verbose )
181
191
182
192
if args .single_toc is True :
183
193
# don't write toc.json for empty folders
@@ -190,5 +200,4 @@ def main():
190
200
sys .exit (1 )
191
201
192
202
if __name__ == "__main__" :
193
- main ()
194
-
203
+ main ()
0 commit comments