1###############################################################################
2#
3# FeaturePropertyBag - A class for writing the Excel XLSX featurePropertyBag.xml
4# file.
5#
6# SPDX-License-Identifier: BSD-2-Clause
7#
8# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
9#
10
11# Package imports.
12from . import xmlwriter
13
14
15class FeaturePropertyBag(xmlwriter.XMLwriter):
16 """
17 A class for writing the Excel XLSX FeaturePropertyBag file.
18
19
20 """
21
22 ###########################################################################
23 #
24 # Public API.
25 #
26 ###########################################################################
27
28 def __init__(self):
29 """
30 Constructor.
31
32 """
33
34 super().__init__()
35
36 self.feature_property_bags = set()
37
38 ###########################################################################
39 #
40 # Private API.
41 #
42 ###########################################################################
43
44 def _assemble_xml_file(self):
45 # Assemble and write the XML file.
46
47 # Write the XML declaration.
48 self._xml_declaration()
49
50 # Write the FeaturePropertyBags element.
51 self._write_feature_property_bags()
52
53 # Write the Checkbox bag element.
54 self._write_checkbox_bag()
55
56 # Write the XFControls bag element.
57 self._write_xf_control_bag()
58
59 # Write the XFComplement bag element.
60 self._write_xf_compliment_bag()
61
62 # Write the XFComplements bag element.
63 self._write_xf_compliments_bag()
64
65 # Write the DXFComplements bag element.
66 if "DXFComplements" in self.feature_property_bags:
67 self._write_dxf_compliments_bag()
68
69 self._xml_end_tag("FeaturePropertyBags")
70
71 # Close the file.
72 self._xml_close()
73
74 ###########################################################################
75 #
76 # XML methods.
77 #
78 ###########################################################################
79
80 def _write_feature_property_bags(self):
81 # Write the <FeaturePropertyBags> element.
82
83 xmlns = (
84 "http://schemas.microsoft.com/office/spreadsheetml/2022/featurepropertybag"
85 )
86
87 attributes = [("xmlns", xmlns)]
88
89 self._xml_start_tag("FeaturePropertyBags", attributes)
90
91 def _write_checkbox_bag(self):
92 # Write the Checkbox <bag> element.
93 attributes = [("type", "Checkbox")]
94
95 self._xml_empty_tag("bag", attributes)
96
97 def _write_xf_control_bag(self):
98 # Write the XFControls<bag> element.
99 attributes = [("type", "XFControls")]
100
101 self._xml_start_tag("bag", attributes)
102
103 # Write the bagId element.
104 self._write_bag_id("CellControl", 0)
105
106 self._xml_end_tag("bag")
107
108 def _write_xf_compliment_bag(self):
109 # Write the XFComplement <bag> element.
110 attributes = [("type", "XFComplement")]
111
112 self._xml_start_tag("bag", attributes)
113
114 # Write the bagId element.
115 self._write_bag_id("XFControls", 1)
116
117 self._xml_end_tag("bag")
118
119 def _write_xf_compliments_bag(self):
120 # Write the XFComplements <bag> element.
121 attributes = [
122 ("type", "XFComplements"),
123 ("extRef", "XFComplementsMapperExtRef"),
124 ]
125
126 self._xml_start_tag("bag", attributes)
127 self._xml_start_tag("a", [("k", "MappedFeaturePropertyBags")])
128
129 self._write_bag_id("", 2)
130
131 self._xml_end_tag("a")
132 self._xml_end_tag("bag")
133
134 def _write_dxf_compliments_bag(self):
135 # Write the DXFComplements <bag> element.
136 attributes = [
137 ("type", "DXFComplements"),
138 ("extRef", "DXFComplementsMapperExtRef"),
139 ]
140
141 self._xml_start_tag("bag", attributes)
142 self._xml_start_tag("a", [("k", "MappedFeaturePropertyBags")])
143
144 self._write_bag_id("", 2)
145
146 self._xml_end_tag("a")
147 self._xml_end_tag("bag")
148
149 def _write_bag_id(self, key, bag_id):
150 # Write the <bagId> element.
151 attributes = []
152
153 if key:
154 attributes = [("k", key)]
155
156 self._xml_data_element("bagId", bag_id, attributes)