<commit_before>


<!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="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>regolith.tools &#8212; regolith - software group content managment system</title>
    <link rel="stylesheet" href="../../_static/cloud.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Noticia+Text|Open+Sans|Droid+Sans+Mono" type="text/css" />
    <script type="text/javascript" src="../../_static/documentation_options.js"></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>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/javascript" src="../../_static/jquery.cookie.js"></script>
    <script type="text/javascript" src="../../_static/cloud.base.js"></script>
    <script type="text/javascript" src="../../_static/cloud.js"></script>
    <link rel="shortcut icon" href="../../_static/regolith-logo.ico"/>
    <link rel="search" title="Search" href="../../search.html" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
    <div class="relbar-top">
        
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
    <li><a href="../../index.html">regolith</a> &#187;</li>

          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
    </div>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for regolith.tools</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Misc. regolith tools.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">email.utils</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">platform</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">deepcopy</span>

<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>

<span class="kn">from</span> <span class="nn">regolith.dates</span> <span class="k">import</span> <span class="n">month_to_int</span><span class="p">,</span> <span class="n">date_to_float</span>
<span class="kn">from</span> <span class="nn">regolith.sorters</span> <span class="k">import</span> <span class="n">doc_date_key</span><span class="p">,</span> <span class="n">id_key</span><span class="p">,</span> <span class="n">ene_date_key</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">bibtexparser.bwriter</span> <span class="k">import</span> <span class="n">BibTexWriter</span>
    <span class="kn">from</span> <span class="nn">bibtexparser.bibdatabase</span> <span class="k">import</span> <span class="n">BibDatabase</span>

    <span class="n">HAVE_BIBTEX_PARSER</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">HAVE_BIBTEX_PARSER</span> <span class="o">=</span> <span class="kc">False</span>

<span class="n">LATEX_OPTS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-halt-on-error&#39;</span><span class="p">,</span> <span class="s1">&#39;-file-line-error&#39;</span><span class="p">]</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">3</span><span class="p">:</span>
    <span class="n">string_types</span> <span class="o">=</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span>
    <span class="n">unicode_type</span> <span class="o">=</span> <span class="nb">str</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">pass</span>
    <span class="c1"># string_types = (str, unicode)</span>
    <span class="c1"># unicode_type = unicode</span>

<span class="n">DEFAULT_ENCODING</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">getdefaultencoding</span><span class="p">()</span>

<span class="n">ON_WINDOWS</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Windows&#39;</span><span class="p">)</span>
<span class="n">ON_MAC</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Darwin&#39;</span><span class="p">)</span>
<span class="n">ON_LINUX</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Linux&#39;</span><span class="p">)</span>
<span class="n">ON_POSIX</span> <span class="o">=</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;posix&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="dbdirname"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.dbdirname">[docs]</a><span class="k">def</span> <span class="nf">dbdirname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets the database dir name.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;local&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
        <span class="n">dbsdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rc</span><span class="o">.</span><span class="n">builddir</span><span class="p">,</span> <span class="s1">&#39;_dbs&#39;</span><span class="p">)</span>
        <span class="n">dbdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dbsdir</span><span class="p">,</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dbdir</span> <span class="o">=</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;url&#39;</span><span class="p">]</span></div>
    <span class="k">return</span> <span class="n">dbdir</span>


<div class="viewcode-block" id="dbpathname"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.dbpathname">[docs]</a><span class="k">def</span> <span class="nf">dbpathname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets the database path name.&quot;&quot;&quot;</span>
    <span class="n">dbdir</span> <span class="o">=</span> <span class="n">dbdirname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">)</span>
    <span class="n">dbpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dbdir</span><span class="p">,</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;path&#39;</span><span class="p">])</span></div>
    <span class="k">return</span> <span class="n">dbpath</span>


<div class="viewcode-block" id="fallback"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.fallback">[docs]</a><span class="k">def</span> <span class="nf">fallback</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">backup</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator for returning the object if cond is true and a backup if</span>
<span class="sd">    cond is false. &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">dec</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span> <span class="k">if</span> <span class="n">cond</span> <span class="k">else</span> <span class="n">backup</span>
</div>
    <span class="k">return</span> <span class="n">dec</span>


