[Py3] Fix renamed functions

* types.*
* UserDict
* has_key()
* file()
This commit is contained in:
Raphaël Barrois
2013-11-14 01:23:43 +01:00
committed by Christian Heimes
parent 2d635e21dd
commit 5248df94d3
10 changed files with 83 additions and 80 deletions

View File

@@ -10,8 +10,7 @@ $Id: cidict.py,v 1.13 2009/04/17 14:34:34 stroeder Exp $
__version__ = """$Revision: 1.13 $""" __version__ = """$Revision: 1.13 $"""
from UserDict import UserDict from ldap.compat import UserDict
from string import lower
class cidict(UserDict): class cidict(UserDict):
""" """
@@ -24,15 +23,15 @@ class cidict(UserDict):
self.update(default or {}) self.update(default or {})
def __getitem__(self,key): def __getitem__(self,key):
return self.data[lower(key)] return self.data[key.lower()]
def __setitem__(self,key,value): def __setitem__(self,key,value):
lower_key = lower(key) lower_key = key.lower()
self._keys[lower_key] = key self._keys[lower_key] = key
self.data[lower_key] = value self.data[lower_key] = value
def __delitem__(self,key): def __delitem__(self,key):
lower_key = lower(key) lower_key = key.lower()
del self._keys[lower_key] del self._keys[lower_key]
del self.data[lower_key] del self.data[lower_key]
@@ -41,7 +40,7 @@ class cidict(UserDict):
self[key] = dict[key] self[key] = dict[key]
def has_key(self,key): def has_key(self,key):
return UserDict.has_key(self,lower(key)) return UserDict.has_key(self,key.lower())
def __contains__(self,key): def __contains__(self,key):
return self.has_key(key) return self.has_key(key)

10
Lib/ldap/compat.py Normal file
View File

@@ -0,0 +1,10 @@
"""Compatibility wrappers for Py2/Py3."""
import sys
if sys.version_info[0] < 3:
from UserDict import UserDict
from urllib import quote, unquote
else:
from collections import UserDict
from urllib.parse import quote, unquote

View File

