For working with OVAL documents. That interaction will entail the use of the other classes. Can be used to find certain elements within the document, update the document, and save the changes to a file
| 113 | |
| 114 | |
| 115 | class OvalDocument(object): |
| 116 | """ |
| 117 | For working with OVAL documents. That interaction will entail the use of the other classes. |
| 118 | Can be used to find certain elements within the document, update the document, and save the changes to a file |
| 119 | """ |
| 120 | |
| 121 | # A time format to match what OVAL expects |
| 122 | TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z" |
| 123 | |
| 124 | NS_DEFAULT = {"def": "http://oval.mitre.org/XMLSchema/oval-definitions-5"} |
| 125 | NS_OVAL = {"oval": "http://oval.mitre.org/XMLSchema/oval-common-5"} |
| 126 | NS_XSI = {"xsi": "http://www.w3.org/2001/XMLSchema-instance"} |
| 127 | |
| 128 | # xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" |
| 129 | # xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" |
| 130 | # xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 131 | # xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd |
| 132 | # http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd |
| 133 | # http://oval.mitre.org/XMLSchema/oval-definitions-5#solaris solaris-definitions-schema.xsd |
| 134 | # http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd |
| 135 | # http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd">^M |
| 136 | |
| 137 | @staticmethod |
| 138 | def indent(elem, level=0): |
| 139 | i = "\n" + level * " " |
| 140 | if len(elem): |
| 141 | if not elem.text or not elem.text.strip(): |
| 142 | elem.text = i + " " |
| 143 | if not elem.tail or not elem.tail.strip(): |
| 144 | elem.tail = i |
| 145 | for elem in elem: |
| 146 | OvalDocument.indent(elem, level + 1) |
| 147 | if not elem.tail or not elem.tail.strip(): |
| 148 | elem.tail = i |
| 149 | else: |
| 150 | if level and (not elem.tail or not elem.tail.strip()): |
| 151 | elem.tail = i |
| 152 | |
| 153 | @staticmethod |
| 154 | def getOvalTimestamp(timestamp=None): |
| 155 | """Renders a datetime to a string formatted according to the OVAL specification. |
| 156 | if the timestamp argument is None (which it is by default) or is not of type datetime, |
| 157 | this function will return a string using the current datetime. |
| 158 | |
| 159 | @type timestamp: datetime |
| 160 | @param timestamp: A datetime to be formatted as an OVAL timestamp, or None to use the current time. |
| 161 | |
| 162 | @rtype: string |
| 163 | @return: a string formatted as per OVAL |
| 164 | """ |
| 165 | if timestamp is None or not isinstance(timestamp, datetime): |
| 166 | now = datetime.date.today() |
| 167 | return now.strftime(OvalDocument.TIME_FORMAT) |
| 168 | else: |
| 169 | return timestamp.strftime(OvalDocument.TIME_FORMAT) |
| 170 | |
| 171 | def __init__(self, tree): |
| 172 | # if not tree or not isinstance(tree, ElementTree): |