[lxml-dev] Useful snippet: namespace objects

I've attached a piece of code that I wrote to make dealing with namespaces a bit easier in lxml and ElementTree. You create a namespace object with the short prefix and the URI of the namespace. You generate fully qualified tag names through __getattr__ hackery (or __getitem__ hackery if the tag isn't a valid Python identifier). rdf = XMLNS('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') xsl = XMLNS('xsl', 'http://www.w3.org/1999/XSL/Transform') dc = XMLNS('dc', 'http://purl.org/dc/elements/1.1/') rdf.RDF == '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF' xsl['value-of'] == '{http://www.w3.org/1999/XSL/Transform}value-of' Also, there's a convenience function nsmap(*args) which will create the appropriate dictionary for passing to the nsmap keyword argument of etree.Element(). E.g. etree.Element(rdf.RDF, nsmap=nsmap(rdf, dc)) I've found this to be quite handy. -- Robert Kern rkern@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter #***************************************************************************** # Copyright (C) 2005 Robert Kern. All rights reserved. # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #***************************************************************************** class XMLNS(object): """Convenience object for handling namespaced tag names in ElementTree-like APIs. ElementTree-like APIs use an inconvenient syntax for representing tags within namespaces. E.g. rdf:RDF -> {http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF dc:creator -> {http://purl.org/dc/elements/1.1/}creator Instances of XMLNS use __getattr__ magic to generate tags with this syntax. You can also use __getitem__ for those tags which are not valid Python identifiers. rdf = XMLNS('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') xsl = XMLNS('xsl', 'http://www.w3.org/1999/XSL/Transform') rdf.RDF == '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF' xsl['value-of'] == '{http://www.w3.org/1999/XSL/Transform}value-of' Constructor: XMLNS(prefix, url) """ def __init__(self, prefix, url): self._url = url self._qname_prefix = u'{%s}'%url self._prefix = prefix def __getitem__(self, tag): return self._qname_prefix + tag __getattr__ = __getitem__ def nsmap(*args): """Create a dictionary mapping prefixes to namespace URLs from XMLNS instances. """ d = {} for ns in args: d[ns._prefix] = ns._url return d rdf = XMLNS('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') dc = XMLNS('dc', 'http://purl.org/dc/elements/1.1/') xsl = XMLNS('xsl', 'http://www.w3.org/1999/XSL/Transform')

Hello ... i'm packaging(my first try) my application for debian, and i want to know if there is a lxml debian package somewhere, to link it ? is anybody has done that ? or is it a simple way to build it as a deb package ? (i'm a newbie in building deb package) cheers marc

Andrey Tatarinov wrote:
the simpliest way to get .deb package for lxml is to build it as .rpm with "python setup.py bdist_rpm" and then convert it with alien.
Maybe this can help too: http://easy-deb.sourceforge.net/ Best, -- Olivier

