Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
181 lines (126 sloc) 9.15 KB
<!DOCTYPE html>
<html lang="en">
<head>
<title>Parsing MongoDB URI</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="./theme/css/main.css" type="text/css" />
<link href="/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="pydanny Atom Feed" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<!--[if lte IE 7]>
<link rel="stylesheet" type="text/css" media="all" href="./css/ie.css"/>
<script src="./js/IE8.js" type="text/javascript"></script><![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" media="all" href="./css/ie6.css"/><![endif]-->
</head>
<body id="index" class="home">
<a href="https://github.com/pydanny/pydanny.github.com">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
</a>
<header id="banner" class="body">
<h1><a href=".">pydanny </a></h1>
<nav><ul>
<li><a href="./pages/about.html">About</a></li>
<li><a href="./pages/tools.html">Tools</a></li>
<li><a href="./pages/upcoming.html">Upcoming</a></li>
<li ><a href="./category/audrey.html">Audrey</a></li>
<li ><a href="./category/blog.html">Blog</a></li>
<li ><a href="./category/django.html">Django</a></li>
<li class="active"><a href="./category/python.html">Python</a></li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<article>
<header>
<h1 class="entry-title">
<a href="parsing-mongodb-uri.html" rel="bookmark"
title="Permalink to Parsing MongoDB URI">Parsing MongoDB URI</a></h1>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="pydanny">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2012-02-20T09:20:00">
Mon 20 February 2012
</abbr>
<address class="vcard author">
By <a class="url fn" href="./author/Daniel-Greenfeld.html">Daniel-Greenfeld</a>
</address>
<p>In <a href="./category/python.html">python</a>. </p>
<p>tags: <a href="./tag/python.html">python</a><a href="./tag/mongodb.html">mongodb</a><a href="./tag/howto.html">howto</a></p>
</footer><!-- /.post-info -->
<p>Rather than hard-code the configuration into a Python based settings file, when using a PaaS such as Heroku you want to pick up the MongoDB URI from the system settings. Here's what I do:</p>
<div class="highlight"><pre><span class="c"># get the dynamic elements from the MongoURI</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="n">r</span> <span class="o">=</span> <span class="s">r&#39;^mongodb\:\/\/(?P&lt;username&gt;[_\w]+):(?P&lt;password&gt;[\w]+)@(?P&lt;host&gt;[\.\w]+):(?P&lt;port&gt;\d+)/(?P&lt;database&gt;[_\w]+)$&#39;</span>
<span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="n">mongolab_url</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;MONGOLAB_URI&#39;</span><span class="p">]</span>
<span class="n">match</span> <span class="o">=</span> <span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">mongolab_url</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
<span class="c"># Save the data to settings</span>
<span class="n">MONGO_HOST</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">]</span>
<span class="n">MONGO_PORT</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s">&#39;port&#39;</span><span class="p">])</span>
<span class="n">MONGO_NAME</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">]</span>
<span class="n">MONGO_DATABASE</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s">&#39;database&#39;</span><span class="p">]</span>
<span class="n">MONGO_PASSWORD</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">]</span>
<span class="c"># Connect to MongoEngine</span>
<span class="kn">from</span> <span class="nn">mongoengine</span> <span class="kn">import</span> <span class="n">connect</span> <span class="k">as</span> <span class="n">me_connect</span>
<span class="n">me_connect</span><span class="p">(</span>
<span class="n">data</span><span class="p">[</span><span class="s">&#39;database&#39;</span><span class="p">],</span>
<span class="n">host</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s">&#39;host&#39;</span><span class="p">],</span>
<span class="n">port</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s">&#39;port&#39;</span><span class="p">]),</span>
<span class="n">username</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">],</span>
<span class="n">password</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">])</span>
</pre></div>
</div><!-- /.entry-content -->
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_identifier = "parsing-mongodb-uri.html";
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://pydanny.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div>
</article>
</section>
<section id="extras" class="body">
<div class="social">
<h2>social</h2>
<ul>
<li><a href="/feeds/all.atom.xml" type="application/atom+xml" rel="alternate">atom feed</a></li>
<li><a href="http://twitter.com/pydanny">twitter</a></li>
<li><a href="https://github.com/pydanny">github</a></li>
<li><a href="http://www.facebook.com/daniel.greenfeld">facebook</a></li>
</ul>
</div><!-- /.social -->
</section><!-- /#extras -->
<footer id="contentinfo" class="body">
<address id="about" class="vcard body">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.
</address><!-- /#about -->
<p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!</p>
</footer><!-- /#contentinfo -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-18066389-3");
pageTracker._trackPageview();
} catch(err) {}</script>
<script type="text/javascript">
var disqus_shortname = 'pydanny';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
</body>
</html>
Something went wrong with that request. Please try again.