diff options
| author | jvoisin | 2013-10-27 23:01:20 +0000 |
|---|---|---|
| committer | jvoisin | 2013-10-27 23:01:20 +0000 |
| commit | 4c81e731a485d3ea84049ef6d568153c8b10e90b (patch) | |
| tree | 86ad43d7df67ed8d27cfbe7ff60dda1545784845 /MAT/mat.py | |
| parent | 6f21743fdae533d7a94f64fb03d706fb342aff01 (diff) | |
Improves documentation
Diffstat (limited to 'MAT/mat.py')
| -rw-r--r-- | MAT/mat.py | 50 |
1 files changed, 23 insertions, 27 deletions
| @@ -1,13 +1,12 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | 2 | ||
| 3 | ''' | 3 | ''' Metadata anonymisation toolkit library |
| 4 | Metadata anonymisation toolkit library | ||
| 5 | ''' | 4 | ''' |
| 6 | 5 | ||
| 7 | import os | ||
| 8 | import subprocess | ||
| 9 | import logging | 6 | import logging |
| 10 | import mimetypes | 7 | import mimetypes |
| 8 | import os | ||
| 9 | import subprocess | ||
| 11 | import xml.sax | 10 | import xml.sax |
| 12 | 11 | ||
| 13 | import hachoir_core.cmd_line | 12 | import hachoir_core.cmd_line |
| @@ -33,6 +32,8 @@ logging.basicConfig(filename=fname, level=LOGGING_LEVEL) | |||
| 33 | import strippers # this is loaded here because we need LOGGING_LEVEL | 32 | import strippers # this is loaded here because we need LOGGING_LEVEL |
| 34 | 33 | ||
| 35 | def get_logo(): | 34 | def get_logo(): |
| 35 | ''' Return the path to the logo | ||
| 36 | ''' | ||
| 36 | if os.path.isfile('./data/mat.png'): | 37 | if os.path.isfile('./data/mat.png'): |
| 37 | return './data/mat.png' | 38 | return './data/mat.png' |
| 38 | elif os.path.isfile('/usr/share/pixmaps/mat.png'): | 39 | elif os.path.isfile('/usr/share/pixmaps/mat.png'): |
| @@ -41,6 +42,8 @@ def get_logo(): | |||
| 41 | return '/usr/local/share/pixmaps/mat.png' | 42 | return '/usr/local/share/pixmaps/mat.png' |
| 42 | 43 | ||
| 43 | def get_datadir(): | 44 | def get_datadir(): |
| 45 | ''' Return the path to the data directory | ||
| 46 | ''' | ||
| 44 | if os.path.isdir('./data/'): | 47 | if os.path.isdir('./data/'): |
| 45 | return './data/' | 48 | return './data/' |
| 46 | elif os.path.isdir('/usr/local/share/mat/'): | 49 | elif os.path.isdir('/usr/local/share/mat/'): |
| @@ -49,8 +52,9 @@ def get_datadir(): | |||
| 49 | return '/usr/share/mat/' | 52 | return '/usr/share/mat/' |
| 50 | 53 | ||
| 51 | def list_supported_formats(): | 54 | def list_supported_formats(): |
| 52 | ''' | 55 | ''' Return a list of all locally supported fileformat. |
| 53 | Return a list of all locally supported fileformat | 56 | It parses that FORMATS file, and removes locally |
| 57 | non-supported formats. | ||
| 54 | ''' | 58 | ''' |
| 55 | handler = XMLParser() | 59 | handler = XMLParser() |
| 56 | parser = xml.sax.make_parser() | 60 | parser = xml.sax.make_parser() |
| @@ -67,8 +71,7 @@ def list_supported_formats(): | |||
| 67 | return localy_supported | 71 | return localy_supported |
| 68 | 72 | ||
| 69 | class XMLParser(xml.sax.handler.ContentHandler): | 73 | class XMLParser(xml.sax.handler.ContentHandler): |
| 70 | ''' | 74 | ''' Parse the supported format xml, and return a corresponding |
| 71 | Parse the supported format xml, and return a corresponding | ||
| 72 | list of dict | 75 | list of dict |
| 73 | ''' | 76 | ''' |
| 74 | def __init__(self): | 77 | def __init__(self): |
| @@ -78,18 +81,16 @@ class XMLParser(xml.sax.handler.ContentHandler): | |||
| 78 | self.between = False | 81 | self.between = False |
| 79 | 82 | ||
| 80 | def startElement(self, name, attrs): | 83 | def startElement(self, name, attrs): |
| 81 | ''' | 84 | ''' Called when entering into xml tag |
| 82 | Called when entering into xml tag | ||
| 83 | ''' | 85 | ''' |
| 84 | self.between = True | 86 | self.between = True |
| 85 | self.key = name | 87 | self.key = name |
| 86 | self.content = '' | 88 | self.content = '' |
| 87 | 89 | ||
| 88 | def endElement(self, name): | 90 | def endElement(self, name): |
| 91 | ''' Called when exiting a xml tag | ||
| 89 | ''' | 92 | ''' |
| 90 | Called when exiting a xml tag | 93 | if name == 'format': # leaving a fileformat section |
| 91 | ''' | ||
| 92 | if name == 'format': # exiting a fileformat section | ||
| 93 | self.list.append(self.dict.copy()) | 94 | self.list.append(self.dict.copy()) |
| 94 | self.dict.clear() | 95 | self.dict.clear() |
| 95 | else: | 96 | else: |
| @@ -98,19 +99,17 @@ class XMLParser(xml.sax.handler.ContentHandler): | |||
| 98 | self.between = False | 99 | self.between = False |
| 99 | 100 | ||
| 100 | def characters(self, characters): | 101 | def characters(self, characters): |
| 101 | ''' | 102 | ''' Concatenate the content between opening and closing tags |
| 102 | Concatenate the content between opening and closing tags | ||
| 103 | ''' | 103 | ''' |
| 104 | if self.between: | 104 | if self.between: |
| 105 | self.content += characters | 105 | self.content += characters |
| 106 | 106 | ||
| 107 | 107 | ||
| 108 | def secure_remove(filename): | 108 | def secure_remove(filename): |
| 109 | ''' | 109 | ''' Securely remove the file |
| 110 | securely remove the file | ||
| 111 | ''' | 110 | ''' |
| 112 | try: | 111 | try: |
| 113 | if subprocess.call(['shred', '--remove', filename]) == 0: | 112 | if not subprocess.call(['shred', '--remove', filename]): |
| 114 | return True | 113 | return True |
| 115 | else: | 114 | else: |
| 116 | raise OSError | 115 | raise OSError |
| @@ -126,22 +125,17 @@ def secure_remove(filename): | |||
| 126 | 125 | ||
| 127 | 126 | ||
| 128 | def create_class_file(name, backup, **kwargs): | 127 | def create_class_file(name, backup, **kwargs): |
| 129 | ''' | 128 | ''' Return a $FILETYPEStripper() class, |
| 130 | return a $FILETYPEStripper() class, | ||
| 131 | corresponding to the filetype of the given file | 129 | corresponding to the filetype of the given file |
| 132 | ''' | 130 | ''' |
| 133 | if not os.path.isfile(name): | 131 | if not os.path.isfile(name): # check if the file exists |
| 134 | # check if the file exists | ||
| 135 | logging.error('%s is not a valid file' % name) | 132 | logging.error('%s is not a valid file' % name) |
| 136 | return None | 133 | return None |
| 137 | 134 | ||
| 138 | if not os.access(name, os.R_OK): | 135 | if not os.access(name, os.R_OK): #check read permissions |
| 139 | #check read permissions | ||
| 140 | logging.error('%s is is not readable' % name) | 136 | logging.error('%s is is not readable' % name) |
| 141 | return None | 137 | return None |
| 142 | 138 | ||
| 143 | is_writable = os.access(name, os.W_OK) | ||
| 144 | |||
| 145 | if not os.path.getsize(name): | 139 | if not os.path.getsize(name): |
| 146 | #check if the file is not empty (hachoir crash on empty files) | 140 | #check if the file is not empty (hachoir crash on empty files) |
| 147 | logging.error('%s is empty' % name) | 141 | logging.error('%s is empty' % name) |
| @@ -161,7 +155,7 @@ def create_class_file(name, backup, **kwargs): | |||
| 161 | mime = parser.mime_type | 155 | mime = parser.mime_type |
| 162 | 156 | ||
| 163 | if mime == 'application/zip': # some formats are zipped stuff | 157 | if mime == 'application/zip': # some formats are zipped stuff |
| 164 | if mimetypes.guess_type(name)[0] is not None: | 158 | if mimetypes.guess_type(name)[0]: |
| 165 | mime = mimetypes.guess_type(name)[0] | 159 | mime = mimetypes.guess_type(name)[0] |
| 166 | 160 | ||
| 167 | if mime.startswith('application/vnd.oasis.opendocument'): | 161 | if mime.startswith('application/vnd.oasis.opendocument'): |
| @@ -169,6 +163,8 @@ def create_class_file(name, backup, **kwargs): | |||
| 169 | elif mime.startswith('application/vnd.openxmlformats-officedocument'): | 163 | elif mime.startswith('application/vnd.openxmlformats-officedocument'): |
| 170 | mime = 'application/officeopenxml' # office openxml | 164 | mime = 'application/officeopenxml' # office openxml |
| 171 | 165 | ||
| 166 | is_writable = os.access(name, os.W_OK) | ||
| 167 | |||
| 172 | try: | 168 | try: |
| 173 | stripper_class = strippers.STRIPPERS[mime] | 169 | stripper_class = strippers.STRIPPERS[mime] |
| 174 | except KeyError: | 170 | except KeyError: |
