ó J¡Vc@sdZdZdddddddd gZd d lmZmZmZd d lZd d lZd d l Z yd dl m Z Wn!e k r¡d dl m Z nXd dl TdZdZedeZededZejdeƒZdeƒZidd6d d6dd6Zidd6dd 6dd6ZddddgZiZxeD]Zd ee See http://python-ldap.sourceforge.net for details. $Id: ldif.py,v 1.3 2008/02/11 16:35:43 dwelch Exp $ Python compability note: Tested with Python 2.0+, but should work with Python 1.5.2+. The python-ldap package is distributed under Python-style license. Standard disclaimer: This software is made available by the author(s) to the public for free and "as is". All users of this free software are solely and entirely responsible for their own choice and use of this software for their own purposes. By using this software, each user agrees that the author(s) shall not be liable for damages of any kind in relation to its use or performance. The author(s) do not warrant that this software is fit for any purpose. Note: This file is part of the python-ldap package. For the complete python-ldap package, please visit: http://sourceforge.net/projects/python-ldap/ It has been modified for use in HPLIP. s0.5.5t ldif_patterntAttrTypeandValueLDIFt CreateLDIFt ParseLDIFt LDIFWritert LDIFParsertLDIFRecordListtLDIFCopyi(turllib2_requestt urllib2_parset urllib2_erroriÿÿÿÿN(tStringIO(t*s[\w;.]+(;[\w_-]+)*s(([^,]|\\,)+|".*?")s [ ]*=[ ]*s ([ ]*,[ ]*s)*[ ]*s^%s$s>^((dn(:|::) %(dn_pattern)s)|(%(attrtype_pattern)s(:|::) .*)$)+itaddtdeleteitreplacetmodifytmodrdns (^(| | | |:|<)|[ €-ÿ]+|[ ]+$)cCs>|dkrdStj|ƒ}|dko=|jdƒ|kS(s% returns 1 if s is a LDAP DN tiiN(tdn_regextmatchtNonetgroup(tstrm((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pytis_dnRs cCstj|ƒdk S(sK returns 1 if s has to be base-64 encoded because of special chars N(tsafe_string_retsearchR(R((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt needs_base64\scCs#tg|D]}|df^q ƒS(sR return a dictionary with all items of l being the keys of the dictionary N(tdictR(tlti((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt list_dictcscBsMeZdZd ddd„Zd„Zd„Zd„Zd„Zd„Z RS( s’ Write LDIF entry or change records to file object Copy LDIF input to a file output object containing all data retrieved via URLs iLs cCsV||_tgtpgD]}|jƒ^qƒ|_||_||_d|_dS(sI output_file file object for output base64_attrs list of attribute types to be base64-encoded in any case cols Specifies how many columns a line may have before it's folded into many lines. line_sep String used as line separator iN(t _output_fileR t bases64_attrstlowert _base64_attrst_colst _line_septrecords_written(tselft output_filet base64_attrstcolstline_septa((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt__init__qs .  cCsöt|ƒ}||jkrA|jj|ƒ|jj|jƒn±|j}|jj|dt||jƒ!ƒ|jj|jƒxo||krñ|jjdƒ|jj||t|||jdƒ!ƒ|jj|jƒ||jd}qƒWdS(s? Write string line as one or more folded lines it iN(tlenR%R!twriteR&tmin(R(tlinetline_lentpos((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt_unfoldLDIFLineƒs  #+cCsu|jƒ|jks!t|ƒrU|jdj|tj|ƒjddƒgƒƒn|jdj||gƒƒdS(s› Write a single attribute type/value pair attr_type attribute type attr_value attribute value s:: s Rs: N(R#R$RR6tjointbase64t encodestringR(R(t attr_typet attr_value((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt_unparseAttrTypeandValue˜s  4cCsWt|jƒƒ}|jƒx3|D]+}x"||D]}|j||ƒq5Wq$WdS(s? entry dictionary holding an entry N(tlisttkeystsortR<(R(tentryt attr_typesR:R;((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt_unparseEntryRecord©s   c Cst|dƒ}|dkr%d}n!|dkr:d}n tdƒ‚|jd|ƒx·|D]¯}|dkr~|\}}n>|dkr°|\}}}|jt||ƒn tdƒ‚|ræx!|D]}|j||ƒqÉWn|dkr]|jjd |jƒq]q]Wd S( s\ modlist list of additions (2-tuple) or modifications (3-tuple) iiR iRsmodlist item of wrong lengtht changetypes'Subsequent modlist item of wrong lengtht-N(R0t ValueErrorR<t MOD_OP_STRR!R1R&( R(tmodlisttmod_lenRCtmodtmod_typetmod_valstmod_optmod_val((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt_unparseChangeRecord´s&           cCs‹|s dS|jd|ƒt|tƒr9|j|ƒn+t|tƒrX|j|ƒn tdƒ‚|jj|j ƒ|j d|_ dS(sñ dn string-representation of distinguished name record Either a dictionary holding the LDAP entry {attrtype:record} or a list with a modify list like for LDAPObject.modify(). Ntdns*Argument record must be dictionary or listi( R<t isinstanceRRBR=RNRER!R1R&R'(R(ROtrecord((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pytunparseÏs N( t__name__t __module__t__doc__RR.R6R<RBRNRR(((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyRjs   iLcCsHtƒ}t|||dƒ}|j||ƒ|jƒ}|jƒ|S(s Create LDIF single formatted record including trailing empty line. This is a compability function. Use is deprecated! dn string-representation of distinguished name record Either a dictionary holding the LDAP entry {attrtype:record} or a list with a modify list like for LDAPObject.modify(). base64_attrs list of attribute types to be base64-encoded in any case cols Specifies how many columns a line may have before it's folded into many lines. s (R RRRtgetvaluetclose(RORQR*R+tft ldif_writerR((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyRês    cBsPeZdZd„Zd dd dd„Zd„Zd„Zd„Zd„Z RS( sò Base class for a LDIF parser. Applications should sub-class this class and override method handle() to implement something meaningful. Public class attributes: records_read Counter for records processed so far cCs|jdƒjdƒS(sQ Strip trailing line separators from s, but no other whitespaces s s (tstrip(R(R((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyt _stripLineSep sis cCs„||_||_tg|p!gD]}|jƒ^q"ƒ|_tg|pOgD]}|jƒ^qPƒ|_||_d|_dS(sK Parameters: input_file File-object to read the LDIF input from ignored_attr_types Attributes with these attribute type names will be ignored. max_entries If non-zero specifies the maximum number of entries to be read from f. process_url_schemes List containing strings with URLs schemes to process with urllib. An empty list turns off all URL processing and the attribute is ignored completely. line_sep String used as line separator iN(t _input_filet _max_entriesR R#t_process_url_schemest_ignored_attr_typesR&t records_read(R(t input_filetignored_attr_typest max_entriestprocess_url_schemesR,RR-((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR.s   .. cCsdS(sƒ Process a single content LDIF record. This method should be implemented by applications using LDIFParser. N((R(ROR@((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pythandle2scCs†|j|jƒg}|jjƒ|_xO|jrx|jddkrx|j|j|jdƒƒ|jjƒ|_q*Wdj|ƒS(sO Unfold several folded lines with trailing space into one line iR/iR(R[t_lineR\treadlinetappendR7(R(tunfolded_lines((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR68s cCsƒ|jƒ}x&|r4|ddkr4|jƒ}qW| sT|dksT|dkrXd Sy|jdƒ}Wn#tk rtjd|ƒdSX|d|!}|||d!}|d krÕtj||dƒ}n¤|d krD||djƒ}d }|j ryt j |ƒ}|d|j krAt j |ƒjƒ}qAqyn5|d ks\|dkred}n||djƒ}||fS(sj Parse a single attribute type and value pair from one or more lines of LDIF data it#s s t:sMalformed line: %sRis::s:|jrR|j s:|j|jkrRd }d }d }d }i}|jƒ\}}xº|d k r&|d k r&|jƒ}tj dt |ƒt |ƒfƒ| sÂ| rÚ|jƒ\}}qmn|dkr/|d k rt dƒ‚nt |ƒs&t dt |ƒƒ‚n|}nâ|dkrP|d krPd}nÁ|dkrÀ|d krwt dƒ‚n|d k r’t d ƒ‚n|t kr·t d t |ƒƒ‚n|}nQ|d k r|jƒ|jkr||kr||j|ƒq|g|| %sROs*Two lines starting with dn: in one record.s8No valid string-representation of distinguished name %s.tversioniRCs/Read changetype: before getting valid dn: line.s2Two lines starting with changetype: in one record.schangetype value %s is invalid.N(R\RgRfR]R`RRyR#RmtdebugtreprRERtvalid_changetype_dictR_RhRe(R(RzRORCtmodopR@R:R;((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pytparsevsR  #            N( RSRTRUR[RR.ReR6RyR(((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyRs   1cBs)eZdZdddd„Zd„ZRS(st Collect all records of LDIF input into a single list. of 2-tuples (dn, entry). It can be a memory hog! icCs&tj|||||ƒg|_dS(s” See LDIFParser.__init__() Additional Parameters: all_records List instance for storing parsed records N(RR.t all_records(R(RaRbRcRd((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR.Äs cCs|jj||fƒdS(sD Append single record to dictionary of all records. N(R€Rh(R(ROR@((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyReÐsN(RSRTRURR.Re(((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR¾s cBs2eZdZddddddd„Zd„ZRS(sS Copy LDIF input to LDIF output containing all data retrieved via URLs iiLs c Cs5tj|||||ƒt||||ƒ|_dS(sE See LDIFParser.__init__() and LDIFWriter.__init__() N(RR.Rt _output_ldif( R(RaR)RbRcRdR*R+R,((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR.ÝscCs|jj||ƒdS(s: Write single LDIF record to output file. N(RRR(R(ROR@((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyReæsN(RSRTRURR.Re(((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyR×s  cCs/t|d|d|ddƒ}|jƒ|jS(sc Parse LDIF records read from file. This is a compability function. Use is deprecated! RbRcRdi(RRR€(RXt ignore_attrst maxentriest ldif_parser((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pyRís ((('RUt __version__t__all__t sixext.movesRR R R8trettypestioR t ImportErrortgtattrtype_patterntattrvalue_patternt rdn_patternt dn_patterntcompileRtvarsRtMOD_OP_INTEGERRFt CHANGE_TYPESR}tcRtSAFE_STRING_PATTERNRRRR RRRRRR(((sY/builds/ul11u3sru-gate/components/hplip/build/prototype/i386/usr/share/hplip/base/ldif.pytsR           €¼