def __toJSONDict(mo, includeAllProps=False, excludeChildren=False): """Create a dictionary from a Mo. Args: mo (cobra.mit.mo.MO): The managed object to represent as a dictionary. includeAllProps (bool, optional): If True all of the Mo's properties will be included in the Mo, otherwise only the naming properties and properties marked dirty will be included. The default is False. excludeChildren (bool): If True the children will not be included in the resulting dictionary, otherwise the children are included. The default is False. Returns: dict: The Mo as a python dictionary. """ meta = mo.meta className = meta.moClassName moDict = {} attrDict = {} for propMeta in meta.props: moPropName = propMeta.moPropName value = getPropValue(mo, propMeta, includeAllProps) if value is not None: attrDict[moPropName] = {} attrDict[moPropName] = str(value) if len(attrDict) > 0: moDict['attributes'] = attrDict else: moDict['attributes'] = {} if not excludeChildren: childrenArray = [] for childMo in mo.children: childMoDict = __toJSONDict(childMo, includeAllProps, excludeChildren) childrenArray.append(childMoDict) if len(childrenArray) > 0: moDict['children'] = childrenArray return {className: moDict}
def _toXMLStr(mo, includeAllProps, excludeChildren=False): """Create a XML string from a Mo without the XML header. This method does not provide the ability to pretty print the XML string. Args: mo (cobra.mit.mo.Mo): The Mo that should be represented by the XML string. includeAllProps (bool): Include all properties if True, only include naming properties and properties that are marked dirty. excludeChildren (bool, optional): Do not include children Mo's if True, do include children Mo's otherwise. The default is False. Returns: str: The XML string derived from the mo. """ def encodeValue(xmlValue): """Encode a string into XML entities. Args: xmlValue (str): The string to encode. Returns: str: The xmlValue encoded to xml entities where needed. """ newValue = [] for xmlChar in xmlValue: if xmlChar == '&': newValue.append('&') elif xmlChar == '"': newValue.append('"') elif xmlChar == "'": newValue.append(''') elif xmlChar == ">": newValue.append('>') elif xmlChar == "<": newValue.append('<') else: newValue.append(xmlChar) return ''.join(newValue) meta = mo.meta className = meta.moClassName # Create the attribute string attrStr = "" for propMeta in meta.props: moPropName = propMeta.moPropName value = getPropValue(mo, propMeta, includeAllProps) if value is not None: value = encodeValue(str(value)) attrStr = attrStr + " {0}='{1}'".format(moPropName, value) childXml = "" if not excludeChildren: for childMo in mo.children: childXml += _toXMLStr(childMo, includeAllProps, excludeChildren) xmlStr = "" if attrStr or childXml: xmlStr = "<{0}{1}>{2}</{0}>".format(className, attrStr, childXml) else: xmlStr = "<{0}></{0}>".format(className) return xmlStr
def _toXMLStr(mo, includeAllProps, excludeChildren=False): """Create a XML string from a Mo without the XML header. This method does not provide the ability to pretty print the XML string. Args: mo (cobra.mit.mo.Mo): The Mo that should be represented by the XML string. includeAllProps (bool): Include all properties if True, only include naming properties and properties that are marked dirty. excludeChildren (bool, optional): Do not include children Mo's if True, do include children Mo's otherwise. The default is False. Returns: str: The XML string derived from the mo. """ def encodeValue(xmlValue): """Encode a string into XML entities. Args: xmlValue (str): The string to encode. Returns: str: The xmlValue encoded to xml entities where needed. """ newValue = [] for xmlChar in xmlValue: if xmlChar == "&": newValue.append("&") elif xmlChar == '"': newValue.append(""") elif xmlChar == "'": newValue.append("'") elif xmlChar == ">": newValue.append(">") elif xmlChar == "<": newValue.append("<") else: newValue.append(xmlChar) return "".join(newValue) meta = mo.meta className = meta.moClassName # Create the attribute string attrStr = "" for propMeta in meta.props: moPropName = propMeta.moPropName value = getPropValue(mo, propMeta, includeAllProps) if value is not None: value = encodeValue(str(value)) attrStr = attrStr + " {0}='{1}'".format(moPropName, value) childXml = "" if not excludeChildren: for childMo in mo.children: childXml += _toXMLStr(childMo, includeAllProps, excludeChildren) xmlStr = "" if attrStr or childXml: xmlStr = "<{0}{1}>{2}</{3}>".format(className, attrStr, childXml, className) return xmlStr