summaryrefslogtreecommitdiff
path: root/libmat/mat.py
diff options
context:
space:
mode:
authorjvoisin2015-11-03 14:53:04 +0100
committerjvoisin2015-11-03 14:53:04 +0100
commitc9465c4e92f2dd4cd29da26f0ed93c918b0498ba (patch)
tree7631475c79bff945f4aa48b30bb7b5b3c37ca4e8 /libmat/mat.py
parentbbdaaeaba42a4cf6ed61fed424e253aeb9b29a6a (diff)
SImplification and documentation of `create_class_file()`
Diffstat (limited to 'libmat/mat.py')
-rw-r--r--libmat/mat.py22
1 files changed, 9 insertions, 13 deletions
diff --git a/libmat/mat.py b/libmat/mat.py
index decbbed..df607a5 100644
--- a/libmat/mat.py
+++ b/libmat/mat.py
@@ -113,10 +113,10 @@ class XMLParser(xml.sax.handler.ContentHandler):
113 113
114 114
115def secure_remove(filename): 115def secure_remove(filename):
116 """ Securely remove the file 116 """ Securely remove $filename
117 :param str filename: File to be removed
117 """ 118 """
118 # I want the file removed, even if it's read-only 119 try: # I want the file removed, even if it's read-only
119 try:
120 os.chmod(filename, 220) 120 os.chmod(filename, 220)
121 except OSError: 121 except OSError:
122 logging.error('Unable to add write rights to %s' % filename) 122 logging.error('Unable to add write rights to %s' % filename)
@@ -145,17 +145,17 @@ def secure_remove(filename):
145def create_class_file(name, backup, **kwargs): 145def create_class_file(name, backup, **kwargs):
146 """ Return a $FILETYPEStripper() class, 146 """ Return a $FILETYPEStripper() class,
147 corresponding to the filetype of the given file 147 corresponding to the filetype of the given file
148
149 :param str name: name of the file to be parsed
150 :param bool backup: shell the file be backuped?
148 """ 151 """
149 if not os.path.isfile(name): # check if the file exists 152 if not os.path.isfile(name): # check if the file exists
150 logging.error('%s is not a valid file' % name) 153 logging.error('%s is not a valid file' % name)
151 return None 154 return None
152 155 elif not os.access(name, os.R_OK): # check read permissions
153 if not os.access(name, os.R_OK): # check read permissions
154 logging.error('%s is is not readable' % name) 156 logging.error('%s is is not readable' % name)
155 return None 157 return None
156 158 elif not os.path.getsize(name): # check if the file is not empty (hachoir crash on empty files)
157 if not os.path.getsize(name):
158 # check if the file is not empty (hachoir crash on empty files)
159 logging.error('%s is empty' % name) 159 logging.error('%s is empty' % name)
160 return None 160 return None
161 161
@@ -168,15 +168,11 @@ def create_class_file(name, backup, **kwargs):
168 if not parser: 168 if not parser:
169 logging.info('Unable to parse %s with hachoir' % filename) 169 logging.info('Unable to parse %s with hachoir' % filename)
170 170
171 mime = mimetypes.guess_type(filename)[0] 171 mime = mimetypes.guess_type(name)[0]
172 if not mime: 172 if not mime:
173 logging.info('Unable to find mimetype of %s' % filename) 173 logging.info('Unable to find mimetype of %s' % filename)
174 return None 174 return None
175 175
176 if mime == 'application/zip': # some formats are zipped stuff
177 if mimetypes.guess_type(name)[0]:
178 mime = mimetypes.guess_type(name)[0]
179
180 if mime.startswith('application/vnd.oasis.opendocument'): 176 if mime.startswith('application/vnd.oasis.opendocument'):
181 mime = 'application/opendocument' # opendocument fileformat 177 mime = 'application/opendocument' # opendocument fileformat
182 elif mime.startswith('application/vnd.openxmlformats-officedocument'): 178 elif mime.startswith('application/vnd.openxmlformats-officedocument'):