Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/openpyxl/packaging/core.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.3.3, created at 2023-12-20 06:34 +0000

1# Copyright (c) 2010-2023 openpyxl 

2 

3import datetime 

4 

5from openpyxl.descriptors import ( 

6 DateTime, 

7 Alias, 

8) 

9from openpyxl.descriptors.serialisable import Serialisable 

10from openpyxl.descriptors.nested import NestedText 

11from openpyxl.xml.functions import ( 

12 Element, 

13 QName, 

14) 

15from openpyxl.xml.constants import ( 

16 COREPROPS_NS, 

17 DCORE_NS, 

18 XSI_NS, 

19 DCTERMS_NS, 

20) 

21 

22 

23class NestedDateTime(DateTime, NestedText): 

24 

25 expected_type = datetime.datetime 

26 

27 def to_tree(self, tagname=None, value=None, namespace=None): 

28 namespace = getattr(self, "namespace", namespace) 

29 if namespace is not None: 

30 tagname = "{%s}%s" % (namespace, tagname) 

31 el = Element(tagname) 

32 if value is not None: 

33 el.text = value.isoformat(timespec="seconds") + 'Z' 

34 return el 

35 

36 

37class QualifiedDateTime(NestedDateTime): 

38 

39 """In certain situations Excel will complain if the additional type 

40 attribute isn't set""" 

41 

42 def to_tree(self, tagname=None, value=None, namespace=None): 

43 el = super(QualifiedDateTime, self).to_tree(tagname, value, namespace) 

44 el.set("{%s}type" % XSI_NS, QName(DCTERMS_NS, "W3CDTF")) 

45 return el 

46 

47 

48class DocumentProperties(Serialisable): 

49 """High-level properties of the document. 

50 Defined in ECMA-376 Par2 Annex D 

51 """ 

52 

53 tagname = "coreProperties" 

54 namespace = COREPROPS_NS 

55 

56 category = NestedText(expected_type=str, allow_none=True) 

57 contentStatus = NestedText(expected_type=str, allow_none=True) 

58 keywords = NestedText(expected_type=str, allow_none=True) 

59 lastModifiedBy = NestedText(expected_type=str, allow_none=True) 

60 lastPrinted = NestedDateTime(allow_none=True) 

61 revision = NestedText(expected_type=str, allow_none=True) 

62 version = NestedText(expected_type=str, allow_none=True) 

63 last_modified_by = Alias("lastModifiedBy") 

64 

65 # Dublin Core Properties 

66 subject = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

67 title = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

68 creator = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

69 description = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

70 identifier = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

71 language = NestedText(expected_type=str, allow_none=True, namespace=DCORE_NS) 

72 # Dublin Core Terms 

73 created = QualifiedDateTime(allow_none=True, namespace=DCTERMS_NS) 

74 modified = QualifiedDateTime(allow_none=True, namespace=DCTERMS_NS) 

75 

76 __elements__ = ("creator", "title", "description", "subject","identifier", 

77 "language", "created", "modified", "lastModifiedBy", "category", 

78 "contentStatus", "version", "revision", "keywords", "lastPrinted", 

79 ) 

80 

81 

82 def __init__(self, 

83 category=None, 

84 contentStatus=None, 

85 keywords=None, 

86 lastModifiedBy=None, 

87 lastPrinted=None, 

88 revision=None, 

89 version=None, 

90 created=None, 

91 creator="openpyxl", 

92 description=None, 

93 identifier=None, 

94 language=None, 

95 modified=None, 

96 subject=None, 

97 title=None, 

98 ): 

99 now = datetime.datetime.utcnow() 

100 self.contentStatus = contentStatus 

101 self.lastPrinted = lastPrinted 

102 self.revision = revision 

103 self.version = version 

104 self.creator = creator 

105 self.lastModifiedBy = lastModifiedBy 

106 self.modified = modified or now 

107 self.created = created or now 

108 self.title = title 

109 self.subject = subject 

110 self.description = description 

111 self.identifier = identifier 

112 self.language = language 

113 self.keywords = keywords 

114 self.category = category