473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

LDAP/LDIF Parsing

All,

I am hoping someone would be able to help me with a problem. I have an
LDAP server running on a linux box, this LDAP server contains a
telephone list in various groupings, the ldif file of which is -

dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
dc: example
o: Example Organisation

dn: ou=groupa,dc=example,dc=com
ou: groupa
objectClass: top
objectClass: organizationalUnit
description: Group A

dn: cn=johnsmith,ou=groupa,dc=example,dc=com
cn: johnsmith
objectClass: top
objectClass: person
sn: Smith
telephoneNumber: 112

dn: cn=davesteel,ou=groupa,dc=example,dc=com
cn: davesteel
objectClass: top
objectClass: person
sn: Steel
telephoneNumber: 113

dn: ou=groupb,dc=example,dc=com
ou: groupb
objectClass: top
objectClass: organizationalUnit
description: Group B

dn: cn=williamdavis,ou=groupb,dc=example,dc=com
cn: williamdavis
objectClass: top
objectClass: person
sn: Davis
telephoneNumber: 122

dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com
cn: jamesjarvis
objectClass: top
objectClass: person
sn: Jarvis
telephoneNumber: 123

I am creating a python client program that will display the telephone
list in the same directory structure as is on the LDAP server (i.e. it
starts with buttons of all the groups, when you click on a group it
comes up with buttons of all the numbers or groups available, and you
can continually drill down).

I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -

