2929class FileFormat :
3030 """Mapping of keys for subtrees and items, dependant on depth in the ToC."""
3131
32- subtrees_keys : Sequence [str ] = attr .ib (())
33- items_keys : Sequence [str ] = attr .ib (())
34- default_subtrees_key : str = attr .ib (DEFAULT_SUBTREES_KEY )
35- default_items_key : str = attr .ib (DEFAULT_ITEMS_KEY )
3632 toc_defaults : Dict [str , Any ] = attr .ib (factory = dict )
33+ subtrees_keys : Sequence [str ] = attr .ib (default = ())
34+ items_keys : Sequence [str ] = attr .ib (default = ())
35+ default_subtrees_key : str = attr .ib (default = DEFAULT_SUBTREES_KEY )
36+ default_items_key : str = attr .ib (default = DEFAULT_ITEMS_KEY )
3737
3838 def get_subtrees_key (self , depth : int ) -> str :
39- """Get the subtrees key name for this depth in the ToC
39+ """Get the subtrees key name for this depth in the ToC.
4040
4141 :param depth: recursive depth (starts at 0)
42+ :return: subtrees key name
4243 """
4344 try :
4445 return self .subtrees_keys [depth ]
4546 except IndexError :
4647 return self .default_subtrees_key
4748
4849 def get_items_key (self , depth : int ) -> str :
49- """Get the items key name for this depth in the ToC
50+ """Get the items key name for this depth in the ToC.
5051
5152 :param depth: recursive depth (starts at 0)
53+ :return: items key name
5254 """
5355 try :
5456 return self .items_keys [depth ]
@@ -72,18 +74,27 @@ def get_items_key(self, depth: int) -> str:
7274
7375
7476class MalformedError (Exception ):
75- """Raised if toc file is malformed."""
77+ """Raised if the `_toc.yml` file is malformed."""
7678
7779
7880def parse_toc_yaml (path : Union [str , Path ], encoding : str = "utf8" ) -> SiteMap :
79- """Parse the ToC file."""
81+ """Parse the ToC file.
82+
83+ :param path: `_toc.yml` file path
84+ :param encoding: `_toc.yml` file character encoding
85+ :return: parsed site map
86+ """
8087 with Path (path ).open (encoding = encoding ) as handle :
8188 data = yaml .safe_load (handle )
8289 return parse_toc_data (data )
8390
8491
8592def parse_toc_data (data : Dict [str , Any ]) -> SiteMap :
86- """Parse a dictionary of the ToC."""
93+ """Parse a dictionary of the ToC.
94+
95+ :param data: ToC data dictionary
96+ :return: parsed site map
97+ """
8798 if not isinstance (data , Mapping ):
8899 raise MalformedError (f"toc is not a mapping: { type (data )} " )
89100
@@ -95,14 +106,19 @@ def parse_toc_data(data: Dict[str, Any]) -> SiteMap:
95106 f"'{ data .get (FILE_FORMAT_KEY , 'default' )} '"
96107 )
97108
98- defaults : Dict [str , Any ] = {** file_format .toc_defaults , ** data .get ("defaults" , {})}
109+ defaults : Dict [str , Any ] = {
110+ ** file_format .toc_defaults ,
111+ ** data .get ("defaults" , {}),
112+ }
99113
100114 doc_item , docs_list = _parse_doc_item (
101115 data , defaults , "/" , depth = 0 , is_root = True , file_format = file_format
102116 )
103117
104118 site_map = SiteMap (
105- root = doc_item , meta = data .get ("meta" ), file_format = data .get (FILE_FORMAT_KEY )
119+ root = doc_item ,
120+ meta = data .get ("meta" ),
121+ file_format = data .get (FILE_FORMAT_KEY ),
106122 )
107123
108124 _parse_docs_list (
@@ -121,7 +137,17 @@ def _parse_doc_item(
121137 file_format : FileFormat ,
122138 is_root : bool = False ,
123139) -> Tuple [Document , Sequence [Tuple [str , Dict [str , Any ]]]]:
124- """Parse a single doc item."""
140+ """Parse a single doc item.
141+
142+ :param data: doc item dictionary
143+ :param defaults: doc item defaults dictionary
144+ :param path: doc item file path
145+ :param depth: recursive depth (starts at 0)
146+ :param file_format: doc item file format
147+ :param is_root: whether this is the root item, defaults to False
148+ :raises MalformedError: invalid doc item
149+ :return: parsed doc item
150+ """
125151 file_key = ROOT_KEY if is_root else FILE_KEY
126152 if file_key not in data :
127153 raise MalformedError (f"'{ file_key } ' key not found @ '{ path } '" )
@@ -275,13 +301,26 @@ def _parse_docs_list(
275301 depth : int ,
276302 file_format : FileFormat ,
277303):
278- """Parse a list of docs."""
304+ """Parse a list of docs.
305+
306+ :param docs_list: sequence of doc items
307+ :param site_map: site map
308+ :param defaults: default doc item values
309+ :param path: file path, unused
310+ :param depth: recursive depth (starts at 0)
311+ :param file_format: doc item file format
312+ :raises MalformedError: doc file used multiple times
313+ """
279314 for child_path , doc_data in docs_list :
280315 docname = doc_data [FILE_KEY ]
281316 if docname in site_map :
282317 raise MalformedError (f"document file used multiple times: '{ docname } '" )
283318 child_item , child_docs_list = _parse_doc_item (
284- doc_data , defaults , child_path , depth = depth , file_format = file_format
319+ doc_data ,
320+ defaults ,
321+ child_path ,
322+ depth = depth ,
323+ file_format = file_format ,
285324 )
286325 site_map [docname ] = child_item
287326
@@ -296,7 +335,13 @@ def _parse_docs_list(
296335
297336
298337def create_toc_dict (site_map : SiteMap , * , skip_defaults : bool = True ) -> Dict [str , Any ]:
299- """Create the Toc dictionary from a site-map."""
338+ """Create the ToC dictionary from a site-map.
339+
340+ :param site_map: site map
341+ :param skip_defaults: do not add key/values for values that are already the default
342+ :raises KeyError: invalid file format
343+ :return: ToC dictionary
344+ """
300345 try :
301346 file_format = FILE_FORMATS [site_map .file_format or "default" ]
302347 except KeyError :
@@ -327,10 +372,19 @@ def _docitem_to_dict(
327372 is_root : bool = False ,
328373 parsed_docnames : Optional [Set [str ]] = None ,
329374) -> Dict [str , Any ]:
330- """
375+ """Create ToC dictionary from a `Document` and a `SiteMap`.
331376
377+ :param doc_item: document instance
378+ :param site_map: site map
379+ :param depth: recursive depth (starts at 0)
380+ :param file_format: doc item file format
332381 :param skip_defaults: do not add key/values for values that are already the default
333-
382+ :param is_root: whether this is the root item, defaults to False
383+ :param parsed_docnames: parsed document names cache used to prevent infinite
384+ recursion
385+ :raises RecursionError: site map recursion
386+ :raises TypeError: invalid ToC item
387+ :return: parsed ToC dictionary
334388 """
335389 file_key = ROOT_KEY if is_root else FILE_KEY
336390 subtrees_key = file_format .get_subtrees_key (depth )
0 commit comments