Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/packaging/workbook.py: 56%

111 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# Copyright (c) 2010-2022 openpyxl 

2 

3from openpyxl.descriptors.serialisable import Serialisable 

4from openpyxl.descriptors import ( 

5 Alias, 

6 Typed, 

7 String, 

8 Integer, 

9 Bool, 

10 NoneSet, 

11 Set, 

12 Sequence, 

13) 

14from openpyxl.descriptors.excel import ExtensionList, Relation 

15from openpyxl.descriptors.sequence import NestedSequence 

16from openpyxl.descriptors.nested import NestedString 

17 

18from openpyxl.xml.constants import SHEET_MAIN_NS 

19 

20from openpyxl.workbook.defined_name import DefinedName, DefinedNameList 

21from openpyxl.workbook.external_reference import ExternalReference 

22from openpyxl.workbook.function_group import FunctionGroupList 

23from openpyxl.workbook.properties import WorkbookProperties, CalcProperties, FileVersion 

24from openpyxl.workbook.protection import WorkbookProtection, FileSharing 

25from openpyxl.workbook.smart_tags import SmartTagList, SmartTagProperties 

26from openpyxl.workbook.views import CustomWorkbookView, BookView 

27from openpyxl.workbook.web import WebPublishing, WebPublishObjectList 

28 

29 

30class FileRecoveryProperties(Serialisable): 

31 

32 tagname = "fileRecoveryPr" 

33 

34 autoRecover = Bool(allow_none=True) 

35 crashSave = Bool(allow_none=True) 

36 dataExtractLoad = Bool(allow_none=True) 

37 repairLoad = Bool(allow_none=True) 

38 

39 def __init__(self, 

40 autoRecover=None, 

41 crashSave=None, 

42 dataExtractLoad=None, 

43 repairLoad=None, 

44 ): 

45 self.autoRecover = autoRecover 

46 self.crashSave = crashSave 

47 self.dataExtractLoad = dataExtractLoad 

48 self.repairLoad = repairLoad 

49 

50 

51class ChildSheet(Serialisable): 

52 """ 

53 Represents a reference to a worksheet or chartsheet in workbook.xml 

54 

55 It contains the title, order and state but only an indirect reference to 

56 the objects themselves. 

57 """ 

58 

59 tagname = "sheet" 

60 

61 name = String() 

62 sheetId = Integer() 

63 state = NoneSet(values=(['visible', 'hidden', 'veryHidden'])) 

64 id = Relation() 

65 

66 def __init__(self, 

67 name=None, 

68 sheetId=None, 

69 state="visible", 

70 id=None, 

71 ): 

72 self.name = name 

73 self.sheetId = sheetId 

74 self.state = state 

75 self.id = id 

76 

77 

78class PivotCache(Serialisable): 

79 

80 tagname = "pivotCache" 

81 

82 cacheId = Integer() 

83 id = Relation() 

84 

85 def __init__(self, 

86 cacheId=None, 

87 id=None 

88 ): 

89 self.cacheId = cacheId 

90 self.id = id 

91 

92 

93class WorkbookPackage(Serialisable): 

94 

95 """ 

96 Represent the workbook file in the archive 

97 """ 

98 

99 tagname = "workbook" 

100 

101 conformance = NoneSet(values=['strict', 'transitional']) 

102 fileVersion = Typed(expected_type=FileVersion, allow_none=True) 

103 fileSharing = Typed(expected_type=FileSharing, allow_none=True) 

104 workbookPr = Typed(expected_type=WorkbookProperties, allow_none=True) 

105 properties = Alias("workbookPr") 

106 workbookProtection = Typed(expected_type=WorkbookProtection, allow_none=True) 

107 bookViews = NestedSequence(expected_type=BookView) 

108 sheets = NestedSequence(expected_type=ChildSheet) 

109 functionGroups = Typed(expected_type=FunctionGroupList, allow_none=True) 

110 externalReferences = NestedSequence(expected_type=ExternalReference) 