('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})

Basically i think i need to parse the search results to create objects
and build the python buttons around this, but i was hoping someone
would be able to point me in the correct direction of how to do this?
Is there a parser available? (there is an ldif library available but
it is not obvious how this works, i cannot see much documentation, and
it seems to be deprecated...).

Many thanks.

Ian

Feb 1 '07 #1
10 8178
>
I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -

('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})

But this is exactly what your LDAP-record contains. What else should there
be? And no, you don't need a parser, as the above _is_ the parsed result.
No parser can possibly give you anything else.

You can of course create wrapper-objects, that you instantiate based on the
values in 'objectClass', and that allow convenient access to certain
properties. Yet this is entirely up to you, as there is no one else who can
forsee how things should look and work like in _your_ application.

Diez
Feb 1 '07 #2
Cruelemort a écrit :
All,

I am hoping someone would be able to help me with a problem. I have an
LDAP server running on a linux box, this LDAP server contains a
telephone list in various groupings, the ldif file of which is -
(snip)
>
I am creating a python client program that will display the telephone
list in the same directory structure as is on the LDAP server (i.e. it
starts with buttons of all the groups, when you click on a group it
comes up with buttons of all the numbers or groups available, and you
can continually drill down).

I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -

('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})
What's your problem ? That's exactly what your ldap record should look
like. A (base_dn, record) tuple, where the record is a dict of
attribute_name:[values, ...]
Basically i think i need to parse the search results to create objects
Q&D wrapper:

class LdapObject(object):
def __init__(self, ldapentry):
self.dn, self._record = ldapentry

def __getattr__(self, name):
try:
data = self._record[name]
except KeyError:
raise AttributeError(
"object %s has no attribute %s" % (self, name)
)
else:
# all LDAP attribs are multivalued by default,
# even when the schema says they are monovalued
if len(data) == 1:
return data[0]
else:
return data[:]

def isa(self, objectClass):
return objectClass in self.objectClass:

root = LdapObject(
('dc=example,dc=com',
{'objectClass': ['top', 'dcObject','organization'],
'dc': ['example'],
'o': ['Example Organisation']}
))

root.o
='Example Organisation'
root.objectClass
=['top', 'dcObject','organization']
root.isa('organization')
=True

FWIW, I once started writing an higher-level LDAP api (kind of an
Object-LDAP Mapper...) using descriptors for ldap attribute access, but
I never finished the damned thing, and it's in a very sorry state. I'll
have to get back to it one day...
and build the python buttons around this, but i was hoping someone
would be able to point me in the correct direction of how to do this?
Is there a parser available?
cf above.
Feb 1 '07 #3

The tree hierarchy is defined by the DN of each object, the types of
the object is specified by its objectClass.
Just collect all items (or do it dynamically by tunning the scope and
the base of your search request)
On 1 fév, 18:22, "Cruelemort" <ian.ing...@gmail.comwrote:
All,

I am hoping someone would be able to help me with a problem. I have an
LDAP server running on a linux box, this LDAP server contains a
telephone list in various groupings, the ldif file of which is -

dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
dc: example
o: Example Organisation

dn: ou=groupa,dc=example,dc=com
ou: groupa
objectClass: top
objectClass: organizationalUnit
description: Group A

dn: cn=johnsmith,ou=groupa,dc=example,dc=com
cn: johnsmith
objectClass: top
objectClass: person
sn: Smith
telephoneNumber: 112

dn: cn=davesteel,ou=groupa,dc=example,dc=com
cn: davesteel
objectClass: top
objectClass: person
sn: Steel
telephoneNumber: 113

dn: ou=groupb,dc=example,dc=com
ou: groupb
objectClass: top
objectClass: organizationalUnit
description: Group B

dn: cn=williamdavis,ou=groupb,dc=example,dc=com
cn: williamdavis
objectClass: top
objectClass: person
sn: Davis
telephoneNumber: 122

dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com
cn: jamesjarvis
objectClass: top
objectClass: person
sn: Jarvis
telephoneNumber: 123

I am creating a python client program that will display the telephone
list in the same directory structure as is on the LDAP server (i.e. it
starts with buttons of all the groups, when you click on a group it
comes up with buttons of all the numbers or groups available, and you
can continually drill down).

I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -

('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})

Basically i think i need to parse the search results to create objects
and build the python buttons around this, but i was hoping someone
would be able to point me in the correct direction of how to do this?
Is there a parser available? (there is an ldif library available but
it is not obvious how this works, i cannot see much documentation, and
it seems to be deprecated...).

Many thanks.

Ian

Feb 1 '07 #4
On Feb 1, 11:08 pm, "aspineux" <aspin...@gmail.comwrote:
The tree hierarchy is defined by the DN of each object, the types of
the object is specified by its objectClass.
Just collect all items (or do it dynamically by tunning the scope and
the base of your search request)

On 1 fév, 18:22, "Cruelemort" <ian.ing...@gmail.comwrote:
All,
I am hoping someone would be able to help me with a problem. I have an
LDAP server running on a linux box, this LDAP server contains a
telephone list in various groupings, the ldif file of which is -
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
dc: example
o: Example Organisation
dn: ou=groupa,dc=example,dc=com
ou: groupa
objectClass: top
objectClass: organizationalUnit
description: Group A
dn: cn=johnsmith,ou=groupa,dc=example,dc=com
cn: johnsmith
objectClass: top
objectClass: person
sn: Smith
telephoneNumber: 112
dn: cn=davesteel,ou=groupa,dc=example,dc=com
cn: davesteel
objectClass: top
objectClass: person
sn: Steel
telephoneNumber: 113
dn: ou=groupb,dc=example,dc=com
ou: groupb
objectClass: top
objectClass: organizationalUnit
description: Group B
dn: cn=williamdavis,ou=groupb,dc=example,dc=com
cn: williamdavis
objectClass: top
objectClass: person
sn: Davis
telephoneNumber: 122
dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com
cn: jamesjarvis
objectClass: top
objectClass: person
sn: Jarvis
telephoneNumber: 123
I am creating a python client program that will display the telephone
list in the same directory structure as is on the LDAP server (i.e. it
starts with buttons of all the groups, when you click on a group it
comes up with buttons of all the numbers or groups available, and you
can continually drill down).
I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -
('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})
Basically i think i need to parse the search results to create objects
and build the python buttons around this, but i was hoping someone
would be able to point me in the correct direction of how to do this?
Is there a parser available? (there is an ldif library available but
it is not obvious how this works, i cannot see much documentation, and
it seems to be deprecated...).
Many thanks.
Ian- Hide quoted text -

- Show quoted text -
Thanks for the replies all - it was a higher level wrapper like Bruno
mentioned that i was looking for (with objects and attributes based on
each objectClass), but the code posted above will work fine.

Many thanks all.

Ian

Feb 2 '07 #5
Bruno Desthuilliers writes:
class LdapObject(object):
(...)
def __getattr__(self, name):
try:
data = self._record[name]
except KeyError:
raise AttributeError(
"object %s has no attribute %s" % (self, name)
)
Note that LDAP attribute descriptions may be invalid Python
attribute names. E.g.
{...
'title;lang-en': ['The Boss']
'title;lang-no': ['Sjefen']}
So you'd have to call getattr() explicitly to get at all the attributes
this way.
else:
# all LDAP attribs are multivalued by default,
# even when the schema says they are monovalued
if len(data) == 1:
return data[0]
else:
return data[:]
IMHO, this just complicates the client code since the client needs to
inserts checks of isinstance(return value, list) all over the place.
Better to have a separate method which extracts just the first value of
an attribute, if you want that.

--
Regards,
Hallvard
Feb 2 '07 #6
Hallvard B Furuseth a écrit :
Bruno Desthuilliers writes:
>class LdapObject(object):
(...)
def __getattr__(self, name):
try:
data = self._record[name]
except KeyError:
raise AttributeError(
"object %s has no attribute %s" % (self, name)
)

Note that LDAP attribute descriptions may be invalid Python
attribute names. E.g.
{...
'title;lang-en': ['The Boss']
'title;lang-no': ['Sjefen']}
So you'd have to call getattr() explicitly to get at all the attributes
this way.
Yeps, true. Another solution would be to add a __getitem__ method
pointing to the same implementation, ie:

__getitem__ = __getattr__
> else:
# all LDAP attribs are multivalued by default,
# even when the schema says they are monovalued
if len(data) == 1:
return data[0]
else:
return data[:]

IMHO, this just complicates the client code since the client needs to
inserts checks of isinstance(return value, list) all over the place.
Better to have a separate method which extracts just the first value of
an attribute, if you want that.
Most of the times, in a situation such as the one described by the OP,
one knows by advance if a given LDAP attribute will be used as
monovalued or multivalued. Well, this is at least my own experience...

Feb 2 '07 #7
Cruelemort wrote:
I was wondering the best way to do this? I have installed and used the
python-ldap libraries and these allow me to access and search the
server, but the searches always return a horrible nesting of lists,
tuples and dictionaries, below is an example of returning just one
record -

('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
'organization'], 'dc': ['example'], 'o': ['Example Organisation']})
It's just modeled after the X.500 data model. A DN and the entry. The
entry consists of attributes which consists of attribute type and a set
of attribute values.

You could write your own wrapper class around ldap.ldapobject.LDAPObject
and overrule method search_s().
(there is an ldif library available but
it is not obvious how this works, i cannot see much documentation, and
it seems to be deprecated...).
Module ldif is not deprecated. It's actively maintained by me like the
rest of python-ldap. It parses LDIF and returns the same data structure
as above. You don't need it for LDAP access anyway. Only for reading
LDIF files.

Ciao, Michael.
Feb 2 '07 #8
Bruno Desthuilliers writes:
>Hallvard B Furuseth a écrit :
>> else:
# all LDAP attribs are multivalued by default,
# even when the schema says they are monovalued
if len(data) == 1:
return data[0]
else:
return data[:]
IMHO, this just complicates the client code since the client needs to
inserts checks of isinstance(return value, list) all over the place.
Better to have a separate method which extracts just the first value of
an attribute, if you want that.

Most of the times, in a situation such as the one described by the OP,
one knows by advance if a given LDAP attribute will be used as
monovalued or multivalued. Well, this is at least my own experience...
But if the attribute is multivalued, you don't know if it will contain
just one value or not. If you expect telephoneNumber to be multivalued,
but receive just one value '123',
for value in foo.telephoneNumber: print value
will print
1
2
3

BTW, Cruelemort, remember that attribute names are case-insensitive. If
you ask the server to for attribute "cn", it might still return "CN".

--
Hallvard
Feb 2 '07 #9
Hallvard B Furuseth a écrit :
Bruno Desthuilliers writes:
>>Hallvard B Furuseth a écrit :
>>> else:
# all LDAP attribs are multivalued by default,
# even when the schema says they are monovalued
if len(data) == 1:
return data[0]
else:
return data[:]

IMHO, this just complicates the client code since the client needs to
inserts checks of isinstance(return value, list) all over the place.
Better to have a separate method which extracts just the first value of
an attribute, if you want that.

Most of the times, in a situation such as the one described by the OP,
one knows by advance if a given LDAP attribute will be used as
monovalued or multivalued. Well, this is at least my own experience...

But if the attribute is multivalued, you don't know if it will contain
just one value or not.
If you know which attributes are supposed to be multivalued in your
specific application, then it's time to write a more serious,
application-specific wrapper.
Feb 3 '07 #10
Bruno Desthuilliers wrote:
>
If you know which attributes are supposed to be multivalued in your
specific application, then it's time to write a more serious,
application-specific wrapper.
ldap.schema can be used to find that out.

Ciao, Michael.
Feb 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thibault Roche | last post by:
Hello I have a problem with the use of ldap with php. I have an interface which is used to create users in a ldap directory. Adding users work fine, but I can't set correctly the userpassword...
0
by: Loren | last post by:
I Have an August 1st deadline to convert an XML file to an LDIF/LDAP File. Some one told me that i should use java. Can anyone help Please?! I have never programed in java before.
0
by: Loren | last post by:
I Have an August 1st deadline to convert an XML file to an LDIF/LDAP File. Some one told me that i should use perl. Can anyone help Please?! I have never programed in perl before.
0
by: bdm | last post by:
Hello, I want to insert new records into an openldap server using the assembly mentioned above. I am kind of stuck I haven't found any examples of how to do this exactly (that I understand??) ...
2
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
4
by: jade | last post by:
Hi everyone I've created a database of contacts from which I'd like to export/import in LDIF format - which would obviously permit me to import to Thunderbird :D . I've managed to export to...
3
by: flit | last post by:
Hello All, I am struggling with some ldap files. I am using the csv module to work with this files (I exported the ldap to a csv file). I have this string on a field...
1
by: Maurice | last post by:
Hello all, I think Access is the tool I need for this but I'm having a little trouble figuring out the best way to accomplish. We are converting a lot of aliases (from a sendmail alias file)...
2
by: Lars | last post by:
Hi I got some programming experience and I recently started looking into Python. I've read much of the tutorial from 2.6 documentation. But it was more interesting to get started on something I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.