2016-10-03 20:28:07 +00:00
|
|
|
<!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" />
|
|
|
|
|
2017-08-02 17:49:29 +00:00
|
|
|
<title>git_lint.option_handler — Git Lint 0.0.7 documentation</title>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2016-10-06 15:32:58 +00:00
|
|
|
<link rel="stylesheet" href="../../static/alabaster.css" type="text/css" />
|
2016-10-04 00:31:52 +00:00
|
|
|
<link rel="stylesheet" href="../../static/pygments.css" type="text/css" />
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
var DOCUMENTATION_OPTIONS = {
|
|
|
|
URL_ROOT: '../../',
|
2017-08-02 17:49:29 +00:00
|
|
|
VERSION: '0.0.7',
|
2016-10-03 20:28:07 +00:00
|
|
|
COLLAPSE_INDEX: false,
|
|
|
|
FILE_SUFFIX: '.html',
|
|
|
|
HAS_SOURCE: true
|
|
|
|
};
|
|
|
|
</script>
|
2016-10-04 00:31:52 +00:00
|
|
|
<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>
|
2017-08-02 17:49:29 +00:00
|
|
|
<link rel="top" title="Git Lint 0.0.7 documentation" href="../../index.html" />
|
2016-10-06 15:32:58 +00:00
|
|
|
<link rel="up" title="Module code" href="../index.html" />
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../../static/custom.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
|
|
|
|
|
2016-10-03 20:28:07 +00:00
|
|
|
</head>
|
2016-10-05 16:16:06 +00:00
|
|
|
<body role="document">
|
2016-10-06 15:32:58 +00:00
|
|
|
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<div class="document">
|
|
|
|
<div class="documentwrapper">
|
|
|
|
<div class="bodywrapper">
|
2016-10-05 16:16:06 +00:00
|
|
|
<div class="body" role="main">
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<h1>Source code for git_lint.option_handler</h1><div class="highlight"><pre>
|
2016-10-05 16:16:06 +00:00
|
|
|
<span></span><span class="c1"># Copyright (C) 2015 Elf M. Sternberg</span>
|
|
|
|
<span class="c1"># Author: Elf M. Sternberg</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="kn">import</span> <span class="nn">getopt</span>
|
|
|
|
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">try</span><span class="p">:</span> <span class="c1"># noqa: F401</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Text</span><span class="p">,</span> <span class="n">Any</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Callable</span><span class="p">,</span> <span class="n">Tuple</span> <span class="c1"># noqa: F401</span>
|
|
|
|
<span class="k">except</span><span class="p">:</span> <span class="c1"># noqa: F401</span>
|
|
|
|
<span class="k">pass</span> <span class="c1"># noqa: F401</span>
|
|
|
|
|
|
|
|
|
|
|
|
<span class="n">Option</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">'Option'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'short'</span><span class="p">,</span> <span class="s1">'long'</span><span class="p">,</span> <span class="s1">'takes'</span><span class="p">,</span> <span class="s1">'help'</span><span class="p">,</span> <span class="s1">'conflicts'</span><span class="p">])</span> <span class="c1"># type: str, str, str, str, List[str]</span>
|
|
|
|
<span class="n">Arguments</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">'Arguments'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'arguments'</span><span class="p">,</span> <span class="s1">'filenames'</span><span class="p">,</span> <span class="s1">'excluded'</span><span class="p">])</span> <span class="c1"># type: Dict[str, str], List[str], List[str]</span>
|
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="c1"># This was a lot shorter and smarter in Hy...</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="c1"># A lot of what you see here is separated from git_lint itself, since this will not be</span>
|
|
|
|
<span class="c1"># relevant to the operation of pre-commit.</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="c1"># ___ _ _ _</span>
|
|
|
|
<span class="c1"># / __|___ _ __ _ __ __ _ _ _ __| | | | (_)_ _ ___</span>
|
|
|
|
<span class="c1"># | (__/ _ \ ' \| ' \/ _` | ' \/ _` | | |__| | ' \/ -_)</span>
|
|
|
|
<span class="c1"># \___\___/_|_|_|_|_|_\__,_|_||_\__,_| |____|_|_||_\___|</span>
|
|
|
|
<span class="c1">#</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
<div class="viewcode-block" id="cleanup_options"><a class="viewcode-back" href="../../git_lint.html#git_lint.option_handler.cleanup_options">[docs]</a><span class="k">def</span> <span class="nf">cleanup_options</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">commandline</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="c1"># type: (List[Option], List[str]) -> Arguments</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="sd">"""Takes a table of options and the commandline, and returns a</span>
|
|
|
|
<span class="sd"> dictionary of those options that appear on the commandline</span>
|
|
|
|
<span class="sd"> along with any extra arguments.</span>
|
|
|
|
|
|
|
|
<span class="sd"> :param List(Tuple (string, string, boolean, string, List(string))) options,</span>
|
|
|
|
<span class="sd"> The table of options: One-letter option, long option, takes arguments,</span>
|
|
|
|
<span class="sd"> Help text, list of (long) options superseded by this one.</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="sd"> : param List(strings) commandline</span>
|
|
|
|
<span class="sd"> The arguments as received by the start-up process</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
|
|
|
|
<span class="sd"> : returns List(strings), List(strings), List(strings)</span>
|
|
|
|
<span class="sd"> The longopt dictionary of arguments and associated values (if any)</span>
|
|
|
|
<span class="sd"> The list of filenames left after argument processing</span>
|
|
|
|
<span class="sd"> The longopt list of arguments that were excluded by argument precedence</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="sd"> """</span>
|
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">make_option_streamliner</span><span class="p">(</span><span class="n">options</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="c1"># type: (List[Option]) -> Callable[[Dict[str, str], Option], Dict[str, str]]</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<span class="sd">"""Takes a list of option tuples, and returns a function that takes</span>
|
|
|
|
<span class="sd"> the output of getopt and reduces it to the longopt key and</span>
|
|
|
|
<span class="sd"> associated values as a dictionary.</span>
|
|
|
|
<span class="sd"> """</span>
|
|
|
|
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="n">fullset</span> <span class="o">=</span> <span class="p">{}</span> <span class="c1"># type: Dict[str, str]</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">if</span> <span class="n">option</span><span class="o">.</span><span class="n">long</span><span class="p">:</span>
|
|
|
|
<span class="n">fullset</span><span class="p">[</span><span class="s1">'--'</span> <span class="o">+</span> <span class="n">option</span><span class="o">.</span><span class="n">long</span><span class="p">]</span> <span class="o">=</span> <span class="n">option</span><span class="o">.</span><span class="n">long</span>
|
|
|
|
<span class="k">if</span> <span class="n">option</span><span class="o">.</span><span class="n">short</span><span class="p">:</span>
|
|
|
|
<span class="n">fullset</span><span class="p">[</span><span class="s1">'-'</span> <span class="o">+</span> <span class="n">option</span><span class="o">.</span><span class="n">short</span><span class="p">]</span> <span class="o">=</span> <span class="n">option</span><span class="o">.</span><span class="n">long</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">streamliner</span><span class="p">(</span><span class="n">acc</span><span class="p">,</span> <span class="n">it</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="c1"># type: (Dict[str, str], Option) -> Dict[str, str]</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="n">acc</span><span class="p">[</span><span class="n">fullset</span><span class="p">[</span><span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">]]]</span> <span class="o">=</span> <span class="n">it</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
<span class="k">return</span> <span class="n">acc</span>
|
|
|
|
|
|
|
|
<span class="k">return</span> <span class="n">streamliner</span>
|
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">remove_conflicted_options</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="c1"># type: (List[Option], Dict[str, str]) -> Tuple[List[str], List[str]]</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="sd">"""Takes our list of option tuples, and a cleaned copy of what was</span>
|
|
|
|
<span class="sd"> requested from getopt, and returns a copy of the request</span>
|
|
|
|
<span class="sd"> without any options that are marked as superseded, along with</span>
|
|
|
|
<span class="sd"> the list of superseded options</span>
|
|
|
|
<span class="sd"> """</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">def</span> <span class="nf">get_excluded_keys</span><span class="p">(</span><span class="n">memo</span><span class="p">,</span> <span class="n">option</span><span class="p">):</span>
|
|
|
|
<span class="k">return</span> <span class="n">memo</span> <span class="o">+</span> <span class="n">option</span><span class="o">.</span><span class="n">conflicts</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<span class="n">keys</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="n">marked</span> <span class="o">=</span> <span class="p">[</span><span class="n">option</span> <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">options</span> <span class="k">if</span> <span class="n">option</span><span class="o">.</span><span class="n">long</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">]</span>
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="n">exclude</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">get_excluded_keys</span><span class="p">,</span> <span class="n">marked</span><span class="p">,</span> <span class="p">[])</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="n">excluded</span> <span class="o">=</span> <span class="p">[</span><span class="n">key</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">]</span>
|
|
|
|
<span class="n">cleaned</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">request</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span>
|
|
|
|
<span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">excluded</span><span class="p">}</span>
|
|
|
|
<span class="k">return</span> <span class="p">(</span><span class="n">cleaned</span><span class="p">,</span> <span class="n">excluded</span><span class="p">)</span>
|
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">shortoptstogo</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">return</span> <span class="n">i</span><span class="o">.</span><span class="n">short</span> <span class="o">+</span> <span class="p">((</span><span class="n">i</span><span class="o">.</span><span class="n">takes</span> <span class="ow">and</span> <span class="s1">':'</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">''</span><span class="p">)</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">longoptstogo</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">return</span> <span class="n">i</span><span class="o">.</span><span class="n">long</span> <span class="o">+</span> <span class="p">((</span><span class="n">i</span><span class="o">.</span><span class="n">takes</span> <span class="ow">and</span> <span class="s1">'='</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">''</span><span class="p">)</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="n">optstringsshort</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">shortoptstogo</span><span class="p">(</span><span class="n">opt</span><span class="p">)</span> <span class="k">for</span> <span class="n">opt</span> <span class="ow">in</span> <span class="n">options</span> <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">short</span><span class="p">])</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="n">optstringslong</span> <span class="o">=</span> <span class="p">[</span><span class="n">longoptstogo</span><span class="p">(</span><span class="n">opt</span><span class="p">)</span> <span class="k">for</span> <span class="n">opt</span> <span class="ow">in</span> <span class="n">options</span><span class="p">]</span>
|
|
|
|
<span class="p">(</span><span class="n">chosen_options</span><span class="p">,</span> <span class="n">filenames</span><span class="p">)</span> <span class="o">=</span> <span class="n">getopt</span><span class="o">.</span><span class="n">getopt</span><span class="p">(</span><span class="n">commandline</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span>
|
|
|
|
<span class="n">optstringsshort</span><span class="p">,</span>
|
|
|
|
<span class="n">optstringslong</span><span class="p">)</span>
|
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="c1"># Turns what getopt returns into something more human-readable</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="n">streamline_options</span> <span class="o">=</span> <span class="n">make_option_streamliner</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
|
|
|
|
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="c1"># Remove any options that are superseded by others.</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
<span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">excluded</span><span class="p">)</span> <span class="o">=</span> <span class="n">remove_conflicted_options</span><span class="p">(</span>
|
2016-10-05 16:16:06 +00:00
|
|
|
<span class="n">options</span><span class="p">,</span> <span class="n">reduce</span><span class="p">(</span><span class="n">streamline_options</span><span class="p">,</span> <span class="n">chosen_options</span><span class="p">,</span> <span class="p">{}))</span>
|
2016-10-03 20:28:07 +00:00
|
|
|
|
2017-08-02 17:49:29 +00:00
|
|
|
<span class="k">return</span> <span class="n">Arguments</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">filenames</span><span class="p">,</span> <span class="n">excluded</span><span class="p">)</span></div>
|
2016-10-03 20:28:07 +00:00
|
|
|
</pre></div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2016-10-05 16:16:06 +00:00
|
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
2016-10-06 15:32:58 +00:00
|
|
|
<div class="sphinxsidebarwrapper"><div class="relations">
|
|
|
|
<h3>Related Topics</h3>
|
|
|
|
<ul>
|
|
|
|
<li><a href="../../index.html">Documentation overview</a><ul>
|
|
|
|
<li><a href="../index.html">Module code</a><ul>
|
|
|
|
</ul></li>
|
|
|
|
</ul></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2016-10-05 16:16:06 +00:00
|
|
|
<div id="searchbox" style="display: none" role="search">
|
2016-10-03 20:28:07 +00:00
|
|
|
<h3>Quick search</h3>
|
|
|
|
<form class="search" action="../../search.html" method="get">
|
2016-10-05 16:16:06 +00:00
|
|
|
<div><input type="text" name="q" /></div>
|
|
|
|
<div><input type="submit" value="Go" /></div>
|
2016-10-03 20:28:07 +00:00
|
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
|
|
<input type="hidden" name="area" value="default" />
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="clearer"></div>
|
|
|
|
</div>
|
2016-10-06 15:32:58 +00:00
|
|
|
<div class="footer">
|
|
|
|
©2016, Kenneth M. "Elf" Sternberg.
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.6</a>
|
|
|
|
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a>
|
|
|
|
|
2016-10-03 20:28:07 +00:00
|
|
|
</div>
|
2016-10-06 15:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 20:28:07 +00:00
|
|
|
</body>
|
|
|
|
</html>
|