1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-26 19:29:58 -05:00
vsphere-automation-sdk-python/vsphere/6.5/_modules/vmware/vapi/bindings/struct.html
2018-08-07 16:42:25 -07:00

436 lines
44 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>vmware.vapi.bindings.struct &mdash; vSphere Automation SDK for Python 6.5.0 documentation</title>
<link rel="stylesheet" href="../../../../_static/vapitheme.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../../',
VERSION: '6.5.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../../_static/doctools.js"></script>
<link rel="top" title="vSphere Automation SDK for Python 6.5.0 documentation" href="../../../../index.html" />
<link rel="up" title="Module code" href="../../../index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../../index.html">vSphere Automation SDK for Python 6.5.0 documentation</a> &raquo;</li>
<li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for vmware.vapi.bindings.struct</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Bindings data classes</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s">&#39;VMware, Inc.&#39;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&#39;Copyright (c) 2015 VMware, Inc. All rights reserved.&#39;</span>
<span class="kn">import</span> <span class="nn">decimal</span>
<span class="kn">import</span> <span class="nn">six</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">try</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">simplejson</span> <span class="kn">as</span> <span class="nn">json</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.bindings.common</span> <span class="kn">import</span> <span class="n">raise_core_exception</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.data.serializers</span> <span class="kn">import</span> <span class="n">cleanjson</span>
<span class="c"># TODO: Split this into static and dynamic structures.</span>
<div class="viewcode-block" id="VapiStruct"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct">[docs]</a><span class="k">class</span> <span class="nc">VapiStruct</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Representation of IDL Structure in python language bindings</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">_validator_list</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mappings</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">struct_value</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Initialize VapiStruct</span>
<span class="sd"> :type mappings: :class:`dict` or :class:`None`</span>
<span class="sd"> :param mappings: A mapping for all field names whose IDL name does</span>
<span class="sd"> not match PEP8 standard name</span>
<span class="sd"> :type struct_value: :class:`vmware.vapi.data.value.StructValue`</span>
<span class="sd"> :param struct_value: StructValue to be used for VapiStruct or :class:`None`</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_mappings</span> <span class="o">=</span> <span class="n">mappings</span>
<span class="c"># fields will either be in native form or in unknown</span>
<span class="c"># fields</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_unexpected_fields</span> <span class="o">=</span> <span class="bp">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_struct_value</span> <span class="o">=</span> <span class="n">struct_value</span>
<div class="viewcode-block" id="VapiStruct.get_field"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.get_field">[docs]</a> <span class="k">def</span> <span class="nf">get_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the struct field value</span>
<span class="sd"> :type attr: :class:`str`</span>
<span class="sd"> :param attr: Field name as defined in IDL</span>
<span class="sd"> :rtype: :class:`object`</span>
<span class="sd"> :return: Field value</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mappings</span> <span class="ow">and</span> <span class="n">attr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mappings</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mappings</span><span class="p">[</span><span class="n">attr</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">)</span>
</div>
<span class="nd">@classmethod</span>
<div class="viewcode-block" id="VapiStruct.validate_struct_value"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.validate_struct_value">[docs]</a> <span class="k">def</span> <span class="nf">validate_struct_value</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">struct_value</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Validate if the given struct value satisfies all</span>
<span class="sd"> the constraints of this VapiStruct.</span>
<span class="sd"> :type struct_value: :class:`vmware.vapi.data.value.StructValue`</span>
<span class="sd"> :param struct_value: StructValue to be validated</span>
<span class="sd"> :type validators: :class:`list` of :class:`vmware.vapi.data.validator.Validator`</span>
<span class="sd"> :param validators: List of validators</span>
<span class="sd"> :raise :class:`vmware.vapi.exception.CoreException` if a constraint is</span>
<span class="sd"> not satisfied</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">cls</span><span class="o">.</span><span class="n">_validator_list</span><span class="p">:</span>
<span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">cls</span><span class="o">.</span><span class="n">_validator_list</span><span class="p">:</span>
<span class="n">msg_list</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">struct_value</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<span class="n">raise_core_exception</span><span class="p">(</span><span class="n">msg_list</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="VapiStruct.validate_constraints"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.validate_constraints">[docs]</a> <span class="k">def</span> <span class="nf">validate_constraints</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Validate if the current VapiStruct instance satisfies all the</span>
<span class="sd"> constraints of this VapiStruct type.</span>
<span class="sd"> :raise :class:`vmware.vapi.exception.CoreException` if a constraint is</span>
<span class="sd"> not satisfied</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">struct_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_struct_value</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">validate_struct_value</span><span class="p">(</span><span class="n">struct_value</span><span class="p">)</span>
</div>
<span class="nd">@classmethod</span>
<div class="viewcode-block" id="VapiStruct.get_binding_type"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.get_binding_type">[docs]</a> <span class="k">def</span> <span class="nf">get_binding_type</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the corresponding BindingType for the VapiStruct class</span>
<span class="sd"> :rtype: :class:`vmware.vapi.bindings.type.BindingType`</span>
<span class="sd"> :return: BindingType for this VapiStruct</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;_binding_type&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
</div>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">_set_binding_type</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">binding_type</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Set the underlying BindingType for this VapiStruct.</span>
<span class="sd"> :type binding_type: :class:`vmware.vapi.bindings.type.BindingType`</span>
<span class="sd"> :param binding_type: BindingType for this VapiStruct</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">cls</span><span class="o">.</span><span class="n">_binding_type</span> <span class="o">=</span> <span class="n">binding_type</span>
<div class="viewcode-block" id="VapiStruct.get_struct_value"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.get_struct_value">[docs]</a> <span class="k">def</span> <span class="nf">get_struct_value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the corresponding StructValue for the VapiStruct class</span>
<span class="sd"> :rtype: :class:`vmware.vapi.data.value.StructValue`</span>
<span class="sd"> :return: StructValue for this VapiStruct</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># For dynamic structures</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_struct_value</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_struct_value</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># For static structures</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.bindings.converter</span> <span class="kn">import</span> <span class="n">TypeConverter</span>
<span class="n">struct_value</span> <span class="o">=</span> <span class="n">TypeConverter</span><span class="o">.</span><span class="n">convert_to_vapi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_binding_type</span><span class="p">)</span>
<span class="n">unexpected_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_unexpected_fields</span> <span class="ow">or</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">unexpected_fields</span><span class="p">):</span>
<span class="n">struct_value</span><span class="o">.</span><span class="n">set_field</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="k">return</span> <span class="n">struct_value</span>
</div>
<span class="k">def</span> <span class="nf">_set_unexpected_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unexpected_fields</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Set the underlying StructValue for this VapiStruct. This is</span>
<span class="sd"> an internal method and should only be used by vAPI runtime.</span>
<span class="sd"> :type unexpected_fields: :class:`dict` of :class:`str` and</span>
<span class="sd"> :class:`vmware.vapi.data.value.DataValue` or :class:`None`</span>
<span class="sd"> :param struct_value: StructValue for this VapiStruct</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_unexpected_fields</span> <span class="o">=</span> <span class="n">unexpected_fields</span>
<div class="viewcode-block" id="VapiStruct.convert_to"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.convert_to">[docs]</a> <span class="k">def</span> <span class="nf">convert_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cls</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert the underlying StructValue to an instance of the provided class</span>
<span class="sd"> if possible. Conversion will be possible if the StructValue contains</span>
<span class="sd"> all the fields expected by the provided class and the type of the value</span>
<span class="sd"> in each fields matches the type of the field expected by the provided</span>
<span class="sd"> class.</span>
<span class="sd"> :type cls: :class:`vmware.vapi.data.value.StructValue`</span>
<span class="sd"> :param cls: The type to convert to</span>
<span class="sd"> :rtype: :class:&#39;vmware.vapi.bindings.struct.VapiStruct&#39;</span>
<span class="sd"> :return: The converted value</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.bindings.converter</span> <span class="kn">import</span> <span class="n">TypeConverter</span>
<span class="k">return</span> <span class="n">TypeConverter</span><span class="o">.</span><span class="n">convert_to_python</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_struct_value</span><span class="p">(),</span>
<span class="n">cls</span><span class="o">.</span><span class="n">get_binding_type</span><span class="p">())</span>
</div>
<div class="viewcode-block" id="VapiStruct.to_json"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.to_json">[docs]</a> <span class="k">def</span> <span class="nf">to_json</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert the object into a json string.</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: JSON string representation of this object</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">struct_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_struct_value</span><span class="p">()</span>
<span class="k">return</span> <span class="n">cleanjson</span><span class="o">.</span><span class="n">DataValueConverter</span><span class="o">.</span><span class="n">convert_to_json</span><span class="p">(</span><span class="n">struct_value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="VapiStruct.to_dict"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.VapiStruct.to_dict">[docs]</a> <span class="k">def</span> <span class="nf">to_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert the object into a python dictionary. Even the nested types</span>
<span class="sd"> are converted to dictionaries.</span>
<span class="sd"> :rtype: :class:`dict`</span>
<span class="sd"> :return: Dictionary representation of this object</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># TODO: Implement native converter from DataValue -&gt; Dictionary</span>
<span class="c"># to improve performance if it is used heavily</span>
<span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_json</span><span class="p">(),</span> <span class="n">parse_float</span><span class="o">=</span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">)</span>
</div>
<span class="k">def</span> <span class="nf">_get_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the attributes of the vAPI structure object</span>
<span class="sd"> :rtype: :class:`list` of :class:`str`</span>
<span class="sd"> :return: List of attributes of this object</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c"># Using getmembers in inspect to return all the attributes</span>
<span class="c"># of this object. And later filter those to get only the</span>
<span class="c"># public data attributes</span>
<span class="k">return</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iterkeys</span><span class="p">(</span><span class="nb">vars</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;_&#39;</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="k">if</span> <span class="n">other</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">False</span>
<span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_attrs</span><span class="p">():</span>
<span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">False</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">class_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
<span class="n">attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_attrs</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
<span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">)))</span>
<span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attrs</span><span class="p">])</span>
<span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">class_name</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_attrs</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
<span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> : </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">)))</span>
<span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">attrs</span><span class="p">])</span>
<span class="k">return</span> <span class="s">&#39;{</span><span class="si">%s</span><span class="s">}&#39;</span> <span class="o">%</span> <span class="n">result</span>
<span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__hash__</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="PrettyPrinter"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.PrettyPrinter">[docs]</a><span class="k">class</span> <span class="nc">PrettyPrinter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Helper class to pretty print Python native values (with special support</span>
<span class="sd"> for VapiStruct objects).</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Initialize PrettyPrinter</span>
<span class="sd"> :type stream: :class:`object`</span>
<span class="sd"> :param stream: A stream object that implements File protocol&#39;s</span>
<span class="sd"> write operation</span>
<span class="sd"> :type indent: :class:`int`</span>
<span class="sd"> :param indent: Indentation to be used for new lines</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_stream</span> <span class="o">=</span> <span class="n">stream</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_indent</span> <span class="o">=</span> <span class="n">indent</span>
<div class="viewcode-block" id="PrettyPrinter.pprint"><a class="viewcode-back" href="../../../../vmware.vapi.bindings.html#vmware.vapi.bindings.struct.PrettyPrinter.pprint">[docs]</a> <span class="k">def</span> <span class="nf">pprint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Print a Python native value</span>
<span class="sd"> :type value: :class:`vmware.vapi.bindings.struct.VapiStruct`</span>
<span class="sd"> :param value: VapiStruct to be pretty printed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_process_value</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>
</div>
<span class="k">def</span> <span class="nf">_print_level</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="p">,</span> <span class="n">newline</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Print data at a given identation level</span>
<span class="sd"> :type value: :class:`str`</span>
<span class="sd"> :param value: String to be printed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> :type newline: :class:`bool`</span>
<span class="sd"> :param newline: If true, prints a new line after the data. If false,</span>
<span class="sd"> only prints the data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">level</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39; &#39;</span> <span class="o">*</span> <span class="n">level</span> <span class="o">+</span> <span class="n">value</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="n">newline</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_process_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Process a value</span>
<span class="sd"> :type value: :class:`object`</span>
<span class="sd"> :param value: Value to be processed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">VapiStruct</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_pprint_struct</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">level</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_pprint_dict</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">level</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_pprint_list</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">level</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;,&quot;</span> <span class="o">%</span> <span class="n">value</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">integer_types</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">,&#39;</span> <span class="o">%</span> <span class="n">value</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;None,&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">,&#39;</span> <span class="o">%</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_pprint_struct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Pretty print a struct</span>
<span class="sd"> :type value: :class:`vmware.vapi.bindings.struct.VapiStruct`</span>
<span class="sd"> :param value: Value to be processed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">class_name</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="n">class_name</span> <span class="o">+</span> <span class="s">&#39;(&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">_get_attrs</span><span class="p">()):</span> <span class="c"># pylint: disable=W0212</span>
<span class="n">v</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=&#39;</span> <span class="o">%</span> <span class="n">k</span><span class="p">,</span> <span class="n">level</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_process_value</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;),&#39;</span><span class="p">,</span> <span class="n">level</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_pprint_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Pretty print a dictionary</span>
<span class="sd"> :type value: :class:`dict`</span>
<span class="sd"> :param value: Value to be processed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;{},&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">return</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;{&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&quot;&#39;</span><span class="si">%s</span><span class="s">&#39;:&quot;</span> <span class="o">%</span> <span class="n">k</span><span class="p">,</span> <span class="n">level</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_process_value</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">level</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;},&#39;</span><span class="p">,</span> <span class="n">level</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_pprint_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Pretty print a list</span>
<span class="sd"> :type value: :class:`list`</span>
<span class="sd"> :param value: Value to be processed</span>
<span class="sd"> :type level: :class:`int`</span>
<span class="sd"> :param level: Indentation level</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;[],&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">return</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;[&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">level</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_process_value</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_level</span><span class="p">(</span><span class="s">&#39;],&#39;</span><span class="p">,</span> <span class="n">level</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_indent</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../../index.html">vSphere Automation SDK for Python 6.5.0 documentation</a> &raquo;</li>
<li><a href="../../../index.html" >Module code</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2014, VMware, Inc..
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>