Skip to content

Commit

Permalink
Fix module imports for py3 and use replacements for methods removed i…
Browse files Browse the repository at this point in the history
…n py3. (#87)
  • Loading branch information
adiroiban authored Jan 15, 2018
1 parent 3e3e7cd commit 4e2b677
Show file tree
Hide file tree
Showing 25 changed files with 460 additions and 269 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ python:
- 2.7

env:
- TOX_ENV=py27-twlatest
- TOX_ENV=py27-twtrunk
- TOX_ENV=py34-twtrunk
- TOX_ENV=py27-test-twlatest
- TOX_ENV=py27-test-twtrunk
- TOX_ENV=py34-test-twtrunk
- TOX_ENV=linters

matrix:
fast_finish: true
allow_failures:
- env: TOX_ENV=py34-twtrunk
- env: TOX_ENV=py34-test-twtrunk

install:
- pip install tox coveralls codecov

script:
- tox -e $TOX_ENV

after_success:
after_script:
- codecov
- coveralls

Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Development environment

Tox is used to manage both local development and CI environment.

The recommended local dev enviroment is `tox -e py27-dev`
The recommended local dev enviroment is `tox -e py27-test-dev`

When running on local dev env, you will get a coverage report for whole
code as well as for the changes since `master`.
Expand All @@ -32,8 +32,8 @@ The reports are also produced in HTML at:
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
tox -e py27-test-dev ldaptor.test.test_delta.TestModifyOp.testAsLDIF
tox -e py27-test-dev ldaptor.test.test_usage


Release notes
Expand Down
2 changes: 1 addition & 1 deletion docs/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ https://github.com/twisted/ldaptor/blob/master/CONTRIBUTING.rst

* [ ] I have updated the release notes at `docs/source/NEWS.rst`
* [ ] I have updated the automated tests.
* [ ] All tests pass on your local system for `tox -e py27-dev`
* [ ] All tests pass on your local dev environment. See `CONTRIBUTING.rst`.
22 changes: 10 additions & 12 deletions ldaptor/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os.path
import ConfigParser

from six.moves import configparser
from zope.interface import implementer

from ldaptor import interfaces
from ldaptor.insensitive import InsensitiveString
from ldaptor.protocols.ldap import distinguishedname


Expand Down Expand Up @@ -48,8 +47,8 @@ def getBaseDN(self):
cfg = loadConfig()
try:
return cfg.get('ldap', 'base')
except (ConfigParser.NoOptionError,
ConfigParser.NoSectionError):
except (configparser.NoOptionError,
configparser.NoSectionError):
raise MissingBaseDNError()

def getServiceLocationOverrides(self):
Expand Down Expand Up @@ -99,8 +98,8 @@ def getIdentityBaseDN(self):
cfg = loadConfig()
try:
return cfg.get('authentication', 'identity-base')
except (ConfigParser.NoOptionError,
ConfigParser.NoSectionError):
except (configparser.NoOptionError,
configparser.NoSectionError):
return self.getBaseDN()

def getIdentitySearch(self, name):
Expand All @@ -113,10 +112,10 @@ def getIdentitySearch(self, name):
else:
cfg = loadConfig()
try:
f=cfg.get('authentication', 'identity-search', vars=data)
except (ConfigParser.NoOptionError,
ConfigParser.NoSectionError):
f='(|(cn=%(name)s)(uid=%(name)s))' % data
f = cfg.get('authentication', 'identity-search', vars=data)
except (configparser.NoOptionError,
configparser.NoSectionError):
f = '(|(cn=%(name)s)(uid=%(name)s))' % data
return f


Expand All @@ -139,8 +138,7 @@ def loadConfig(configFiles=None,
"""
global __config
if __config is None or reload:
x = ConfigParser.SafeConfigParser()
x.optionxform = InsensitiveString
x = configparser.SafeConfigParser()

for section, options in DEFAULTS.items():
x.add_section(section)
Expand Down
5 changes: 4 additions & 1 deletion ldaptor/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
(This means these do not belong here: adding or deleting of entries,
changing of location in tree)
"""
import six

from ldaptor import attributeset
from ldaptor.protocols import pureldap, pureber
from ldaptor.protocols.ldap import ldif, distinguishedname


class Modification(attributeset.LDAPAttributeSet):
def patch(self, entry):
raise NotImplementedError(
Expand All @@ -22,7 +25,7 @@ def asLDAP(self):
tmplist = list(self)
newlist = []
for x in range(len(tmplist)):
if (isinstance(tmplist[x], unicode)):
if (isinstance(tmplist[x], six.text_type)):
value = tmplist[x].encode('utf-8')
newlist.append(value)
else:
Expand Down
21 changes: 8 additions & 13 deletions ldaptor/dns.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"""DNS-related utilities."""

from socket import inet_aton, inet_ntoa
import struct


def aton_octets(ip):
s = inet_aton(ip)
octets = list(s)
n = 0
for o in octets:
n = n << 8
n += ord(o)
return n
return struct.unpack('!I', s)[0]


def aton_numbits(num):
n = 0
Expand All @@ -20,6 +17,7 @@ def aton_numbits(num):
num-=1
return n


def aton(ip):
try:
i=int(ip)
Expand All @@ -28,16 +26,13 @@ def aton(ip):
else:
return aton_numbits(i)


def ntoa(n):
s=(
chr((n>>24)&0xFF)
+ chr((n>>16)&0xFF)
+ chr((n>>8)&0xFF)
+ chr(n&0xFF)
)
ip=inet_ntoa(s)
s = struct.pack('!I', n)
ip = inet_ntoa(s)
return ip


def netmaskToNumbits(netmask):
bits = aton(netmask)
i = 2**31
Expand Down
51 changes: 0 additions & 51 deletions ldaptor/insensitive.py

This file was deleted.

16 changes: 8 additions & 8 deletions ldaptor/ldiftree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""
import errno
import os
import uuid

from twisted.internet import defer, error
from twisted.python import failure
from twisted.mail.maildir import _generateMaildirName as tempName
from zope.interface import implementer

from ldaptor import entry, interfaces, attributeset, entryhelpers
Expand Down Expand Up @@ -57,7 +57,7 @@ def _get(path, dn):
entry = os.path.join(path,
*['%s.dir' % rdn for rdn in l[:-1]])
entry = os.path.join(entry, '%s.ldif' % l[-1])
f = file(entry)
f = open(entry)
while 1:
data = f.read(8192)
if not data:
Expand All @@ -77,8 +77,8 @@ def _get(path, dn):

def _putEntry(fileName, entry):
"""fileName is without extension."""
tmp = fileName + '.' + tempName() + '.tmp'
f = file(tmp, 'w')
tmp = fileName + '.' + str(uuid.uuid4()) + '.tmp'
f = open(tmp, 'w')
f.write(str(entry))
f.close()
os.rename(tmp, fileName+'.ldif')
Expand Down Expand Up @@ -139,7 +139,7 @@ def _load(self):
parser = StoreParsedLDIF()

try:
f = file(entryPath)
f = open(entryPath)
except IOError as e:
if e.errno == errno.ENOENT:
return
Expand Down Expand Up @@ -241,8 +241,8 @@ def _addChild(self, rdn, attributes):
if not os.path.exists(self.path):
os.mkdir(self.path)
fileName = os.path.join(self.path, '%s' % rdn)
tmp = fileName + '.' + tempName() + '.tmp'
f = file(tmp, 'w')
tmp = fileName + '.' + str(uuid.uuid4()) + '.tmp'
f = open(tmp, 'w')
f.write(str(e))
f.close()
os.rename(tmp, fileName+'.ldif')
Expand Down Expand Up @@ -287,7 +287,7 @@ def __repr__(self):
def __cmp__(self, other):
if not isinstance(other, LDIFTreeEntry):
return NotImplemented
return cmp(self.dn, other.dn)
return (self.dn > other.dn) - (self.dn < other.dn)

def commit(self):
assert self.path.endswith('.dir')
Expand Down
27 changes: 17 additions & 10 deletions ldaptor/protocols/ldap/distinguishedname.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# See rfc2253
from functools import total_ordering

import six

# See rfc2253
# Note that RFC 2253 sections 2.4 and 3 disagree whether "=" needs to
# be quoted. Let's trust the syntax, slapd refuses to accept unescaped
# "=" in RDN values.
Expand Down Expand Up @@ -151,15 +154,14 @@ def __init__(self, magic=None, stringValue=None, attributeTypesAndValues=None):
assert attributeTypesAndValues is None
if isinstance(magic, RelativeDistinguishedName):
attributeTypesAndValues = magic.split()
elif isinstance(magic, basestring):
elif isinstance(magic, six.string_types):
stringValue = magic
else:
attributeTypesAndValues = magic

if stringValue is None:
assert attributeTypesAndValues is not None
import types
assert not isinstance(attributeTypesAndValues, types.StringType)
assert not isinstance(attributeTypesAndValues, six.string_types)
self.attributeTypesAndValues = tuple(attributeTypesAndValues)
else:
assert attributeTypesAndValues is None
Expand Down Expand Up @@ -208,6 +210,7 @@ def count(self):
return len(self.attributeTypesAndValues)


@total_ordering
class DistinguishedName:
"""LDAP Distinguished Name."""
listOfRDNs = None
Expand All @@ -221,7 +224,7 @@ def __init__(self, magic=None, stringValue=None, listOfRDNs=None):
assert listOfRDNs is None
if isinstance(magic, DistinguishedName):
listOfRDNs = magic.split()
elif isinstance(magic, basestring):
elif isinstance(magic, six.string_types):
stringValue = magic
else:
listOfRDNs = magic
Expand Down Expand Up @@ -255,7 +258,7 @@ def __hash__(self):
return hash(str(self))

def __eq__(self, other):
if isinstance(other, basestring):
if isinstance(other, six.string_types):
return str(self) == other
if not isinstance(other, DistinguishedName):
return NotImplemented
Expand All @@ -264,12 +267,16 @@ def __eq__(self, other):
def __ne__(self, other):
return not (self == other)

def __cmp__(self, other):
if isinstance(other, basestring):
return cmp(str(self), other)
def __lt__(self, other):
"""
Comparison used for determining the hierarchy.
"""
if not isinstance(other, DistinguishedName):
return NotImplemented
return cmp(self.split(), other.split())

# The comparison is naive and broken.
# See https://github.com/twisted/ldaptor/issues/94
return self.split() < other.split()

def getDomainName(self):
domainParts = []
Expand Down
Loading

0 comments on commit 4e2b677

Please sign in to comment.