list2form : gestion des erreurs et des petits fichiers (merci cgi.py…)
[progfou.git] / nautilus-scripts / Déverrouillage de formulaire
... / ...
CommitLineData
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright © 2014 AUF
4# Licence: GPL-2
5# Author: Progfou <jean-christophe.andre@auf.org>
6# Created: 2014-03-11
7# Debian-Depends: python (>= 2.6)
8import sys
9import os.path
10import zipfile
11from xml.dom import minidom
12
13if len(sys.argv) < 2:
14 sys.exit(0)
15
16for filename in sys.argv[1:]:
17 if not zipfile.is_zipfile(filename):
18 continue
19 # sanity checks
20 odf = zipfile.ZipFile(filename, 'r')
21 if not odf:
22 continue # u"WARNING: skipping '%s': not a ZIP file" % filename
23 if not odf.namelist():
24 odf.close()
25 continue # u"WARNING: skipping '%s': not a ZIP file" % filename
26 if 'content.xml' not in odf.namelist() or \
27 'styles.xml' not in odf.namelist() or \
28 'settings.xml' not in odf.namelist():
29 odf.close()
30 continue # u"WARNING: skipping '%s': not an ODF file" % filename
31
32 # copy creation
33 filename_base, filename_ext = os.path.splitext(filename)
34 copyname = filename_base + '-déverrouillé' + filename_ext
35 new_odf = zipfile.ZipFile(copyname, 'w', zipfile.ZIP_DEFLATED)
36
37 for info in odf.infolist():
38 data = odf.read(info.filename)
39 if info.filename == 'content.xml':
40 modified = False
41 # content parsing
42 content = minidom.parseString(data)
43 # unlock section protection
44 #for node in content.getElementsByTagName("text:section"):
45 # if node.getAttribute("text:protected") == 'true':
46 # node.setAttribute("text:protected", "false")
47 # modified = True
48 # save content changes, only if necessary
49 if modified:
50 data = content.toxml().encode('utf-8')
51 elif info.filename == 'settings.xml':
52 modified = False
53 # settings parsing
54 settings = minidom.parseString(data)
55 for node in settings.getElementsByTagName("config:config-item"):
56 name = node.getAttribute("config:name")
57 if not node.firstChild:
58 continue
59 value = node.firstChild.wholeText
60 # unlock form protection
61 if name == 'ProtectForm' and value == 'true':
62 node.firstChild.replaceWholeText('false')
63 modified = True
64 # unlock document editing
65 elif name == 'LoadReadonly' and value == 'true':
66 node.firstChild.replaceWholeText('false')
67 modified = True
68 # save settings changes, only if necessary
69 if modified:
70 data = settings.toxml().encode('utf-8')
71 new_odf.writestr(info, data)
72
73 new_odf.close()
74 odf.close()
75
76sys.exit(0)