Skip to content

Commit

Permalink
Use absolute imports and fix all python2 -3 warnings when running tes…
Browse files Browse the repository at this point in the history
…ts. (#86)
  • Loading branch information
adiroiban committed Jan 14, 2018
1 parent 00d9f8e commit 3e3e7cd
Show file tree
Hide file tree
Showing 21 changed files with 703 additions and 176 deletions.
7 changes: 7 additions & 0 deletions CONTRIBUTING.rst
Expand Up @@ -25,9 +25,16 @@ The recommended local dev enviroment is `tox -e py27-dev`
When running on local dev env, you will get a coverage report for whole
code as well as for the changes since `master`.
The reports are also produced in HTML at:

* build/coverage-html/index.html
* build/coverage-diff.html

You can run a subset of the test by passing the dotted path to the test or
test case, test module or test package::

tox -e py27-dev ldaptor.test.test_delta.TestModifyOp.testAsLDIF
tox -e py27-dev ldaptor.test.test_usage


Release notes
-------------
Expand Down
6 changes: 4 additions & 2 deletions docs/source/NEWS.rst
Expand Up @@ -2,7 +2,7 @@ Changelog
=========


Release 17.1 (UNRELEASED)
Release 18.0 (UNRELEASED)
-------------------------

Features
Expand All @@ -21,11 +21,13 @@ Changes
Twisted trunk branch.
- The local development environment was updated to produce overall and diff
coverage reports in HTML format.
- `six` package is now a direct dependency in preparation for the Python 3
port.

Bugfixes
^^^^^^^^

- DN matching now case insensitive.
- DN matching is now case insensitive.


Release 16.0 (2016-06-07)
Expand Down
22 changes: 18 additions & 4 deletions ldaptor/delta.py
Expand Up @@ -185,16 +185,22 @@ def __repr__(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return 0
return NotImplemented
if self.dn != other.dn:
return 0
if self.modifications != other.modifications:
return 0
return 1

def __hash__(self):
# We use the LDIF representation as similar objects
# should have the same LDIF.
return hash(self.asLDIF())

def __ne__(self, other):
return not self==other


class AddOp(Operation):
def __init__(self, entry):
self.entry = entry
Expand All @@ -220,13 +226,19 @@ def __repr__(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
if self.entry != other.entry:
return False
return True

def __ne__(self, other):
return not self==other
return not self == other

def __hash__(self):
# Use the LDIF representions as equal operations should
# have the same LDIF.
return hash(self.asLDIF())


class DeleteOp(Operation):
def __init__(self, dn):
Expand Down Expand Up @@ -254,11 +266,13 @@ def __repr__(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
if self.dn != other.dn:
return False
return True

def __ne__(self, other):
return not self==other

def __hash__(self):
return hash(self.dn)
9 changes: 5 additions & 4 deletions ldaptor/dns.py
Expand Up @@ -2,13 +2,14 @@

from socket import inet_aton, inet_ntoa


def aton_octets(ip):
s=inet_aton(ip)
octets=map(None, s)
s = inet_aton(ip)
octets = list(s)
n = 0
for o in octets:
n=n<<8
n+=ord(o)
n = n << 8
n += ord(o)
return n

def aton_numbits(num):
Expand Down
3 changes: 1 addition & 2 deletions ldaptor/md4.py
Expand Up @@ -17,7 +17,7 @@
import struct
from warnings import warn
# site
from compat import b, bytes, bascii_to_str, irange # , PY3 # twisted isn't Python3, yet
from ldaptor.compat import b, bytes, bascii_to_str, irange, PYPY
# local
__all__ = ["md4"]

Expand Down Expand Up @@ -244,7 +244,6 @@ def hexdigest(self):
# check if hashlib provides accelarated md4
# =============================================================================
import hashlib
from compat import PYPY


def _has_native_md4(): # pragma: no cover -- runtime detection
Expand Down
3 changes: 2 additions & 1 deletion ldaptor/numberalloc.py
@@ -1,4 +1,5 @@
"""Find an available uidNumber/gidNumber/other similar number."""
from __future__ import division

from ldaptor.protocols import pureldap

Expand Down Expand Up @@ -29,7 +30,7 @@ def _nextGuess(self, found, lastGuess):
if max is None:
max=self.min+1000

guess=(max+self.min)/2
guess = (max + self.min) // 2
d=self.makeAGuess(guess)
d.addCallback(self._nextGuess, guess)
return d
Expand Down
2 changes: 1 addition & 1 deletion ldaptor/protocols/ldap/ldapclient.py
Expand Up @@ -88,7 +88,7 @@ def _send(self, op):
msg = pureldap.LDAPMessage(op)
if self.debug:
log.msg('C->S %s' % repr(msg))
assert not self.onwire.has_key(msg.id)
assert msg.id not in self.onwire
return msg

def _cbSend(self, msg, d):
Expand Down
2 changes: 1 addition & 1 deletion ldaptor/protocols/ldap/ldapconnector.py
Expand Up @@ -48,7 +48,7 @@ def __getstate__(self):

def _findOverRide(self, dn, overrides):
while True:
if overrides.has_key(dn):
if dn in overrides:
return overrides[dn]
if dn == '':
break
Expand Down
15 changes: 9 additions & 6 deletions ldaptor/protocols/ldap/ldaperrors.py
Expand Up @@ -81,7 +81,7 @@ def __str__(self):
else:
return codeName

import new

def init(**errors):
global reverse
reverse = {}
Expand All @@ -90,11 +90,14 @@ def init(**errors):
klass = Success
else:
classname = 'LDAP'+name[0].upper()+name[1:]
klass = new.classobj(classname,
(LDAPException,),
{ 'resultCode': value,
'name': name,
})
klass = type(
classname,
(LDAPException,),
{
'resultCode': value,
'name': name,
},
)
globals()[classname] = klass
reverse[value] = klass

Expand Down
17 changes: 11 additions & 6 deletions ldaptor/protocols/ldap/ldapsyntax.py
@@ -1,4 +1,5 @@
"""Pythonic API for LDAP operations."""
import functools

from twisted.internet import defer
from twisted.python.failure import Failure
Expand Down Expand Up @@ -247,22 +248,22 @@ def __str__(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return 0
return NotImplemented
if self.dn != other.dn:
return 0
return False

my=self.keys()
my.sort()
its=other.keys()
its.sort()
if my != its:
return 0
return False
for key in my:
myAttr = self[key]
itsAttr = other[key]
if myAttr != itsAttr:
return 0
return 1
return False
return True

def __ne__(self, other):
return not self == other
Expand All @@ -273,6 +274,9 @@ def __len__(self):
def __nonzero__(self):
return True

def __hash__(self):
return hash(str(self))

def bind(self, password):
r = pureldap.LDAPBindRequest(dn=str(self.dn), auth=password)
d = self.client.send(r)
Expand Down Expand Up @@ -563,7 +567,8 @@ def _passwordChangerPriorityComparison(me, other):

prefix = 'setPasswordMaybe_'
names = [name[len(prefix):] for name in dir(self) if name.startswith(prefix)]
names.sort(_passwordChangerPriorityComparison)
names.sort(
key=functools.cmp_to_key(_passwordChangerPriorityComparison))

d = defer.maybeDeferred(self._setPasswordAll,
[],
Expand Down
30 changes: 17 additions & 13 deletions ldaptor/protocols/pureber.py
Expand Up @@ -30,9 +30,10 @@
# Only some BOOLEAN and INTEGER types have default values in
# this protocol definition.


import string

from six.moves import UserList

# xxxxxxxx
# |/|\.../
# | | |
Expand Down Expand Up @@ -70,7 +71,6 @@ def __str__(self):
return "BERDecoderContext has no tag 0x%02x: %s" \
% (self.tag, self.context)

import UserList

def berDecodeLength(m, offset=0):
"""
Expand Down Expand Up @@ -117,6 +117,7 @@ def ber2int(e, signed=True):
v = (v << 8) | ord(e[i])
return v


class BERBase(object):
tag = None

Expand All @@ -137,16 +138,19 @@ def __cmp__(self, other):
return -1

def __eq__(self, other):
if isinstance(other, BERBase):
return str(self) == str(other)
else:
return False
if not isinstance(other, BERBase):
return NotImplemented

return str(self) == str(other)

def __ne__(self, other):
if isinstance(other, BERBase):
return str(self) != str(other)
else:
return False
if not isinstance(other, BERBase):
return NotImplemented

return str(self) != str(other)

def __hash__(self):
return hash(str(self))


class BERStructured(BERBase):
Expand Down Expand Up @@ -289,7 +293,8 @@ def __repr__(self):
class BEREnumerated(BERInteger):
tag = 0x0a

class BERSequence(BERStructured, UserList.UserList):

class BERSequence(BERStructured, UserList):
# TODO __getslice__ calls __init__ with no args.
tag = 0x10

Expand All @@ -301,9 +306,8 @@ def fromBER(klass, tag, content, berdecoder=None):

def __init__(self, value=None, tag=None):
BERStructured.__init__(self, tag)
UserList.UserList.__init__(self)
assert value is not None
self[:] = value
UserList.__init__(self, value)

def __str__(self):
r=string.join(map(str, self.data), '')
Expand Down
2 changes: 1 addition & 1 deletion ldaptor/protocols/pureldap.py
Expand Up @@ -17,7 +17,7 @@

import string

from pureber import (
from ldaptor.protocols.pureber import (

BERBoolean, BERDecoderContext, BEREnumerated, BERInteger, BERNull,
BEROctetString, BERSequence, BERSequenceOf, BERSet, BERStructured,
Expand Down
11 changes: 5 additions & 6 deletions ldaptor/test/test_autofill_samba.py
@@ -1,9 +1,8 @@
"""
Test cases for ldaptor.protocols.ldap.autofill.sambaAccount module.
"""

import sets
from twisted.trial import unittest

from ldaptor.protocols.ldap import ldapsyntax
from ldaptor.protocols.ldap.autofill import sambaAccount, sambaSamAccount
from ldaptor import testutil
Expand Down Expand Up @@ -153,15 +152,15 @@ def testDefaultSetting(self):
def cb(dummy):
client.assertNothingSent()

self.failUnlessEqual(sets.Set(o.keys()), sets.Set([
self.failUnlessEqual(set(o.keys()), {
'objectClass',
'sambaAcctFlags',
'sambaLogoffTime',
'sambaLogonTime',
'sambaPwdCanChange',
'sambaPwdLastSet',
'sambaPwdMustChange',
]))
})

self.failUnlessEqual(o['sambaAcctFlags'], ['[UX ]'])
self.failUnlessEqual(o['sambaPwdLastSet'], ['1'])
Expand All @@ -187,7 +186,7 @@ def testDefaultSetting_fixedPrimaryGroupSID(self):
def cb(dummy):
client.assertNothingSent()

self.failUnlessEqual(sets.Set(o.keys()), sets.Set([
self.failUnlessEqual(set(o.keys()), {
'objectClass',
'sambaAcctFlags',
'sambaLogoffTime',
Expand All @@ -196,7 +195,7 @@ def cb(dummy):
'sambaPwdLastSet',
'sambaPwdMustChange',
'sambaPrimaryGroupSID',
]))
})

self.failUnlessEqual(o['sambaPrimaryGroupSID'], ['foo-4131312'])
self.failUnlessEqual(o['sambaAcctFlags'], ['[UX ]'])
Expand Down

0 comments on commit 3e3e7cd

Please sign in to comment.