<br>Alan &amp; Martin,<br><br>Thanks for the feedback and suggestions.&nbsp; Kodos is a great tool.&nbsp; I use it regularly to debug my regex mistakes.&nbsp; It can also be a excellent learning tool to those unfamiliar with regular expressions. &nbsp; Thanks for the Python-LDAP link and ldif example code.&nbsp; 
<br><br><div><span class="gmail_quote">On 5/20/07, <b class="gmail_sendername">Martin Walsh</b> &lt;<a href="mailto:mwalsh@groktech.org">mwalsh@groktech.org</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Tom,<br><br>Tom Tucker wrote:<br> &gt; Why the cStringIO stuff?&nbsp;&nbsp;The input data shown below is collected from<br>&gt; os.popen.&nbsp;&nbsp;I was trying to find an easy way of matching my regex.<br><br>Ah, ldap...</blockquote><div>
<br>Oh yes,&nbsp; <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">&gt; Matching with a string seemed easier than looping through the ouput
<br>&gt; collected.&nbsp;&nbsp;Hmm.&nbsp;&nbsp;Come to think of it, I guess I could match on the<br>&gt; first &quot;^dn&quot; catpure that output and then keep looping until &quot;^cn:&quot; is<br>&gt; seen. Then repeat.<br><br>Honestly, I&#39;m not very good with regular expressions -- and try to avoid
<br>them when possible. But in cases where they seem to be the best option,<br>I have formed a heavy dependence on regex debuggers like kodos.<br><a href="http://kodos.sourceforge.net/">http://kodos.sourceforge.net/</a></blockquote>
<div><br><br>Kodos is an excellent regex tool.&nbsp; I use it regularly to verify regex strings.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt; Anyways, any suggestions to fix the below code?<br>&lt;snip&gt;<br><br>Have you had a look at the python-ldap package?<br><br><a href="http://python-ldap.sourceforge.net/">http://python-ldap.sourceforge.net/</a></blockquote>
<div><br><br>Thanks.&nbsp; I&nbsp; checked the Python module index page and <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You could probably access ldap directly with python, if that&#39;s an
<br>option. Or, you could roll your own ldif parser (but make sure your data<br>contains a newline between each dn, or the parser will choke with a<br>&#39;ValueError: Two lines starting with dn: in one record.&#39;):<br>
<br>import ldif<br>from cStringIO import StringIO<br><br>class MyLDIF(ldif.LDIFParser):<br>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, inputfile):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ldif.LDIFParser.__init__(self, inputfile)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.users = []<br><br>&nbsp;&nbsp;&nbsp;&nbsp;def handle(self, dn, entry):
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.users.append((entry[&#39;uid&#39;], entry[&#39;cn&#39;]))<br><br>raw = &quot;&quot;&quot;\<br>&lt;snip your ldif example with newlines added between dns&gt;<br>&quot;&quot;&quot;<br><br>if __name__ == &#39;__main__&#39;:
<br>&nbsp;&nbsp;&nbsp;&nbsp;io = StringIO(raw)<br>&nbsp;&nbsp;&nbsp;&nbsp;lp = MyLDIF(io)<br>&nbsp;&nbsp;&nbsp;&nbsp;lp.parse()<br>&nbsp;&nbsp;&nbsp;&nbsp;for user in lp.users:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uid = user[0][0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cn = user[1][0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print uid<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print cn<br><br>... or ...<br><br>You could also use 
ldif.LDIFRecordList directly without creating a<br>custom parser class which would return a list of (dn, entry) tuples. The<br>module author warns that &#39;It can be a memory hog!&#39;, and I can imagine<br>this is true if you are working with a particularly large ldap directory.
<br><br>io = StringIO(raw)<br>directory = ldif.LDIFRecordList(io)<br>directory.parse()<br>for dn, entry in directory.all_records:<br>&nbsp;&nbsp;&nbsp;&nbsp;print entry[&#39;uid&#39;][0]<br>&nbsp;&nbsp;&nbsp;&nbsp;print entry[&#39;cn&#39;][0]<br><br>_______________________________________________
<br>Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>