1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-22 17:39:59 -05:00
vsphere-automation-sdk-python/vsphere/6.0/_modules/vmware/vapi/lib/converter.html
2018-08-07 16:42:25 -07:00

240 lines
16 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.lib.converter &mdash; vCloud Suite SDK for Python 6.0.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.0.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="vCloud Suite SDK for Python 6.0.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">vCloud Suite SDK for Python 6.0.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.lib.converter</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Convenience methods for converting variable names</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 2011 VMware, Inc. All rights reserved. -- VMware Confidential&#39;</span>
<span class="kn">import</span> <span class="nn">keyword</span>
<span class="kn">from</span> <span class="nn">re</span> <span class="kn">import</span> <span class="nb">compile</span> <span class="k">as</span> <span class="n">compile_regex</span>
<div class="viewcode-block" id="Converter"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter">[docs]</a><span class="k">class</span> <span class="nc">Converter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convenience methods for converting variable names</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">_mixedcase_to_underscore</span> <span class="o">=</span> <span class="n">compile_regex</span><span class="p">(</span>
<span class="s">r&#39;(?&lt;=[a-z])[A-Z]|(?&lt;!^)[A-Z](?=[a-z])&#39;</span><span class="p">)</span>
<span class="n">_underscore_to_mixedcase</span> <span class="o">=</span> <span class="n">compile_regex</span><span class="p">(</span><span class="s">r&#39;_([A-Za-z])&#39;</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.capitalize"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.capitalize">[docs]</a> <span class="k">def</span> <span class="nf">capitalize</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Capitalize the first letter of the name</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name to be converted</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name with first letter capitalized</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">+</span> <span class="n">name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.uncapitalize"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.uncapitalize">[docs]</a> <span class="k">def</span> <span class="nf">uncapitalize</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Uncapitalize the first letter of the name</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name to be converted</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name with first letter uncapitalized</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">+</span> <span class="n">name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.mixedcase_to_underscore"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.mixedcase_to_underscore">[docs]</a> <span class="k">def</span> <span class="nf">mixedcase_to_underscore</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert from mixedCase to lower_case_with_underscore format</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name in mixedCase format</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name in lower_case_with_underscore format</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">_mixedcase_to_underscore</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&quot;_\g&lt;0&gt;&quot;</span><span class="p">,</span>
<span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.underscore_to_mixedcase"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.underscore_to_mixedcase">[docs]</a> <span class="k">def</span> <span class="nf">underscore_to_mixedcase</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert from lower_case_with_underscore to mixedCase format</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name in lower_case_with_underscore</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name in mixedCase</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">_underscore_to_mixedcase</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="k">lambda</span> <span class="n">m</span><span class="p">:</span>
<span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()),</span>
<span class="n">name</span><span class="p">)</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.capwords_to_underscore"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.capwords_to_underscore">[docs]</a> <span class="k">def</span> <span class="nf">capwords_to_underscore</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert from CapWords to lower_case_with_underscore format</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name in CapWords format</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name in lower_case_with_underscore format</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">mixedcase_to_underscore</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.underscore_to_capwords"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.underscore_to_capwords">[docs]</a> <span class="k">def</span> <span class="nf">underscore_to_capwords</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Convert from lower_case_with_underscore to CapWords format</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: name in lower_case_with_underscore</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: name in CapWords</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">Converter</span><span class="o">.</span><span class="n">underscore_to_mixedcase</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">capitalize</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.unreserve_name"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.unreserve_name">[docs]</a> <span class="k">def</span> <span class="nf">unreserve_name</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts the argument if it clashes with a python keyword. If the string</span>
<span class="sd"> matches a keyword, adds a trailing underscore, else it returns the same</span>
<span class="sd"> string</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: The string to be converted</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: The converted string</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">keyword</span><span class="o">.</span><span class="n">iskeyword</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="ow">or</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;self&#39;</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">name</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">name</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.pepify"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.pepify">[docs]</a> <span class="k">def</span> <span class="nf">pepify</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts the argument into a name that conforms to PEP8 standard.</span>
<span class="sd"> i.e. lower_case_with_underscore</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: The string to be converted</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: The converted string</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">Converter</span><span class="o">.</span><span class="n">mixedcase_to_underscore</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">unreserve_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Converter.canonical_to_pep"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.converter.Converter.canonical_to_pep">[docs]</a> <span class="k">def</span> <span class="nf">canonical_to_pep</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Converts the argument from vAPI canonical format to PEP8 compliant</span>
<span class="sd"> parameter or attribute name</span>
<span class="sd"> :type name: :class:`str`</span>
<span class="sd"> :param name: The string to be converted</span>
<span class="sd"> :rtype: :class:`str`</span>
<span class="sd"> :return: The converted string</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">Converter</span><span class="o">.</span><span class="n">unreserve_name</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span></div></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">vCloud Suite SDK for Python 6.0.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>