Version 0.2
authorEric Mc Sween <eric.mcsween@auf.org>
Tue, 28 May 2013 21:45:17 +0000 (17:45 -0400)
committerEric Mc Sween <eric.mcsween@auf.org>
Tue, 28 May 2013 21:45:17 +0000 (17:45 -0400)
.gitignore [new file with mode: 0644]
codaxmli/__init__.py [new file with mode: 0644]
codaxmli/java/router.jar [new file with mode: 0644]
codaxmli/java/xercesImpl.jar [new file with mode: 0644]
codaxmli/lxmlutils.py [new file with mode: 0644]
setup.py [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..2b71bcd
--- /dev/null
@@ -0,0 +1,3 @@
+*.pyc
+*.egg-info
+/dist
diff --git a/codaxmli/__init__.py b/codaxmli/__init__.py
new file mode 100644 (file)
index 0000000..3658ae7
--- /dev/null
@@ -0,0 +1,68 @@
+import os
+import subprocess
+from tempfile import mkstemp
+from lxml import etree
+
+from pkg_resources import resource_filename
+
+
+CLASSPATH = ':'.join([
+    resource_filename('codaxmli', 'java/router.jar'),
+    resource_filename('codaxmli', 'java/xercesImpl.jar'),
+])
+
+
+class Client(object):
+
+    def __init__(self, host, port, lsv_name, user, password, company,
+                 trace=False):
+        self.host = host
+        self.port = port
+        self.lsv_name = lsv_name
+        self.user = user
+        self.password = password
+        self.company = company
+        self.trace = trace
+
+    def send(self, request):
+        if not isinstance(request, basestring):
+            request = etree.tostring(request)
+        if self.trace:
+            print request
+        request_fd, request_path = mkstemp(prefix='xmli-')
+        os.write(request_fd, request)
+        os.close(request_fd)
+
+        response_fd, response_path = mkstemp(prefix='xmli-')
+        subprocess.check_call([
+            'java', '-classpath', CLASSPATH, 'com.coda.xml.router.Router',
+            '-nsv:' + self.host, '-port:' + str(self.port),
+            '-name:' + self.lsv_name, '-user:' + self.user,
+            '-password:' + self.password, '-company:' + self.company,
+            request_path
+        ], stdout=response_fd)
+        os.close(response_fd)
+
+        response = etree.parse(response_path)
+        os.remove(request_path)
+        os.remove(response_path)
+
+        errors = response.findall('//Reason')
+        if errors:
+            msgs = []
+            for error in errors:
+                lines = []
+                for text in error.findall('Text'):
+                    lines.append(text.text)
+                for path in error.findall('Path'):
+                    lines.append('Path: ' + path.text)
+                for hint in error.findall('Hint'):
+                    lines.append(hint.text)
+                msgs.append('\n'.join(lines))
+            raise XMLiError('\n\n'.join(msgs).encode('utf-8'))
+
+        return response
+
+
+class XMLiError(Exception):
+    pass
diff --git a/codaxmli/java/router.jar b/codaxmli/java/router.jar
new file mode 100644 (file)
index 0000000..3bcb441
Binary files /dev/null and b/codaxmli/java/router.jar differ
diff --git a/codaxmli/java/xercesImpl.jar b/codaxmli/java/xercesImpl.jar
new file mode 100644 (file)
index 0000000..14c3162
Binary files /dev/null and b/codaxmli/java/xercesImpl.jar differ
diff --git a/codaxmli/lxmlutils.py b/codaxmli/lxmlutils.py
new file mode 100644 (file)
index 0000000..c1d591f
--- /dev/null
@@ -0,0 +1,8 @@
+from lxml.etree import SubElement
+
+
+def get_or_create_child(elem, name):
+    child = elem.find(name)
+    if child is None:
+        child = SubElement(elem, name)
+    return child
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..662f434
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,25 @@
+# encoding: utf-8
+
+from setuptools import setup, find_packages
+
+name = 'coda-xmli'
+version = '0.2'
+
+setup(
+    name=name,
+    version=version,
+    description="Librairie d'interfaçage avec Coda XMLi",
+    long_description="",
+    classifiers=[],
+    keywords='',
+    author='Éric Mc Sween',
+    author_email='eric.mcsween@auf.org',
+    url='http://pypi.auf.org/%s' % name,
+    license='GPL',
+    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+    include_package_data=True,
+    zip_safe=False,
+    install_requires=[
+        'setuptools',
+    ],
+)