2121}
2222
2323
24- @dataclass
25- class Notes :
26- description : str | None
27- table : str | None
28-
29-
3024@dataclass
3125class File :
3226 name : str
3327 description : str
3428 table : str
35- notes : Notes | None
29+ notes : str | None
3630
3731
3832@dataclass
@@ -50,28 +44,28 @@ def generate_markdown() -> str:
5044
5145def load_sections () -> Iterable [Section ]:
5246 for title , patterns in _FILE_ORDER .items ():
47+ paths = []
5348 for pattern in patterns :
54- paths = map (str , _SCHEMA_DIR .glob (f"{ pattern } .yaml" ))
55- files = (load_file (Path (path )) for path in sorted (paths ))
56- yield Section (title , files )
49+ paths . extend ( map (str , _SCHEMA_DIR .glob (f"{ pattern } .yaml" ) ))
50+ files = (load_file (Path (path )) for path in sorted (paths ))
51+ yield Section (title , files )
5752
5853
5954def load_file (path : Path ) -> File :
6055 with path .open () as f :
6156 data = yaml .safe_load (f )
6257
6358 try :
64- table , notes_table = fields2table (data ["fields" ])
59+ table = fields2table (data ["fields" ])
6560 except KeyError :
6661 print (f"MISSING VALUE IN { path } " )
6762 raise
6863
6964 name = f"{ path .stem } .csv"
70- title = add_full_stop (data ["title" ])
71- if desc := data .get ("description" , None ):
72- desc = add_full_stop (desc )
73- notes = Notes (desc , notes_table ) if desc or notes_table else None
74- return File (name , title , table , notes )
65+ desc = add_full_stop (data ["description" ])
66+ if note := data .get ("notes" , None ):
67+ note = format_notes (note )
68+ return File (name , desc , table , note )
7569
7670
7771def add_full_stop (s : str ) -> str :
@@ -84,28 +78,28 @@ def add_full_stop(s: str) -> str:
8478
8579def fields2table (fields : list [dict [str , str ]]) -> tuple [str , str | None ]:
8680 data = []
87- notes = []
8881 for f in fields :
89- row = {"Field" : f"`{ f ['name' ]} `" , "Description" : f ["title" ]}
90- data .append (row )
91-
92- if desc := f .get ("description" , "" ):
93- # MarkdownTable can't handle newlines, so replace with HTML equivalent
94- desc = desc .replace ("\n \n " , "<br /><br />" ).replace ("\n " , " " )
95- row = {"Field" : f"`{ f ['name' ]} `" , "Notes" : desc }
96- notes .append (row )
97-
98- data = [
99- {
82+ # MarkdownTable can't handle newlines, so replace with HTML equivalent
83+ notes = f .get ("notes" , "" )
84+ notes = notes .replace ("\n \n " , "<br /><br />" ).replace ("\n " , " " )
85+ row = {
10086 "Field" : f"`{ f ['name' ]} `" ,
101- "Description" : f ["title" ],
87+ "Description" : f ["description" ],
88+ "Notes" : notes ,
10289 }
103- for f in fields
104- ]
105-
90+ data .append (row )
10691 table = str (MarkdownTable .from_dicts (data ))
107- notes_table = str (MarkdownTable .from_dicts (notes )) if notes else None
108- return table , notes_table
92+ return table
93+
94+
95+ def format_notes (notes ) -> str :
96+ if isinstance (notes , list ):
97+ items = [add_full_stop (item ) for item in notes ]
98+ elif isinstance (notes , str ):
99+ items = [add_full_stop (notes )]
100+ else :
101+ return ""
102+ return "\n " .join (f"- { item } " for item in items )
109103
110104
111105if __name__ == "__main__" :
0 commit comments