@@ -121,15 +121,15 @@ class SimpleLDAPObject:
return result return result
def __setattr__(self,name,value): def __setattr__(self,name,value):
if self.CLASSATTR_OPTION_MAPPING.has_key(name): if name in self.CLASSATTR_OPTION_MAPPING:
self.set_option(self.CLASSATTR_OPTION_MAPPING[name],value) self.set_option(self.CLASSATTR_OPTION_MAPPING[name],value)
else: else:
self.__dict__[name] = value self.__dict__[name] = value
def __getattr__(self,name): def __getattr__(self,name):
if self.CLASSATTR_OPTION_MAPPING.has_key(name): if name in self.CLASSATTR_OPTION_MAPPING:
return self.get_option(self.CLASSATTR_OPTION_MAPPING[name]) return self.get_option(self.CLASSATTR_OPTION_MAPPING[name])
elif self.__dict__.has_key(name): elif name in self.__dict__:
return self.__dict__[name] return self.__dict__[name]
else: else:
raise AttributeError('%s has no attribute %s' % ( raise AttributeError('%s has no attribute %s' % (
@@ -814,7 +814,7 @@ class ReconnectLDAPObject(SimpleLDAPObject):
"""return data representation for pickled object""" """return data representation for pickled object"""
d = {} d = {}
for k,v in self.__dict__.items(): for k,v in self.__dict__.items():
if not self.__transient_attrs__.has_key(k): if k not in self.__transient_attrs__:
d[k] = v d[k] = v
return d return d
@@ -888,7 +888,7 @@ class ReconnectLDAPObject(SimpleLDAPObject):
return # reconnect() return # reconnect()
def _apply_method_s(self,func,*args,**kwargs): def _apply_method_s(self,func,*args,**kwargs):
if not self.__dict__.has_key('_l'): if '_l' not in self.__dict__:
self.reconnect(self._uri,retry_max=self._retry_max,retry_delay=self._retry_delay) self.reconnect(self._uri,retry_max=self._retry_max,retry_delay=self._retry_delay)
try: try:
return func(self,*args,**kwargs) return func(self,*args,**kwargs)

View File

@@ -6,17 +6,11 @@ See http://www.python-ldap.org/ for details.
\$Id: models.py,v 1.48 2015/06/06 09:21:38 stroeder Exp $ \$Id: models.py,v 1.48 2015/06/06 09:21:38 stroeder Exp $
""" """
import UserDict,ldap.cidict import ldap.cidict
from ldap.compat import UserDict
from ldap.schema.tokenizer import split_tokens,extract_tokens from ldap.schema.tokenizer import split_tokens,extract_tokens
if __debug__:
from types import TupleType,StringType,IntType
try:
from types import BooleanType
except ImportError:
BooleanType = IntType
NOT_HUMAN_READABLE_LDAP_SYNTAXES = { NOT_HUMAN_READABLE_LDAP_SYNTAXES = {
'1.3.6.1.4.1.1466.115.121.1.4':None, # Audio '1.3.6.1.4.1.1466.115.121.1.4':None, # Audio
@@ -70,7 +64,7 @@ class SchemaElement:
return self.oid return self.oid
def key_attr(self,key,value,quoted=0): def key_attr(self,key,value,quoted=0):
assert value is None or type(value)==StringType,TypeError("value has to be of StringType, was %s" % repr(value)) assert value is None or type(value)==str,TypeError("value has to be of StringType, was %s" % repr(value))
if value: if value:
if quoted: if quoted:
return " %s '%s'" % (key,value.replace("'","\\'")) return " %s '%s'" % (key,value.replace("'","\\'"))
@@ -80,7 +74,7 @@ class SchemaElement:
return "" return ""
def key_list(self,key,values,sep=' ',quoted=0): def key_list(self,key,values,sep=' ',quoted=0):
assert type(values)==TupleType,TypeError("values has to be of ListType") assert type(values) == tuple,TypeError("values has to be of ListType")
if not values: if not values:
return '' return ''
if quoted: if quoted:
@@ -161,13 +155,13 @@ class ObjectClass(SchemaElement):
self.sup = ('top',) self.sup = ('top',)
else: else:
self.sup = d['SUP'] self.sup = d['SUP']
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.sup)==TupleType assert type(self.sup) == tuple
assert type(self.kind)==IntType assert type(self.kind) == int
assert type(self.must)==TupleType assert type(self.must) == tuple
assert type(self.may)==TupleType assert type(self.may) == tuple
return return
def __str__(self): def __str__(self):
@@ -288,13 +282,13 @@ class AttributeType(SchemaElement):
self.collective = d['COLLECTIVE']!=None self.collective = d['COLLECTIVE']!=None
self.no_user_mod = d['NO-USER-MODIFICATION']!=None self.no_user_mod = d['NO-USER-MODIFICATION']!=None
self.usage = AttributeUsage.get(d['USAGE'][0],0) self.usage = AttributeUsage.get(d['USAGE'][0],0)
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.sup)==TupleType,'attribute sup has type %s' % (type(self.sup)) assert type(self.sup) == tuple,'attribute sup has type %s' % (type(self.sup))
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.single_value)==BooleanType and (self.single_value==0 or self.single_value==1) assert type(self.single_value) == bool and (self.single_value==0 or self.single_value==1)
assert type(self.no_user_mod)==BooleanType and (self.no_user_mod==0 or self.no_user_mod==1) assert type(self.no_user_mod) == bool and (self.no_user_mod==0 or self.no_user_mod==1)
assert self.syntax is None or type(self.syntax)==StringType assert self.syntax is None or type(self.syntax) == str
assert self.syntax_len is None or type(self.syntax_len)==type(0) assert self.syntax_len is None or type(self.syntax_len)==type(0)
return return
@@ -353,7 +347,7 @@ class LDAPSyntax(SchemaElement):
NOT_HUMAN_READABLE_LDAP_SYNTAXES.has_key(self.oid) or \ NOT_HUMAN_READABLE_LDAP_SYNTAXES.has_key(self.oid) or \
d['X-NOT-HUMAN-READABLE'][0]=='TRUE' d['X-NOT-HUMAN-READABLE'][0]=='TRUE'
self.x_binary_transfer_required = d['X-BINARY-TRANSFER-REQUIRED'][0]=='TRUE' self.x_binary_transfer_required = d['X-BINARY-TRANSFER-REQUIRED'][0]=='TRUE'
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
return return
def __str__(self): def __str__(self):
@@ -400,10 +394,10 @@ class MatchingRule(SchemaElement):
self.desc = d['DESC'][0] self.desc = d['DESC'][0]
self.obsolete = d['OBSOLETE']!=None self.obsolete = d['OBSOLETE']!=None
self.syntax = d['SYNTAX'][0] self.syntax = d['SYNTAX'][0]
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert self.syntax is None or type(self.syntax)==StringType assert self.syntax is None or type(self.syntax) == str
return return
def __str__(self): def __str__(self):
@@ -450,10 +444,10 @@ class MatchingRuleUse(SchemaElement):
self.desc = d['DESC'][0] self.desc = d['DESC'][0]
self.obsolete = d['OBSOLETE']!=None self.obsolete = d['OBSOLETE']!=None
self.applies = d['APPLIES'] self.applies = d['APPLIES']
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.applies)==TupleType assert type(self.applies) == tuple
return return
def __str__(self): def __str__(self):
@@ -517,13 +511,13 @@ class DITContentRule(SchemaElement):
self.must = d['MUST'] self.must = d['MUST']
self.may = d['MAY'] self.may = d['MAY']
self.nots = d['NOT'] self.nots = d['NOT']
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.aux)==TupleType assert type(self.aux) == tuple
assert type(self.must)==TupleType assert type(self.must) == tuple
assert type(self.may)==TupleType assert type(self.may) == tuple
assert type(self.nots)==TupleType assert type(self.nots) == tuple
return return
def __str__(self): def __str__(self):
@@ -584,11 +578,11 @@ class DITStructureRule(SchemaElement):
self.obsolete = d['OBSOLETE']!=None self.obsolete = d['OBSOLETE']!=None
self.form = d['FORM'][0] self.form = d['FORM'][0]
self.sup = d['SUP'] self.sup = d['SUP']
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.form)==StringType assert type(self.form) == str
assert type(self.sup)==TupleType assert type(self.sup) == tuple
return return
def __str__(self): def __str__(self):
@@ -648,12 +642,12 @@ class NameForm(SchemaElement):
self.oc = d['OC'][0] self.oc = d['OC'][0]
self.must = d['MUST'] self.must = d['MUST']
self.may = d['MAY'] self.may = d['MAY']
assert type(self.names)==TupleType assert type(self.names) == tuple
assert self.desc is None or type(self.desc)==StringType assert self.desc is None or type(self.desc) == str
assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1)
assert type(self.oc)==StringType assert type(self.oc) == str
assert type(self.must)==TupleType assert type(self.must) == tuple
assert type(self.may)==TupleType assert type(self.may) == tuple
return return
def __str__(self): def __str__(self):
@@ -667,7 +661,7 @@ class NameForm(SchemaElement):
return '( %s )' % ''.join(result) return '( %s )' % ''.join(result)
class Entry(UserDict.UserDict): class Entry(UserDict):
""" """
Schema-aware implementation of an LDAP entry class. Schema-aware implementation of an LDAP entry class.