the simpliest way to get .deb package for lxml is to build it as .rpm with "python setup.py bdist_rpm" and then convert it with alien.
great idea ! but i've got that : sudo python setup.py bdist_rpm Password: running bdist_rpm writing 'build/bdist.linux-i686/rpm/SPECS/lxml.spec' running sdist warning: sdist: missing required meta-data: url reading manifest file 'MANIFEST' creating lxml-0.7 creating lxml-0.7/src creating lxml-0.7/src/lxml creating lxml-0.7/src/lxml/tests making hard links in lxml-0.7... hard linking README.txt -> lxml-0.7 hard linking setup.py -> lxml-0.7 hard linking src/lxml/__init__.py -> lxml-0.7/src/lxml hard linking src/lxml/_elementpath.py -> lxml-0.7/src/lxml hard linking src/lxml/etree.pyx -> lxml-0.7/src/lxml hard linking src/lxml/tests/__init__.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_etree.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_unicode.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_xpathevaluator.py -> lxml-0.7 /src/lxml/tests tar -cf dist/lxml-0.7.tar lxml-0.7 gzip -f9 dist/lxml-0.7.tar removing 'lxml-0.7' (and everything under it) copying dist/lxml-0.7.tar.gz -> build/bdist.linux-i686/rpm/SOURCES building RPMs rpmbuild -ba --define _topdir /home/manatlan/lxml/build/bdist.linux-i686/rpm --clean build/bdist.linux-i686/rpm/SPECS/lxml.spec Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.74785 + umask 022 + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + rm -rf lxml-0.7 + /bin/gzip -dc /home/manatlan/lxml/build/bdist.linux-i686/rpm/SOURCES/lxml- 0.7.tar.gz + tar -xvvf - drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/lxml/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/lxml/tests/ -rw-r--r-- manatlan/manatlan 21 2004-07-09 13:25:27 lxml-0.7 /src/lxml/tests/__init__.py -rw-r--r-- manatlan/manatlan 59538 2005-06-15 14:44:49 lxml-0.7 /src/lxml/tests/test_etree.py -rw-r--r-- manatlan/manatlan 776 2005-04-09 18:37:09 lxml-0.7 /src/lxml/tests/test_unicode.py -rw-r--r-- manatlan/manatlan 7576 2005-05-26 16:12:24 lxml-0.7 /src/lxml/tests/test_xpathevaluator.py -rw-r--r-- manatlan/manatlan 21 2004-07-09 13:25:27 lxml-0.7 /src/lxml/__init__.py -rw-r--r-- manatlan/manatlan 5956 2005-01-12 16:18:11 lxml-0.7 /src/lxml/_elementpath.py -rw-r--r-- manatlan/manatlan 64750 2005-06-15 14:44:49 lxml-0.7 /src/lxml/etree.pyx -rw-r--r-- manatlan/manatlan 82 2005-04-08 19:31:26 lxml-0.7/README.txt -rw-r--r-- manatlan/manatlan 6649 2005-04-15 19:53:04 lxml-0.7/setup.py -rw-r--r-- root/root 182 2005-09-16 14:29:51 lxml-0.7/PKG-INFO + STATUS=0 + '[' 0 -ne 0 ']' + cd lxml-0.7 + exit 0 Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.74785 + umask 022 + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + cd lxml-0.7 + env 'CFLAGS=-O2 -march=i386 -mcpu=i686' python setup.py build Traceback (most recent call last): File "setup.py", line 12, in ? from lxmldistutils import build_ext ImportError: No module named lxmldistutils error: Bad exit status from /var/tmp/rpm-tmp.74785 (%build) RPM build errors: Bad exit status from /var/tmp/rpm-tmp.74785 (%build) error: command 'rpmbuild' failed with exit status 1 On 9/16/05, Andrey Tatarinov <elephantum@cyberzoo.ru> wrote:

i've make a deb package (my first one !), by hand i've build it on "ubuntu hoary" (i386) it's here : http://jbrout.free.fr/lxml-0.7.i386.deb (here are the depends : python (>=2.4), libxml2 (>=2.6), libxslt1.1 (>=1.1) ) like i said, i've build it by hand, it's my first one, and i don't understand all ;-) but a "dpkg -i" install it right on my system ... if anybody can test it on a debian system ... On 9/16/05, manatlan <manatlan@gmail.com> wrote:

i've made a better deb package ... http://jbrout.free.fr/download/debian/binary/python2.4-lxml-0.7.i386.deb (more pythonic ...) i had put theses dependances : libc6 (>= 2.2.5-13), libgcc1 (>= 1:3.0.3-1), libxml2 (>= 2.6.16), libxslt1.1(>= 1.1.12), python2.4 (like python2.4-libmxml2 / python2.4-libxslt1 for my ubuntu hoary) On 9/17/05, manatlan <manatlan@gmail.com> wrote:

note that someone has done a archlinux package of lxml (0.7) too (for my program, see : http://jbrout.python-hosting.com/wiki/Download ) it's here : http://aur.archlinux.org/packages.php?do_Details=1&ID=2149&O=0&L=0&C=0&K=jbrout&SB=n&SO=a&PP=25&do_MyPackages=0&do_Orphans=0 On 9/18/05, manatlan <manatlan@gmail.com> wrote:

Hello ... i'm packaging(my first try) my application for debian, and i want to know if there is a lxml debian package somewhere, to link it ? is anybody has done that ? or is it a simple way to build it as a deb package ? (i'm a newbie in building deb package) cheers marc

Andrey Tatarinov wrote:
the simpliest way to get .deb package for lxml is to build it as .rpm with "python setup.py bdist_rpm" and then convert it with alien.
Maybe this can help too: http://easy-deb.sourceforge.net/ Best, -- Olivier

