summaryrefslogtreecommitdiff
path: root/lib/mat.py
diff options
context:
space:
mode:
authorjvoisin2012-12-08 02:02:25 +0100
committerjvoisin2012-12-13 14:24:01 +0100
commitcbf8a2a65928694202e19b6bcf56ec84bcbf613c (patch)
treee106475b0d5c003505336b5ae6416e4508bb768b /lib/mat.py
parent67d5c1fa6b9ab6e1e7328ee57b15d8e46526d72a (diff)
Reorganize source tree and files installation location, cleanup setup.py (Closes: #689409)
Diffstat (limited to 'lib/mat.py')
-rw-r--r--lib/mat.py150
1 files changed, 0 insertions, 150 deletions
diff --git a/lib/mat.py b/lib/mat.py
deleted file mode 100644
index 6d9471c..0000000
--- a/lib/mat.py
+++ /dev/null
@@ -1,150 +0,0 @@
1#!/usr/bin/env python
2
3'''
4 Metadata anonymisation toolkit library
5'''
6
7import os
8import subprocess
9import logging
10import mimetypes
11import xml.sax
12
13import hachoir_core.cmd_line
14import hachoir_parser
15
16import strippers
17
18__version__ = '0.3.2'
19__author__ = 'jvoisin'
20
21#Silence
22LOGGING_LEVEL = logging.CRITICAL
23hachoir_core.config.quiet = True
24fname = ''
25
26#Verbose
27LOGGING_LEVEL = logging.DEBUG
28#hachoir_core.config.quiet = False
29#logname = 'report.log'
30
31logging.basicConfig(filename=fname, level=LOGGING_LEVEL)
32
33
34def get_sharedir(filename):
35 '''
36 An ugly hack to find various files
37 '''
38 if os.path.isfile(filename):
39 return filename
40 elif os.path.exists(os.path.join('/usr/local/share/mat/', filename)):
41 return os.path.join('/usr/local/share/mat/', filename)
42 elif os.path.exists(os.path.join('/usr/share/mat/', filename)):
43 return os.path.join('/usr/share/mat', filename)
44
45
46class XMLParser(xml.sax.handler.ContentHandler):
47 '''
48 Parse the supported format xml, and return a corresponding
49 list of dict
50 '''
51 def __init__(self):
52 self.dict = {}
53 self.list = []
54 self.content, self.key = '', ''
55 self.between = False
56
57 def startElement(self, name, attrs):
58 '''
59 Called when entering into xml balise
60 '''
61 self.between = True
62 self.key = name
63 self.content = ''
64
65 def endElement(self, name):
66 '''
67 Called when exiting a xml balise
68 '''
69 if name == 'format': # exiting a fileformat section
70 self.list.append(self.dict.copy())
71 self.dict.clear()
72 else:
73 content = self.content.replace('\s', ' ')
74 self.dict[self.key] = content
75 self.between = False
76
77 def characters(self, characters):
78 '''
79 Concatenate the content between opening and closing balises
80 '''
81 if self.between:
82 self.content += characters
83
84
85def secure_remove(filename):
86 '''
87 securely remove the file
88 '''
89 removed = False
90 try:
91 subprocess.call(['shred', '--remove', filename])
92 removed = True
93 except:
94 logging.error('Unable to securely remove %s' % filename)
95
96 if removed is False:
97 try:
98 os.remove(filename)
99 except:
100 logging.error('Unable to remove %s' % filename)
101
102
103def create_class_file(name, backup, add2archive):
104 '''
105 return a $FILETYPEStripper() class,
106 corresponding to the filetype of the given file
107 '''
108 if not os.path.isfile(name):
109 # check if the file exists
110 logging.error('%s is not a valid file' % name)
111 return None
112
113 if not os.access(name, os.R_OK):
114 #check read permissions
115 logging.error('%s is is not readable' % name)
116 return None
117
118 if not os.access(name, os.W_OK):
119 #check write permission
120 logging.error('%s is not writtable' % name)
121 return None
122
123 filename = ''
124 try:
125 filename = hachoir_core.cmd_line.unicodeFilename(name)
126 except TypeError: # get rid of "decoding Unicode is not supported"
127 filename = name
128
129 parser = hachoir_parser.createParser(filename)
130 if not parser:
131 logging.info('Unable to parse %s' % filename)
132 return None
133
134 mime = parser.mime_type
135
136 if mime == 'application/zip': # some formats are zipped stuff
137 mime = mimetypes.guess_type(name)[0]
138
139 if mime.startswith('application/vnd.oasis.opendocument'):
140 mime = 'application/opendocument' # opendocument fileformat
141 elif mime.startswith('application/vnd.openxmlformats-officedocument'):
142 mime = 'application/officeopenxml' # office openxml
143
144 try:
145 stripper_class = strippers.STRIPPERS[mime]
146 except KeyError:
147 logging.info('Don\'t have stripper for %s format' % mime)
148 return None
149
150 return stripper_class(filename, parser, mime, backup, add2archive)