View File

@@ -10,8 +10,6 @@ import ldap.cidict,ldap.schema
from ldap.schema.models import * from ldap.schema.models import *
from UserDict import UserDict
SCHEMA_CLASS_MAPPING = ldap.cidict.cidict() SCHEMA_CLASS_MAPPING = ldap.cidict.cidict()
SCHEMA_ATTR_MAPPING = {} SCHEMA_ATTR_MAPPING = {}

View File

@@ -23,9 +23,7 @@ __all__ = [
'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl' 'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl'
] ]
import UserDict from ldap.compat import UserDict, quote, unquote
from urllib import quote,unquote
LDAP_SCOPE_BASE = 0 LDAP_SCOPE_BASE = 0
LDAP_SCOPE_ONELEVEL = 1 LDAP_SCOPE_ONELEVEL = 1
@@ -134,14 +132,14 @@ class LDAPUrlExtension:
return not self.__eq__(other) return not self.__eq__(other)
class LDAPUrlExtensions(UserDict.UserDict): class LDAPUrlExtensions(UserDict):
""" """
Models a collection of LDAP URL extensions as Models a collection of LDAP URL extensions as
dictionary type dictionary type
""" """
def __init__(self,default=None): def __init__(self,default=None):
UserDict.UserDict.__init__(self) UserDict.__init__(self)
for k,v in (default or {}).items(): for k,v in (default or {}).items():
self[k]=v self[k]=v
@@ -395,10 +393,10 @@ class LDAPUrl:
) )
def __getattr__(self,name): def __getattr__(self,name):
if self.attr2extype.has_key(name): if name in self.attr2extype:
extype = self.attr2extype[name] extype = self.attr2extype[name]
if self.extensions and \ if self.extensions and \
self.extensions.has_key(extype) and \ extype in self.extensions and \
not self.extensions[extype].exvalue is None: not self.extensions[extype].exvalue is None:
result = unquote(self.extensions[extype].exvalue) result = unquote(self.extensions[extype].exvalue)
else: else:
@@ -410,7 +408,7 @@ class LDAPUrl:
return result # __getattr__() return result # __getattr__()
def __setattr__(self,name,value): def __setattr__(self,name,value):
if self.attr2extype.has_key(name): if name in self.attr2extype:
extype = self.attr2extype[name] extype = self.attr2extype[name]
if value is None: if value is None:
# A value of None means that extension is deleted # A value of None means that extension is deleted
@@ -424,7 +422,7 @@ class LDAPUrl:
self.__dict__[name] = value self.__dict__[name] = value
def __delattr__(self,name): def __delattr__(self,name):
if self.attr2extype.has_key(name): if name in self.attr2extype:
extype = self.attr2extype[name] extype = self.attr2extype[name]
if self.extensions: if self.extensions:
try: try:

View File

@@ -154,7 +154,8 @@ class Slapd:
self._log.debug("deleting existing %s", path) self._log.debug("deleting existing %s", path)
os.remove(path) os.remove(path)
self._log.debug("writing config to %s", path) self._log.debug("writing config to %s", path)
file(path, "w").writelines([line + "\n" for line in self._config]) with open(path, 'w') as f:
f.writelines([line + "\n" for line in self._config])
return path return path
def start(self): def start(self):

View File

@@ -165,7 +165,7 @@ class TestLdapCExtension(unittest.TestCase):
self.assertEquals(result, _ldap.RES_SEARCH_RESULT) self.assertEquals(result, _ldap.RES_SEARCH_RESULT)
self.assertEquals(pmsg[0][0], "") # rootDSE has no dn self.assertEquals(pmsg[0][0], "") # rootDSE has no dn
self.assertEquals(msgid, m) self.assertEquals(msgid, m)
self.assertTrue(pmsg[0][1].has_key('objectClass')) self.assertIn('objectClass', pmsg[0][1])
def test_unbind(self): def test_unbind(self):
l = self._init() l = self._init()

View File

@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import ldap, unittest import ldap, unittest
import urllib from ldap.compat import quote
from ldapurl import LDAPUrl from ldapurl import LDAPUrl
@@ -26,9 +28,9 @@ class TestLDAPUrl(unittest.TestCase):
u = MyLDAPUrl("ldap://127.0.0.1:1234/dc=example,dc=com" u = MyLDAPUrl("ldap://127.0.0.1:1234/dc=example,dc=com"
+ "?attr1,attr2,attr3" + "?attr1,attr2,attr3"
+ "?sub" + "?sub"
+ "?" + urllib.quote("(objectClass=*)") + "?" + quote("(objectClass=*)")
+ "?bindname=" + urllib.quote("cn=d,c=au") + "?bindname=" + quote("cn=d,c=au")
+ ",X-BINDPW=" + urllib.quote("???") + ",X-BINDPW=" + quote("???")
+ ",trace=8" + ",trace=8"
) )
self.assertEquals(u.urlscheme, "ldap") self.assertEquals(u.urlscheme, "ldap")

View File

@@ -163,6 +163,7 @@ setup(
'dsml', 'dsml',
'ldap', 'ldap',
'ldap.async', 'ldap.async',
'ldap.compat',
'ldap.controls', 'ldap.controls',
'ldap.controls.libldap', 'ldap.controls.libldap',
'ldap.controls.openldap', 'ldap.controls.openldap',