the simpliest way to get .deb package for lxml is to build it as .rpm with "python setup.py bdist_rpm" and then convert it with alien.
great idea ! but i've got that : sudo python setup.py bdist_rpm Password: running bdist_rpm writing 'build/bdist.linux-i686/rpm/SPECS/lxml.spec' running sdist warning: sdist: missing required meta-data: url reading manifest file 'MANIFEST' creating lxml-0.7 creating lxml-0.7/src creating lxml-0.7/src/lxml creating lxml-0.7/src/lxml/tests making hard links in lxml-0.7... hard linking README.txt -> lxml-0.7 hard linking setup.py -> lxml-0.7 hard linking src/lxml/__init__.py -> lxml-0.7/src/lxml hard linking src/lxml/_elementpath.py -> lxml-0.7/src/lxml hard linking src/lxml/etree.pyx -> lxml-0.7/src/lxml hard linking src/lxml/tests/__init__.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_etree.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_unicode.py -> lxml-0.7/src/lxml/tests hard linking src/lxml/tests/test_xpathevaluator.py -> lxml-0.7 /src/lxml/tests tar -cf dist/lxml-0.7.tar lxml-0.7 gzip -f9 dist/lxml-0.7.tar removing 'lxml-0.7' (and everything under it) copying dist/lxml-0.7.tar.gz -> build/bdist.linux-i686/rpm/SOURCES building RPMs rpmbuild -ba --define _topdir /home/manatlan/lxml/build/bdist.linux-i686/rpm --clean build/bdist.linux-i686/rpm/SPECS/lxml.spec Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.74785 + umask 022 + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + rm -rf lxml-0.7 + /bin/gzip -dc /home/manatlan/lxml/build/bdist.linux-i686/rpm/SOURCES/lxml- 0.7.tar.gz + tar -xvvf - drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/lxml/ drwxr-xr-x root/root 0 2005-09-16 14:29:51 lxml-0.7/src/lxml/tests/ -rw-r--r-- manatlan/manatlan 21 2004-07-09 13:25:27 lxml-0.7 /src/lxml/tests/__init__.py -rw-r--r-- manatlan/manatlan 59538 2005-06-15 14:44:49 lxml-0.7 /src/lxml/tests/test_etree.py -rw-r--r-- manatlan/manatlan 776 2005-04-09 18:37:09 lxml-0.7 /src/lxml/tests/test_unicode.py -rw-r--r-- manatlan/manatlan 7576 2005-05-26 16:12:24 lxml-0.7 /src/lxml/tests/test_xpathevaluator.py -rw-r--r-- manatlan/manatlan 21 2004-07-09 13:25:27 lxml-0.7 /src/lxml/__init__.py -rw-r--r-- manatlan/manatlan 5956 2005-01-12 16:18:11 lxml-0.7 /src/lxml/_elementpath.py -rw-r--r-- manatlan/manatlan 64750 2005-06-15 14:44:49 lxml-0.7 /src/lxml/etree.pyx -rw-r--r-- manatlan/manatlan 82 2005-04-08 19:31:26 lxml-0.7/README.txt -rw-r--r-- manatlan/manatlan 6649 2005-04-15 19:53:04 lxml-0.7/setup.py -rw-r--r-- root/root 182 2005-09-16 14:29:51 lxml-0.7/PKG-INFO + STATUS=0 + '[' 0 -ne 0 ']' + cd lxml-0.7 + exit 0 Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.74785 + umask 022 + cd /home/manatlan/lxml/build/bdist.linux-i686/rpm/BUILD + cd lxml-0.7 + env 'CFLAGS=-O2 -march=i386 -mcpu=i686' python setup.py build Traceback (most recent call last): File "setup.py", line 12, in ? from lxmldistutils import build_ext ImportError: No module named lxmldistutils error: Bad exit status from /var/tmp/rpm-tmp.74785 (%build) RPM build errors: Bad exit status from /var/tmp/rpm-tmp.74785 (%build) error: command 'rpmbuild' failed with exit status 1 On 9/16/05, Andrey Tatarinov <elephantum@cyberzoo.ru> wrote:

i've make a deb package (my first one !), by hand i've build it on "ubuntu hoary" (i386) it's here : http://jbrout.free.fr/lxml-0.7.i386.deb (here are the depends : python (>=2.4), libxml2 (>=2.6), libxslt1.1 (>=1.1) ) like i said, i've build it by hand, it's my first one, and i don't understand all ;-) but a "dpkg -i" install it right on my system ... if anybody can test it on a debian system ... On 9/16/05, manatlan <manatlan@gmail.com> wrote:

i've made a better deb package ... http://jbrout.free.fr/download/debian/binary/python2.4-lxml-0.7.i386.deb (more pythonic ...) i had put theses dependances : libc6 (>= 2.2.5-13), libgcc1 (>= 1:3.0.3-1), libxml2 (>= 2.6.16), libxslt1.1(>= 1.1.12), python2.4 (like python2.4-libmxml2 / python2.4-libxslt1 for my ubuntu hoary) On 9/17/05, manatlan <manatlan@gmail.com> wrote:

note that someone has done a archlinux package of lxml (0.7) too (for my program, see : http://jbrout.python-hosting.com/wiki/Download ) it's here : http://aur.archlinux.org/packages.php?do_Details=1&ID=2149&O=0&L=0&C=0&K=jbrout&SB=n&SO=a&PP=25&do_MyPackages=0&do_Orphans=0 On 9/18/05, manatlan <manatlan@gmail.com> wrote:
participants (5)
-
Andrey Tatarinov
-
manatlan
-
Martijn Faassen
-
Olivier Grisel
-
Robert Kern