source: dcds_util.py @ 0:2f43cb89e87c

Revision 0:2f43cb89e87c, 1.3 KB checked in by andre.hagenbruch@rub.de, 8 years ago (diff)

Initial commit for version 0.2

Line 
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4dcds_util.py
5
6Created by Andre Hagenbruch on 2009-03-20.
7Copyright (c) 2009 University Library Bochum. All rights reserved.
8"""
9
10from lxml import etree
11#from elm_builder import elm
12
13DCTERMS_NAMESPACE = 'http://purl.org/dc/terms/'
14DCDS_NAMESPACE = 'http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml'
15DCDS = '{%s}' % DCDS_NAMESPACE
16
17NSMAP = {'dcds': DCDS_NAMESPACE}
18
19def elm(ename, val, **attr):
20        e = etree.Element(ename, nsmap = NSMAP)
21        try:
22                if attr is not None:
23                        for att in attr:
24                                e.set(att, attr[att])
25        except IndexError:
26                pass
27        if val is not None:
28                try:
29                        e.text = val.decode('utf-8')
30                except UnicodeDecodeError:
31                        e.text = val.decode('iso-8859-1')
32                except UnicodeEncodeError :
33                        e.text = val
34        return e
35
36def desc(resource):
37        description = elm(DCDS + 'description', None, **{'resourceURI': resource})
38        return description
39       
40def stmt(prop, value):
41        statement = elm('statement', None, **{'propertyURI': DCTERMS_NAMESPACE + prop})
42        statement.append(elm('literalValueString', value))
43        return statement
44
45def main():
46        my_desc = desc('tag:bibliolabs.ub.rub.de,2009:dcds:test:1')
47        my_desc.append(stmt('title', 'Die unendlichen Wuppungen des Seins'))
48        my_desc.append(stmt('creator', 'Test, Theo'))
49        my_desc.append(stmt('issued', '2009'))
50        print etree.tostring(my_desc, pretty_print = True)
51
52
53if __name__ == '__main__':
54        main()
55
Note: See TracBrowser for help on using the repository browser.