/[formed]/trunk/tools/anonym/xmlexport.py
ViewVC logotype

Annotation of /trunk/tools/anonym/xmlexport.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 242 - (hide annotations)
Wed Feb 27 07:13:31 2008 UTC (17 years ago) by teichmann
File MIME type: text/x-python
File size: 4574 byte(s)
Various bug fixes for XML import of anonymous cases.

1 teichmann 242 #!/usr/bin/python
2 teichmann 231 #
3     # (c) 2008 by Intevation GmbH
4     # author: Sascha L. Teichmann ([email protected])
5     #
6     import sys
7     #import os
8     import codecs
9     #import getopt
10     import psycopg2 as dbapi
11     import psycopg2.extras as extras
12     from types import StringTypes
13    
14     dbapi.extensions.register_type(dbapi.extensions.UNICODE)
15    
16     DBNAME = "ka_inte00002_db"
17     HOST = "laodameia"
18     PORT = 5434
19     USER = "postgres"
20     PASSWORD = ""
21    
22 torsten 235 ANON_ON = \
23     """SELECT wert FROM ka_konfiguration_tbl
24     WHERE bez = 'anon_transfer'"""
25    
26 teichmann 231 SELECT_MASTER_IDS = \
27 torsten 235 """SELECT id from master_tbl_anonym_view"""
28 teichmann 231
29     SELECT_MASTER_TBL = \
30 torsten 235 """SELECT * FROM master_tbl_anonym_view WHERE id = %(id)s"""
31 teichmann 231
32     RG_VIEWS = (
33     "kompetenzfestellung",
34     "angebote_berufliche_qualifizierung",
35     "angebote_berufsvorbereitung",
36     "angebote_bildenden_bereich",
37     "angebote_lebensbewaeltigung")
38    
39     SELECT_RG = \
40     """SELECT * FROM %s WHERE master_id = %d"""
41    
42     BLACK_LIST = set(['id', 'master_id', 'bearbeiter_id'])
43    
44     def escape(value, tagname):
45     if tagname == 'uuid_id':
46     return "%s" % value
47     if type(value) in StringTypes:
48     value = value.replace(']]>', '')
49     return "<![CDATA[%s]]>" % value
50     if value is None:
51     return ""
52     return "%s" % value
53    
54     def dumpColums(columns):
55     out = []
56     for tagname, value in columns:
57     if tagname not in BLACK_LIST and not value is None:
58     out.append("<%s>%s</%s>" % (tagname, escape(value, tagname), tagname))
59     return out
60    
61     class Case:
62    
63     def __init__(self, master_id):
64     self.master_id = int(master_id)
65     self.columns = None
66     self.rgs = []
67    
68     def fetchData(self, cur):
69     self.fetchMasterTable(cur)
70     self.fetchRepeatGroups(cur)
71    
72     def fetchMasterTable(self, cur):
73     cur.execute(SELECT_MASTER_TBL, { 'id': self.master_id })
74     row = cur.fetchone()
75     if not row: raise Exception("No dataset for id %d" % self.master_id)
76     columns = row._index.items() # XXX: hackish!
77     columns.sort(lambda a, b: cmp(a[1], b[1]))
78     columns = [(a[0], row[a[1]]) for a in columns]
79     self.columns = columns
80    
81     def fetchRepeatGroups(self, cur):
82     for view in RG_VIEWS:
83 torsten 235 viewname = "rg_%s_tbl_anonym_view" % view
84 teichmann 231 cur.execute(SELECT_RG % (viewname, self.master_id))
85     rs = []
86     while True:
87     row = cur.fetchone()
88     if not row: break
89     cs = row._index.items() # XXX: hackish!
90     cs.sort(lambda a, b: cmp(a[1], b[1]))
91     cs = [(a[0], row[a[1]]) for a in cs]
92     rs.append(cs)
93     if rs:
94     self.rgs.append((view, rs))
95    
96    
97     def dumpMasterTable(self, out):
98     fields = dumpColums(self.columns)
99     if fields:
100     out.write("<master>%s</master>" % "".join(fields))
101    
102     def dumpRepeatGroups(self, out):
103     for view, rgs in self.rgs:
104     for r in rgs:
105     fields = dumpColums(r)
106     if fields:
107     out.write("<%s>%s</%s>" % (view, "".join(fields), view))
108    
109     def dumpData(self, out):
110     out.write("<case>")
111     self.dumpMasterTable(out)
112     self.dumpRepeatGroups(out)
113     out.write("</case>")
114    
115     def main():
116 torsten 235 DBNAME = sys.argv[1]
117     HOST = sys.argv[2]
118     PORT = sys.argv[3]
119 teichmann 231 Writer = codecs.getwriter("utf-8")
120     out = Writer(sys.stdout)
121     con, cur = None, None
122     try:
123 torsten 235 #PASSWORD = raw_input()
124 teichmann 231 con = dbapi.connect(
125     database = DBNAME,
126     port = PORT,
127 torsten 235 user = USER)
128    
129 teichmann 231
130     cur = con.cursor(cursor_factory=extras.DictCursor)
131 torsten 235 cur.execute(ANON_ON)
132     row = cur.next()
133 teichmann 231
134 torsten 235 if row:
135     if row[0] != 'on':
136     print >> sys.stderr, "Anonymer Transfer nicht gestattet"
137     sys.exit(1)
138     else:
139     print >> sys.stderr, "Anonymer Transfer nicht gestattet"
140     sys.exit(1)
141    
142    
143 teichmann 231 cases = []
144    
145     cur.execute(SELECT_MASTER_IDS)
146    
147     while True:
148     row = cur.fetchone()
149     if not row: break
150     cases.append(Case(row[0]))
151    
152     out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
153     out.write("<cases>")
154     for case in cases:
155     case.fetchData(cur)
156     case.dumpData(out)
157     out.write("</cases>")
158    
159     finally:
160     if cur:
161     try: cur.close()
162     except: pass
163     if con:
164     try: con.close()
165     except: pass
166    
167    
168     if __name__ == "__main__":
169     main()
170    
171     # vim:set ts=4 sw=4 si et sta sts=4:

Properties

Name Value
svn:executable *

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26