111 definedNames = Typed(expected_type=DefinedNameList, allow_none=True) 

112 calcPr = Typed(expected_type=CalcProperties, allow_none=True) 

113 oleSize = NestedString(allow_none=True, attribute="ref") 

114 customWorkbookViews = NestedSequence(expected_type=CustomWorkbookView) 

115 pivotCaches = NestedSequence(expected_type=PivotCache, allow_none=True) 

116 smartTagPr = Typed(expected_type=SmartTagProperties, allow_none=True) 

117 smartTagTypes = Typed(expected_type=SmartTagList, allow_none=True) 

118 webPublishing = Typed(expected_type=WebPublishing, allow_none=True) 

119 fileRecoveryPr = Typed(expected_type=FileRecoveryProperties, allow_none=True) 

120 webPublishObjects = Typed(expected_type=WebPublishObjectList, allow_none=True) 

121 extLst = Typed(expected_type=ExtensionList, allow_none=True) 

122 Ignorable = NestedString(namespace="http://schemas.openxmlformats.org/markup-compatibility/2006", allow_none=True) 

123 

124 __elements__ = ('fileVersion', 'fileSharing', 'workbookPr', 

125 'workbookProtection', 'bookViews', 'sheets', 'functionGroups', 

126 'externalReferences', 'definedNames', 'calcPr', 'oleSize', 

127 'customWorkbookViews', 'pivotCaches', 'smartTagPr', 'smartTagTypes', 

128 'webPublishing', 'fileRecoveryPr', 'webPublishObjects') 

129 

130 def __init__(self, 

131 conformance=None, 

132 fileVersion=None, 

133 fileSharing=None, 

134 workbookPr=None, 

135 workbookProtection=None, 

136 bookViews=(), 

137 sheets=(), 

138 functionGroups=None, 

139 externalReferences=(), 

140 definedNames=None, 

141 calcPr=None, 

142 oleSize=None, 

143 customWorkbookViews=(), 

144 pivotCaches=(), 

145 smartTagPr=None, 

146 smartTagTypes=None, 

147 webPublishing=None, 

148 fileRecoveryPr=None, 

149 webPublishObjects=None, 

150 extLst=None, 

151 Ignorable=None, 

152 ): 

153 self.conformance = conformance 

154 self.fileVersion = fileVersion 

155 self.fileSharing = fileSharing 

156 if workbookPr is None: 

157 workbookPr = WorkbookProperties() 

158 self.workbookPr = workbookPr 

159 self.workbookProtection = workbookProtection 

160 self.bookViews = bookViews 

161 self.sheets = sheets 

162 self.functionGroups = functionGroups 

163 self.externalReferences = externalReferences 

164 self.definedNames = definedNames 

165 self.calcPr = calcPr 

166 self.oleSize = oleSize 

167 self.customWorkbookViews = customWorkbookViews 

168 self.pivotCaches = pivotCaches 

169 self.smartTagPr = smartTagPr 

170 self.smartTagTypes = smartTagTypes 

171 self.webPublishing = webPublishing 

172 self.fileRecoveryPr = fileRecoveryPr 

173 self.webPublishObjects = webPublishObjects 

174 

175 

176 def to_tree(self): 

177 tree = super(WorkbookPackage, self).to_tree() 

178 tree.set("xmlns", SHEET_MAIN_NS) 

179 return tree 

180 

181 

182 @property 

183 def active(self): 

184 for view in self.bookViews: 

185 if view.activeTab is not None: 

186 return view.activeTab 

187 return 0 

188 

189 

190 @property 

191 def pivot_caches(self): 

192 """ 

193 Get PivotCache objects 

194 """ 

195 d = {} 

196 for c in self.caches: 

197 cache = get_rel(self.archive, self.rels, id=c.id, cls=CacheDefinition) 

198 if cache.deps: 

199 records = get_rel(self.archive, cache.deps, cache.id, RecordList) 

200 else: 

201 records = None 

202 cache.records = records 

203 d[c.cacheId] = cache 

204 return d