IPsec : différenciation entre les IP publiques du tunnel et de la passerelle.
[progfou.git] / wcs / wcs-extract
CommitLineData
0f48356a
P
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
edf3eba0
P
3"""
4Outil d'export de données w.c.s.
0f48356a 5
edf3eba0
P
6Copyright : Agence universitaire de la Francophonie — www.auf.org
7Licence : GNU General Public Licence, version 2
8Auteur : Jean Christophe André
9Date de création : 15 octobre 2009
10
11Depends: wcs, python-simplejson, python-magic
12"""
0f48356a
P
13import os
14import os.path
a9de62a1
P
15import shutil
16import logging
0f48356a
P
17from time import gmtime, strftime
18import simplejson as json
edf3eba0
P
19import magic
20import mimetypes
0f48356a 21
0cd61ae9
P
22from wcs import publisher
23from wcs.formdef import FormDef
24from wcs.fields import TitleField, CommentField, TextField, \
ee4d6a09 25 StringField, ItemField, ItemsField, EmailField, \
0cd61ae9 26 DateField, FileField, BoolField
0f48356a 27
0f48356a 28
0cd61ae9
P
29def reduce_to_alnum(s, replacement_char='-'):
30 """réduction d'une chaîne de caractères à de l'alpha-numérique"""
31
0f48356a
P
32 avec_accent = u'çÇáàâÁÀÂéèêëÉÈÊËíìîïÍÌÎÏóòôöÓÒÔÖúùûüÚÙÛÜýỳyÿÝỲYŸ'
33 sans_accent = u'cCaaaAAAeeeeEEEEiiiiIIIIooooOOOOuuuuUUUUyyyyYYYY'
34 if type(s) is not unicode:
35 s = unicode(s, 'utf-8')
36 u = False
37 r = ''
38 for c in s:
39 index = avec_accent.find(c)
40 if index >= 0:
41 r += sans_accent[index]
42 elif ('a' <= c.lower() <= 'z') or ('0' <= c <= '9'):
43 r += c
44 elif len(r) > 0 and r[-1] != replacement_char:
45 r += replacement_char
46 else: # r == '' or r[-1] == replacement_char
47 pass
48 r = r.strip(replacement_char)
49 if not u:
50 r = r.encode('utf-8')
51 return r
52
0f48356a 53
0cd61ae9
P
54def extract_fields(formdef, output_directory):
55 """nommage des champs de façon unique"""
56 # TODO: devrait retourner un résultat, qui serait alors sauvé en dehors
57
36319452
P
58 # XXX: hack temporaire… :-/
59 global field_names
60
0cd61ae9
P
61 f = open(os.path.join(output_directory, 'field-names.txt'), 'w')
62
63 field_names = {}
64 field_names_duplicates = {}
0f48356a 65 for field in formdef.fields:
0f48356a
P
66 if isinstance(field, TitleField) or isinstance(field, CommentField):
67 continue
0cd61ae9
P
68 name = reduce_to_alnum(field.label,'_').lower()
69 if name in field_names.values(): # duplicat
70 field_names_duplicates[name] = field_names_duplicates.get(name, 1) + 1
71 name = '%s_%d' % (name, field_names_duplicates[name])
72 field_names.update({field.id: name})
73 print >>f, "%s:%s:%s" % (field.id, field_names[field.id], field.label)
74
0f48356a
P
75 f.close()
76
0cd61ae9 77 f = open(os.path.join(output_directory, 'field-names.json'), 'wb')
47d9d835 78 f.write(json.dumps(field_names, ensure_ascii=False))
0cd61ae9
P
79 f.close()
80
81
82def extract_data(formdef, output_directory):
83 """extraction des données du formulaire"""
84 # TODO: devrait retourner un résultat, qui serait alors sauvé en dehors
85
e80345a7
P
86 # XXX: hack temporaire… :-/
87 global pub
88
edf3eba0
P
89 # on charge la base des types MIME une fois pour toutes
90 #magicmime = magic.Magic(mime=True) => ce sera pour plus tard…
6244020f 91 magicmime = magic.open(magic.MAGIC_MIME)
dffcfa61 92 magicmime.load()
edf3eba0 93
2e14236b 94 liste_dossiers = []
0cd61ae9 95 for object in formdef.data_class().select():
9d424de0
P
96 if object.user is None:
97 logging.warning("Dossier '%s' sans utilisateur associé ?!?"\
98 " On ignore...", object.id)
99 continue
100
0cd61ae9
P
101 result = {
102 'num_dossier': object.id,
103 'wcs_status': object.status,
104 'wcs_workflow_status': object.get_workflow_status().name,
105 'wcs_user_email': object.user.email,
106 'wcs_user_display_name': object.user.display_name,
107 #'wcs_last_modified': strftime('%Y-%m-%d %H:%M:%S', gmtime(object.last_modified())),
e80345a7 108 'wcs_comments': [],
0cd61ae9 109 }
e80345a7
P
110
111 if object.evolution is not None:
112 for e in object.evolution:
113 if e.comment is not None:
114 who = pub.user_class.get(e.who).display_name
115 time = strftime('%Y-%m-%d %H:%M:%S', e.time)
116 comment = '%s -- %s %s' % (e.comment, who, time)
117 result['wcs_comments'].append(comment)
118
0cd61ae9
P
119 qfiles = { }
120 for field in formdef.fields:
121 field_id = str(field.id)
122 if not field_id in object.data:
123 continue
124 if isinstance(field, TitleField) or isinstance(field, CommentField):
125 continue
126 field_name = field_names[field_id]
127 data = object.data.get(field_id)
ee4d6a09
P
128 if data is None:
129 result[field_name] = None
130 continue
0cd61ae9
P
131 if isinstance(field, StringField) or isinstance(field, TextField) \
132 or isinstance(field, EmailField) or isinstance(field, ItemField):
133 result[field_name] = data
ee4d6a09
P
134 elif isinstance(field, ItemsField):
135 result[field_name] = data # liste => peux-être joindre sur ';'
0cd61ae9
P
136 elif isinstance(field, BoolField):
137 result[field_name] = (data == 'True')
138 elif isinstance(field, DateField):
b182779a
P
139 result[field_name] = '%04d-%02d-%02d' % (data.tm_year,
140 data.tm_mon, data.tm_mday)
791f9873 141 elif isinstance(field, FileField):
edf3eba0
P
142 if '.' in data.orig_filename:
143 extension = data.orig_filename.rpartition('.')[2].lower()
144 else: # il n'y a pas d'extension dans le nom de fichier
145 p = os.path.join(pub.app_dir, 'uploads', data.qfilename)
474e5135
P
146 try:
147 #m = magicmime.from_file(p) => ce sera pour plus tard…
148 m = magicmime.file(p).split()[0].strip(';')
149 extension = mimetypes.guess_extension(m)
150 except:
151 logging.warning("Type de fichier inconnu pour '%s'.", p)
152 extension = None
edf3eba0
P
153 if extension is not None:
154 extension = extension[1:]
155 else:
156 extension = 'unknown'
0cd61ae9
P
157 result[field_name] = "%s.%s" % (field_name, extension)
158 qfiles[field_name] = data.qfilename
159 else:
a9de62a1
P
160 logging.error("Type de champ inconnu '%s' pour '%s'.",
161 field.__class__.__name__, field.label)
0cd61ae9
P
162 raise RuntimeError
163
164 num_dossier = result['num_dossier']
165 nom = reduce_to_alnum(result['nom']).upper()
166 prenom = reduce_to_alnum(result['prenom']).upper()
167 adel = result['adresse_electronique'].replace('@','-').lower()
168
169 filename = "%04d-%s-%s-%s" % (num_dossier, nom, prenom, adel)
2e14236b 170 liste_dossiers.append(filename + '.json')
0cd61ae9 171
0cd61ae9
P
172 # copie des fichiers joints
173 for f in qfiles:
174 result[f] = filename + '_' + result[f]
175 src = os.path.join(pub.app_dir, 'uploads', qfiles[f])
a9de62a1 176 dst = os.path.join(output_directory, 'data', result[f])
0cd61ae9 177 if not os.path.exists(dst) or os.path.getmtime(src) > os.path.getmtime(dst):
a9de62a1 178 shutil.copy2(src, dst)
0cd61ae9
P
179 os.chmod(dst, 0644)
180
181 # génération du fichier JSON
a9de62a1
P
182 jsonname = os.path.join(output_directory, 'data', filename + '.json')
183 f = open(jsonname, 'wb')
47d9d835 184 f.write(json.dumps(result, ensure_ascii=False))
0cd61ae9
P
185 f.close()
186
a9de62a1
P
187 logging.info("Dossier '%s' : %s.",
188 filename, result['wcs_workflow_status'])
0cd61ae9 189
b35749bb 190 liste_dossiers.sort()
2e14236b 191 f = open(os.path.join(output_directory, 'liste-dossiers.json'), 'wb')
b35749bb 192 f.write(json.dumps(liste_dossiers, ensure_ascii=False))
2e14236b
P
193 f.close()
194
0cd61ae9
P
195
196if __name__ == '__main__':
197 import sys
198
199 if len(sys.argv) != 4:
200 print >>sys.stderr, "Usage : %s <dossier-destination> <site> <formulaire>" % sys.argv[0]
201 sys.exit(1)
202
203 VHOST = sys.argv[2]
204 FORM_NAME = sys.argv[3]
205 OUTPUT_DIRECTORY = os.path.join(sys.argv[1], VHOST, FORM_NAME)
206
207 os.umask(0022)
208 # création du dossier d'extraction, au besoin
9d424de0
P
209 if not os.path.isdir(os.path.join(OUTPUT_DIRECTORY, 'data')):
210 os.makedirs(os.path.join(OUTPUT_DIRECTORY, 'data'), 0755)
0cd61ae9 211
a9de62a1
P
212 logging.basicConfig(level=logging.DEBUG,
213 format='%(asctime)s %(levelname)s %(message)s',
214 filename=os.path.join(OUTPUT_DIRECTORY, 'last-run.log'),
215 filemode='w')
216
217 logging.info('Début.')
218
0cd61ae9
P
219 pub = publisher.WcsPublisher.create_publisher()
220 pub.app_dir = os.path.join(pub.app_dir, VHOST)
221
222 formdef = FormDef.get_by_urlname(FORM_NAME)
223
224 extract_fields(formdef, OUTPUT_DIRECTORY)
225
b182779a
P
226 try:
227 extract_data(formdef, OUTPUT_DIRECTORY)
228 except:
229 logging.exception("Interruption du traitement pour cause d'erreur !")
0cd61ae9 230
a9de62a1 231 logging.info('Fin.')
0f48356a 232