Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/styles/styleable.py: 36%

103 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 copy import copy 

4from warnings import warn 

5 

6from .numbers import ( 

7 BUILTIN_FORMATS, 

8 BUILTIN_FORMATS_MAX_SIZE, 

9 BUILTIN_FORMATS_REVERSE, 

10) 

11from .proxy import StyleProxy 

12from .cell_style import StyleArray 

13from .named_styles import NamedStyle 

14from .builtins import styles 

15 

16 

17class StyleDescriptor(object): 

18 

19 def __init__(self, collection, key): 

20 self.collection = collection 

21 self.key = key 

22 

23 def __set__(self, instance, value): 

24 coll = getattr(instance.parent.parent, self.collection) 

25 if not getattr(instance, "_style"): 

26 instance._style = StyleArray() 

27 setattr(instance._style, self.key, coll.add(value)) 

28 

29 

30 def __get__(self, instance, cls): 

31 coll = getattr(instance.parent.parent, self.collection) 

32 if not getattr(instance, "_style"): 

33 instance._style = StyleArray() 

34 idx = getattr(instance._style, self.key) 

35 return StyleProxy(coll[idx]) 

36 

37 

38class NumberFormatDescriptor(object): 

39 

40 key = "numFmtId" 

41 collection = '_number_formats' 

42 

43 def __set__(self, instance, value): 

44 coll = getattr(instance.parent.parent, self.collection) 

45 if value in BUILTIN_FORMATS_REVERSE: 

46 idx = BUILTIN_FORMATS_REVERSE[value] 

47 else: 

48 idx = coll.add(value) + BUILTIN_FORMATS_MAX_SIZE 

49 

50 if not getattr(instance, "_style"): 

51 instance._style = StyleArray() 

52 setattr(instance._style, self.key, idx) 

53 

54 

55 def __get__(self, instance, cls): 

56 if not getattr(instance, "_style"): 

57 instance._style = StyleArray() 

58 idx = getattr(instance._style, self.key) 

59 if idx < BUILTIN_FORMATS_MAX_SIZE: 

60 return BUILTIN_FORMATS.get(idx, "General") 

61 coll = getattr(instance.parent.parent, self.collection) 

62 return coll[idx - BUILTIN_FORMATS_MAX_SIZE] 

63 

64 

65class NamedStyleDescriptor(object): 

66 

67 key = "xfId" 

68 collection = "_named_styles" 

69 

70 

71 def __set__(self, instance, value): 

72 if not getattr(instance, "_style"): 

73 instance._style = StyleArray() 

74 coll = getattr(instance.parent.parent, self.collection) 

75 if isinstance(value, NamedStyle): 

76 style = value 

77 if style not in coll: 

78 instance.parent.parent.add_named_style(style) 

79 elif value not in coll.names: 

80 if value in styles: # is it builtin? 

81 style = styles[value] 

82 if style not in coll: 

83 instance.parent.parent.add_named_style(style) 

84 else: 

85 raise ValueError("{0} is not a known style".format(value)) 

86 else: 

87 style = coll[value] 

88 instance._style = copy(style.as_tuple()) 

89 

90 

91 def __get__(self, instance, cls): 

92 if not getattr(instance, "_style"): 

93 instance._style = StyleArray() 

94 idx = getattr(instance._style, self.key) 

95 coll = getattr(instance.parent.parent, self.collection) 

96 return coll.names[idx] 

97 

98 

99class StyleArrayDescriptor(object): 

100 

101 def __init__(self, key): 

102 self.key = key 

103 

104 def __set__(self, instance, value): 

105 if instance._style is None: 

106 instance._style = StyleArray() 

107 setattr(instance._style, self.key, value) 

108 

109 

110 def __get__(self, instance, cls): 

111 if instance._style is None: 

112 return False 

113 return bool(getattr(instance._style, self.key)) 

114 

115 

116class StyleableObject(object): 

117 """ 

118 Base class for styleble objects implementing proxy and lookup functions 

119 """ 

120 

121 font = StyleDescriptor('_fonts', "fontId") 

122 fill = StyleDescriptor('_fills', "fillId") 

123 border = StyleDescriptor('_borders', "borderId") 

124 number_format = NumberFormatDescriptor() 

125 protection = StyleDescriptor('_protections', "protectionId") 

126 alignment = StyleDescriptor('_alignments', "alignmentId") 

127 style = NamedStyleDescriptor() 

128 quotePrefix = StyleArrayDescriptor('quotePrefix') 

129 pivotButton = StyleArrayDescriptor('pivotButton') 

130 

131 __slots__ = ('parent', '_style') 

132 

133 def __init__(self, sheet, style_array=None): 

134 self.parent = sheet 

135 if style_array is not None: 

136 style_array = StyleArray(style_array) 

137 self._style = style_array 

138 

139 

140 @property 

141 def style_id(self): 

142 if self._style is None: 

143 self._style = StyleArray() 

144 return self.parent.parent._cell_styles.add(self._style) 

145 

146 

147 @property 

148 def has_style(self): 

149 if self._style is None: 

150 return False 

151 return any(self._style) 

152