<div class="viewcode-block" id="all_docs_from_collection"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.all_docs_from_collection">[docs]</a><span class="k">def</span> <span class="nf">all_docs_from_collection</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">collname</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Yield all entries in for all collections of a given name in a given</span>
<span class="sd">    database. &quot;&quot;&quot;</span></div>
    <span class="k">yield from</span> <span class="n">client</span><span class="o">.</span><span class="n">all_documents</span><span class="p">(</span><span class="n">collname</span><span class="p">)</span>


<span class="n">SHORT_MONTH_NAMES</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Jan&#39;</span><span class="p">,</span> <span class="s1">&#39;Feb&#39;</span><span class="p">,</span> <span class="s1">&#39;Mar&#39;</span><span class="p">,</span> <span class="s1">&#39;Apr&#39;</span><span class="p">,</span> <span class="s1">&#39;May&#39;</span><span class="p">,</span> <span class="s1">&#39;Jun&#39;</span><span class="p">,</span> <span class="s1">&#39;Jul&#39;</span><span class="p">,</span>
                     <span class="s1">&#39;Aug&#39;</span><span class="p">,</span> <span class="s1">&#39;Sept&#39;</span><span class="p">,</span> <span class="s1">&#39;Oct&#39;</span><span class="p">,</span> <span class="s1">&#39;Nov&#39;</span><span class="p">,</span> <span class="s1">&#39;Dec&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="date_to_rfc822"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.date_to_rfc822">[docs]</a><span class="k">def</span> <span class="nf">date_to_rfc822</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts a date to an RFC 822 formatted string.&quot;&quot;&quot;</span>
    <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="n">month_to_int</span><span class="p">(</span><span class="n">m</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">d</span><span class="p">))</span></div>
    <span class="k">return</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">format_datetime</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>


<div class="viewcode-block" id="rfc822now"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.rfc822now">[docs]</a><span class="k">def</span> <span class="nf">rfc822now</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Creates a string of the current time according to RFC 822.&quot;&quot;&quot;</span>
    <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span></div>
    <span class="k">return</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">format_datetime</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>


<div class="viewcode-block" id="gets"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.gets">[docs]</a><span class="k">def</span> <span class="nf">gets</span><span class="p">(</span><span class="n">seq</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets a key from every element of a sequence if possible.&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq</span><span class="p">:</span></div>
        <span class="k">yield</span> <span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>


<div class="viewcode-block" id="month_and_year"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.month_and_year">[docs]</a><span class="k">def</span> <span class="nf">month_and_year</span><span class="p">(</span><span class="n">m</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Creates a string from month and year data, if available.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="s2">&quot;present&quot;</span>
    <span class="k">if</span> <span class="n">m</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">month_to_int</span><span class="p">(</span><span class="n">m</span><span class="p">)</span></div>
    <span class="k">return</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">SHORT_MONTH_NAMES</span><span class="p">[</span><span class="n">m</span><span class="p">],</span> <span class="n">y</span><span class="p">)</span>


<div class="viewcode-block" id="filter_publications"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.filter_publications">[docs]</a><span class="k">def</span> <span class="nf">filter_publications</span><span class="p">(</span><span class="n">citations</span><span class="p">,</span> <span class="n">authors</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">bold</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Filter publications by the author(s)/editor(s)</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    citations : list of dict</span>
<span class="sd">        The publication citations</span>
<span class="sd">    authors : set of str</span>
<span class="sd">        The authors to be filtered against</span>
<span class="sd">    reverse : bool, optional</span>
<span class="sd">        If True reverse the order, defaults to False</span>
<span class="sd">    bold : bool, optional</span>
<span class="sd">        If True put latex bold around the author(s) in question</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">pubs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">pub</span> <span class="ow">in</span> <span class="n">citations</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">pub</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="p">[]))</span>
               <span class="o">&amp;</span> <span class="nb">set</span><span class="p">(</span><span class="n">pub</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;editor&#39;</span><span class="p">,</span> <span class="p">[]))</span>
               <span class="o">&amp;</span> <span class="n">authors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">pub</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bold</span><span class="p">:</span><commit_msg>Update docs after building Travis build 227 of regro/regolith<commit_after>


<!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="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>regolith.tools &#8212; regolith - software group content managment system</title>
    <link rel="stylesheet" href="../../_static/cloud.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Noticia+Text|Open+Sans|Droid+Sans+Mono" type="text/css" />
    <script type="text/javascript" src="../../_static/documentation_options.js"></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>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/javascript" src="../../_static/jquery.cookie.js"></script>
    <script type="text/javascript" src="../../_static/cloud.base.js"></script>
    <script type="text/javascript" src="../../_static/cloud.js"></script>
    <link rel="shortcut icon" href="../../_static/regolith-logo.ico"/>
    <link rel="search" title="Search" href="../../search.html" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
    <div class="relbar-top">
        
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
    <li><a href="../../index.html">regolith</a> &#187;</li>

          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
    </div>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for regolith.tools</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Misc. regolith tools.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">email.utils</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">platform</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">deepcopy</span>

<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>

<span class="kn">from</span> <span class="nn">regolith.dates</span> <span class="k">import</span> <span class="n">month_to_int</span><span class="p">,</span> <span class="n">date_to_float</span>
<span class="kn">from</span> <span class="nn">regolith.sorters</span> <span class="k">import</span> <span class="n">doc_date_key</span><span class="p">,</span> <span class="n">id_key</span><span class="p">,</span> <span class="n">ene_date_key</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">bibtexparser.bwriter</span> <span class="k">import</span> <span class="n">BibTexWriter</span>
    <span class="kn">from</span> <span class="nn">bibtexparser.bibdatabase</span> <span class="k">import</span> <span class="n">BibDatabase</span>

    <span class="n">HAVE_BIBTEX_PARSER</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">HAVE_BIBTEX_PARSER</span> <span class="o">=</span> <span class="kc">False</span>

<span class="n">LATEX_OPTS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-halt-on-error&#39;</span><span class="p">,</span> <span class="s1">&#39;-file-line-error&#39;</span><span class="p">]</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">3</span><span class="p">:</span>
    <span class="n">string_types</span> <span class="o">=</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">)</span>
    <span class="n">unicode_type</span> <span class="o">=</span> <span class="nb">str</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">pass</span>
    <span class="c1"># string_types = (str, unicode)</span>
    <span class="c1"># unicode_type = unicode</span>

<span class="n">DEFAULT_ENCODING</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">getdefaultencoding</span><span class="p">()</span>

<span class="n">ON_WINDOWS</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Windows&#39;</span><span class="p">)</span>
<span class="n">ON_MAC</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Darwin&#39;</span><span class="p">)</span>
<span class="n">ON_LINUX</span> <span class="o">=</span> <span class="p">(</span><span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Linux&#39;</span><span class="p">)</span>
<span class="n">ON_POSIX</span> <span class="o">=</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;posix&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="dbdirname"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.dbdirname">[docs]</a><span class="k">def</span> <span class="nf">dbdirname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets the database dir name.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;local&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
        <span class="n">dbsdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rc</span><span class="o">.</span><span class="n">builddir</span><span class="p">,</span> <span class="s1">&#39;_dbs&#39;</span><span class="p">)</span>
        <span class="n">dbdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dbsdir</span><span class="p">,</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dbdir</span> <span class="o">=</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;url&#39;</span><span class="p">]</span></div>
    <span class="k">return</span> <span class="n">dbdir</span>


<div class="viewcode-block" id="dbpathname"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.dbpathname">[docs]</a><span class="k">def</span> <span class="nf">dbpathname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets the database path name.&quot;&quot;&quot;</span>
    <span class="n">dbdir</span> <span class="o">=</span> <span class="n">dbdirname</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">rc</span><span class="p">)</span>
    <span class="n">dbpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dbdir</span><span class="p">,</span> <span class="n">db</span><span class="p">[</span><span class="s1">&#39;path&#39;</span><span class="p">])</span></div>
    <span class="k">return</span> <span class="n">dbpath</span>


<div class="viewcode-block" id="fallback"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.fallback">[docs]</a><span class="k">def</span> <span class="nf">fallback</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">backup</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Decorator for returning the object if cond is true and a backup if</span>
<span class="sd">    cond is false. &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">dec</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span> <span class="k">if</span> <span class="n">cond</span> <span class="k">else</span> <span class="n">backup</span>
</div>
    <span class="k">return</span> <span class="n">dec</span>


<div class="viewcode-block" id="all_docs_from_collection"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.all_docs_from_collection">[docs]</a><span class="k">def</span> <span class="nf">all_docs_from_collection</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">collname</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Yield all entries in for all collections of a given name in a given</span>
<span class="sd">    database. &quot;&quot;&quot;</span></div>
    <span class="k">yield from</span> <span class="n">client</span><span class="o">.</span><span class="n">all_documents</span><span class="p">(</span><span class="n">collname</span><span class="p">)</span>


<span class="n">SHORT_MONTH_NAMES</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;Jan&#39;</span><span class="p">,</span> <span class="s1">&#39;Feb&#39;</span><span class="p">,</span> <span class="s1">&#39;Mar&#39;</span><span class="p">,</span> <span class="s1">&#39;Apr&#39;</span><span class="p">,</span> <span class="s1">&#39;May&#39;</span><span class="p">,</span> <span class="s1">&#39;Jun&#39;</span><span class="p">,</span> <span class="s1">&#39;Jul&#39;</span><span class="p">,</span>
                     <span class="s1">&#39;Aug&#39;</span><span class="p">,</span> <span class="s1">&#39;Sept&#39;</span><span class="p">,</span> <span class="s1">&#39;Oct&#39;</span><span class="p">,</span> <span class="s1">&#39;Nov&#39;</span><span class="p">,</span> <span class="s1">&#39;Dec&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="date_to_rfc822"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.date_to_rfc822">[docs]</a><span class="k">def</span> <span class="nf">date_to_rfc822</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts a date to an RFC 822 formatted string.&quot;&quot;&quot;</span>
    <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="n">month_to_int</span><span class="p">(</span><span class="n">m</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">d</span><span class="p">))</span></div>
    <span class="k">return</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">format_datetime</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>


<div class="viewcode-block" id="rfc822now"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.rfc822now">[docs]</a><span class="k">def</span> <span class="nf">rfc822now</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Creates a string of the current time according to RFC 822.&quot;&quot;&quot;</span>
    <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span></div>
    <span class="k">return</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">format_datetime</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>


<div class="viewcode-block" id="gets"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.gets">[docs]</a><span class="k">def</span> <span class="nf">gets</span><span class="p">(</span><span class="n">seq</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Gets a key from every element of a sequence if possible.&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq</span><span class="p">:</span></div>
        <span class="k">yield</span> <span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>


<div class="viewcode-block" id="month_and_year"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.month_and_year">[docs]</a><span class="k">def</span> <span class="nf">month_and_year</span><span class="p">(</span><span class="n">m</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Creates a string from month and year data, if available.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="s2">&quot;present&quot;</span>
    <span class="k">if</span> <span class="n">m</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">month_to_int</span><span class="p">(</span><span class="n">m</span><span class="p">)</span></div>
    <span class="k">return</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">SHORT_MONTH_NAMES</span><span class="p">[</span><span class="n">m</span><span class="p">],</span> <span class="n">y</span><span class="p">)</span>


<div class="viewcode-block" id="filter_publications"><a class="viewcode-back" href="../../api/tools.html#regolith.tools.filter_publications">[docs]</a><span class="k">def</span> <span class="nf">filter_publications</span><span class="p">(</span><span class="n">citations</span><span class="p">,</span> <span class="n">authors</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">bold</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Filter publications by the author(s)/editor(s)</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    citations : list of dict</span>
<span class="sd">        The publication citations</span>
<span class="sd">    authors : set of str</span>
<span class="sd">        The authors to be filtered against</span>
<span class="sd">    reverse : bool, optional</span>
<span class="sd">        If True reverse the order, defaults to False</span>
<span class="sd">    bold : bool, optional</span>
<span class="sd">        If True put latex bold around the author(s) in question</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">pubs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">pub</span> <span class="ow">in</span> <span class="n">citations</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">pub</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="p">[]))</span>
               <span class="o">|</span> <span class="nb">set</span><span class="p">(</span><span class="n">pub</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;editor&#39;</span><span class="p">,</span> <span class="p">[]))</span>
               <span class="o">&amp;</span> <span class="n">authors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">pub</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bold</span><span class="p">:</span>
{'fraction_non_alphanumeric': 0.28404482390608327, 'fraction_numerical': 0.010538954108858058, 'mean_word_length': 6.547523157470802, 'pattern_counts': {'":': 0, '<': 2147, '<?xml version=': 0, '>': 2147, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '897060', 'n_tokens_mistral': 13508, 'n_tokens_neox': 13149, 'n_words': 2063}