1+ import xml .etree .ElementTree as ET
12from enum import Enum , auto
23from dataclasses import dataclass
4+ from bs4 import BeautifulSoup
35
46
57class Kind (Enum ):
@@ -13,19 +15,38 @@ class Kind(Enum):
1315@dataclass
1416class Target :
1517 ident : str
16- vol : int = 0
17- chap : int = 0
18- numbers : tuple [int , ...] | None = None
18+ numbers : tuple [int , ...] = ()
1919 kind : Kind = Kind .PART
2020
2121 def __post_init__ (self ):
2222 self .kind = self .get_kind ()
23+ if self .kind is not Kind .CHAPTER :
24+ return
25+ self .numbers = (self .chapter , )
26+
27+ @property
28+ def volume (self ) -> int :
29+ return (self .chapter - 1 ) // 8 + 1
30+
31+ @property
32+ def chapter (self ) -> int :
33+ return CHAPTER_NUMBER [self .ident ]
2334
2435 def get_kind (self ) -> Kind :
2536 if self .ident .startswith ('ch_' ):
2637 return Kind .CHAPTER
38+ elif self .ident .endswith ('_sec' ):
39+ return Kind .SECTION
2740 raise ValueError (f'Kind of { self .ident !r} is unknown' )
2841
42+ def get_section_title (self , root : BeautifulSoup ) -> str :
43+ element = root .find (id = self .ident )
44+ if element :
45+ return element .get_text (strip = True )
46+ raise LookupError (f'element { self .ident } not found' )
47+
48+
49+
2950
3051def validate (xref ):
3152 return any ([
@@ -34,4 +55,34 @@ def validate(xref):
3455 xref .endswith ('_sec' ),
3556 xref .endswith ('_part' ),
3657 xref .endswith ('_tbl' ),
37- ])
58+ ])
59+
60+ CHAPTER_ID = {
61+ 1 : 'ch_data_model' ,
62+ 2 : 'ch_sequences' ,
63+ 3 : 'ch_dicts_sets' ,
64+ 4 : 'ch_str_bytes' ,
65+ 5 : 'ch_dataclass' ,
66+ 6 : 'ch_refs_mut_mem' ,
67+ 7 : 'ch_func_objects' ,
68+ 8 : 'ch_type_hints_def' ,
69+ 9 : 'ch_closure_decorator' ,
70+ 10 : 'ch_design_patterns' ,
71+ 11 : 'ch_pythonic_obj' ,
72+ 12 : 'ch_seq_methods' ,
73+ 13 : 'ch_ifaces_prot_abc' ,
74+ 14 : 'ch_inheritance' ,
75+ 15 : 'ch_more_types' ,
76+ 16 : 'ch_op_overload' ,
77+ 17 : 'ch_generators' ,
78+ 18 : 'ch_with_match' ,
79+ 19 : 'ch_concurrency_models' ,
80+ 20 : 'ch_executors' ,
81+ 21 : 'ch_async' ,
82+ 22 : 'ch_dynamic_attrs' ,
83+ 23 : 'ch_descriptors' ,
84+ 24 : 'ch_class_metaprog' ,
85+ }
86+
87+ CHAPTER_NUMBER = {i :n for (n , i ) in CHAPTER_ID .items ()}
88+
0 commit comments