14
14
15
15
# Constants
16
16
DOC_MD_PATH = Path (__file__ ).parent / "media_properties.md"
17
+ SOURCE_MD_PATH = Path (__file__ ).parent / "media_props.md"
17
18
LOCAL_POSTGRES_FOLDER = Path (__file__ ).parents [3 ] / "docker" / "upstream_db"
18
19
19
20
SQL_PATH = {
@@ -110,13 +111,42 @@ def add_column_props(media_props, python_columns):
110
111
return media_props
111
112
112
113
114
+ def parse_markdown () -> dict [str , str ]:
115
+ """
116
+ Parse the markdown documentation file and return a dictionary with the
117
+ field name as key and the description as value.
118
+ """
119
+ with open (SOURCE_MD_PATH ) as f :
120
+ contents = [line for line in f .readlines () if line .strip ()]
121
+ current_field = ""
122
+ properties = {}
123
+ property = ""
124
+ value = {}
125
+ for i , line in enumerate (contents ):
126
+ if line .startswith ("# " ):
127
+ if current_field and value :
128
+ properties [current_field ] = value
129
+ current_field = line .replace ("# " , "" ).strip ()
130
+ value = {}
131
+ continue
132
+ elif line .startswith ("## " ):
133
+ property = line .replace ("## " , "" ).strip ()
134
+ value [property ] = ""
135
+ continue
136
+ else :
137
+ value [property ] += line
138
+
139
+ return properties
140
+
141
+
113
142
def generate_media_props () -> dict :
114
143
"""
115
144
Generate a dictionary with the media properties from the database,
116
145
python code and markdown documentation files.
117
146
"""
118
147
media_props = {}
119
148
python_columns = parse_python_columns ()
149
+
120
150
for media_type in ["image" , "audio" ]:
121
151
media_props [media_type ] = create_db_props_dict (media_type )
122
152
media_props [media_type ] = add_column_props (
@@ -148,7 +178,35 @@ def generate_media_props_table(media_properties) -> str:
148
178
return table
149
179
150
180
151
- def generate_markdown_doc (media_properties : dict [str , dict ]) -> str :
181
+ def generate_media_props_doc (
182
+ markdown_descriptions : dict , media_properties : dict
183
+ ) -> str :
184
+ """Generate the long-form documentation for each media property."""
185
+ media_docs = ""
186
+ for prop , description in markdown_descriptions .items ():
187
+ prop_heading = f"### { prop } \n \n "
188
+ media_types = []
189
+ for media_type , value in media_properties .items ():
190
+ print (prop in value .keys ())
191
+ if prop in value .keys ():
192
+ media_types .append (media_type )
193
+
194
+ print (f"\n Media Types: { ', ' .join (media_types )} \n " )
195
+ prop_heading += f"Media Types: { ', ' .join (media_types )} \n \n "
196
+ prop_doc = ""
197
+ for name , value in description .items ():
198
+ if value :
199
+ prop_doc += f"#### { name } \n \n "
200
+ prop_doc += f"{ value } \n \n "
201
+ if prop_doc :
202
+ media_docs += prop_heading + prop_doc
203
+
204
+ return media_docs
205
+
206
+
207
+ def generate_markdown_doc (
208
+ media_properties : dict [str , dict ], markdown_descriptions : dict [str , dict ]
209
+ ) -> str :
152
210
"""
153
211
Generate the tables with media properties database column and
154
212
Python objects characteristics.
@@ -162,13 +220,17 @@ def generate_markdown_doc(media_properties: dict[str, dict]) -> str:
162
220
media_props_doc += f"""## Audio Properties\n
163
221
{ generate_media_props_table (media_properties ["audio" ])}
164
222
"""
223
+ media_props_doc += f"""## Media Property Descriptions\n
224
+ { generate_media_props_doc (markdown_descriptions , media_properties )}
225
+ """
165
226
return media_props_doc
166
227
167
228
168
229
def write_media_props_doc (path : Path = DOC_MD_PATH ) -> None :
169
230
"""Generate the DAG documentation and write it to a file."""
170
231
media_properties = generate_media_props ()
171
- doc_text = generate_markdown_doc (media_properties )
232
+ markdown_descriptions = parse_markdown ()
233
+ doc_text = generate_markdown_doc (media_properties , markdown_descriptions )
172
234
log .info (f"Writing DAG doc to { path } " )
173
235
path .write_text (doc_text )
174
236
0 commit comments