data_source.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. """
  2. Collection of utility primitives for charts.
  3. """
  4. from openpyxl.compat import unicode
  5. from openpyxl.descriptors.serialisable import Serialisable
  6. from openpyxl.descriptors import (
  7. Bool,
  8. Typed,
  9. Alias,
  10. String,
  11. Integer,
  12. Sequence,
  13. )
  14. from openpyxl.descriptors.excel import ExtensionList
  15. from openpyxl.descriptors.nested import (
  16. NestedString,
  17. NestedText,
  18. NestedInteger,
  19. )
  20. class NumFmt(Serialisable):
  21. formatCode = String()
  22. sourceLinked = Bool()
  23. def __init__(self,
  24. formatCode=None,
  25. sourceLinked=False
  26. ):
  27. self.formatCode = formatCode
  28. self.sourceLinked = sourceLinked
  29. class NumberValueDescriptor(NestedText):
  30. """
  31. Data should be numerical but isn't always :-/
  32. """
  33. allow_none = True
  34. def __set__(self, instance, value):
  35. if value == "#N/A":
  36. self.expected_type = unicode
  37. else:
  38. self.expected_type = float
  39. super(NumberValueDescriptor, self).__set__(instance, value)
  40. class NumVal(Serialisable):
  41. idx = Integer()
  42. formatCode = NestedText(allow_none=True, expected_type=unicode)
  43. v = NumberValueDescriptor()
  44. def __init__(self,
  45. idx=None,
  46. formatCode=None,
  47. v=None,
  48. ):
  49. self.idx = idx
  50. self.formatCode = formatCode
  51. self.v = v
  52. class NumData(Serialisable):
  53. formatCode = NestedText(expected_type=unicode, allow_none=True)
  54. ptCount = NestedInteger(allow_none=True)
  55. pt = Sequence(expected_type=NumVal)
  56. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  57. __elements__ = ('formatCode', 'ptCount', 'pt')
  58. def __init__(self,
  59. formatCode=None,
  60. ptCount=None,
  61. pt=(),
  62. extLst=None,
  63. ):
  64. self.formatCode = formatCode
  65. self.ptCount = ptCount
  66. self.pt = pt
  67. class NumRef(Serialisable):
  68. f = NestedText(expected_type=unicode)
  69. ref = Alias('f')
  70. numCache = Typed(expected_type=NumData, allow_none=True)
  71. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  72. __elements__ = ('f', 'numCache')
  73. def __init__(self,
  74. f=None,
  75. numCache=None,
  76. extLst=None,
  77. ):
  78. self.f = f
  79. self.numCache = numCache
  80. class StrVal(Serialisable):
  81. tagname = "strVal"
  82. idx = Integer()
  83. v = NestedText(expected_type=unicode)
  84. def __init__(self,
  85. idx=0,
  86. v=None,
  87. ):
  88. self.idx = idx
  89. self.v = v
  90. class StrData(Serialisable):
  91. tagname = "strData"
  92. ptCount = NestedInteger(allow_none=True)
  93. pt = Sequence(expected_type=StrVal)
  94. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  95. __elements__ = ('ptCount', 'pt')
  96. def __init__(self,
  97. ptCount=None,
  98. pt=(),
  99. extLst=None,
  100. ):
  101. self.ptCount = ptCount
  102. self.pt = pt
  103. class StrRef(Serialisable):
  104. tagname = "strRef"
  105. f = NestedText(expected_type=unicode, allow_none=True)
  106. strCache = Typed(expected_type=StrData, allow_none=True)
  107. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  108. __elements__ = ('f', 'strCache')
  109. def __init__(self,
  110. f=None,
  111. strCache=None,
  112. extLst=None,
  113. ):
  114. self.f = f
  115. self.strCache = strCache
  116. class NumDataSource(Serialisable):
  117. numRef = Typed(expected_type=NumRef, allow_none=True)
  118. numLit = Typed(expected_type=NumData, allow_none=True)
  119. def __init__(self,
  120. numRef=None,
  121. numLit=None,
  122. ):
  123. self.numRef = numRef
  124. self.numLit = numLit
  125. class Level(Serialisable):
  126. tagname = "lvl"
  127. pt = Sequence(expected_type=StrVal)
  128. __elements__ = ('pt',)
  129. def __init__(self,
  130. pt=(),
  131. ):
  132. self.pt = pt
  133. class MultiLevelStrData(Serialisable):
  134. tagname = "multiLvlStrData"
  135. ptCount = Integer(allow_none=True)
  136. lvl = Sequence(expected_type=Level)
  137. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  138. __elements__ = ('ptCount', 'lvl',)
  139. def __init__(self,
  140. ptCount=None,
  141. lvl=(),
  142. extLst=None,
  143. ):
  144. self.ptCount = ptCount
  145. self.lvl = lvl
  146. class MultiLevelStrRef(Serialisable):
  147. tagname = "multiLvlStrRef"
  148. f = NestedText(expected_type=unicode)
  149. multiLvlStrCache = Typed(expected_type=MultiLevelStrData, allow_none=True)
  150. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  151. __elements__ = ('multiLvlStrCache', 'f')
  152. def __init__(self,
  153. f=None,
  154. multiLvlStrCache=None,
  155. extLst=None,
  156. ):
  157. self.f = f
  158. self.multiLvlStrCache = multiLvlStrCache
  159. class AxDataSource(Serialisable):
  160. tagname = "cat"
  161. numRef = Typed(expected_type=NumRef, allow_none=True)
  162. numLit = Typed(expected_type=NumData, allow_none=True)
  163. strRef = Typed(expected_type=StrRef, allow_none=True)
  164. strLit = Typed(expected_type=StrData, allow_none=True)
  165. multiLvlStrRef = Typed(expected_type=MultiLevelStrRef, allow_none=True)
  166. def __init__(self,
  167. numRef=None,
  168. numLit=None,
  169. strRef=None,
  170. strLit=None,
  171. multiLvlStrRef=None,
  172. ):
  173. if not any([numLit, numRef, strRef, strLit, multiLvlStrRef]):
  174. raise TypeError("A data source must be provided")
  175. self.numRef = numRef
  176. self.numLit = numLit
  177. self.strRef = strRef
  178. self.strLit = strLit
  179. self.multiLvlStrRef = multiLvlStrRef