<s> """<STR_LIT>""" <EOL> import base64 <EOL> def base64_encode ( s ) : <EOL> return '<STR_LIT>' . join ( base64 . encodestring ( s ) . split ( '<STR_LIT:\n>' ) ) + '<STR_LIT:\n>' <EOL> def attributeAsLDIF_base64 ( attribute , value ) : <EOL> return "<STR_LIT>" % ( attribute , base64_encode ( value ) ) <EOL> def containsNonprintable ( s ) : <EOL> for c in s : <EOL> if ord ( c ) > <NUM_LIT> or c in ( '<STR_LIT>' , '<STR_LIT:\n>' , '<STR_LIT:\r>' ) : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def attributeAsLDIF ( attribute , value ) : <EOL> if value . startswith ( '<STR_LIT>' ) or value . startswith ( '<STR_LIT:\n>' ) or value . startswith ( '<STR_LIT:\r>' ) or value . startswith ( '<STR_LIT:U+0020>' ) or value . startswith ( '<STR_LIT::>' ) or value . startswith ( '<STR_LIT:<>' ) or value . endswith ( '<STR_LIT:U+0020>' ) or containsNonprintable ( value ) : <EOL> return attributeAsLDIF_base64 ( attribute , value ) <EOL> else : <EOL> return "<STR_LIT>" % ( attribute , value ) <EOL> def asLDIF ( dn , attributes ) : <EOL> s = "<STR_LIT>" % dn <EOL> for k , vs in attributes : <EOL> for v in vs : <EOL> s = s + attributeAsLDIF ( k , v ) <EOL> s = s + "<STR_LIT:\n>" <EOL> return s <EOL> def header ( ) : <EOL> return "<STR_LIT>" <EOL> def manyAsLDIF ( objects ) : <EOL> s = [ header ( ) ] <EOL> for dn , attributes in objects : <EOL> s . append ( asLDIF ( dn , attributes ) ) <EOL> return '<STR_LIT>' . join ( s ) </s>
