diff options
| author | jvoisin | 2012-02-01 22:56:04 +0100 |
|---|---|---|
| committer | jvoisin | 2012-02-01 22:56:04 +0100 |
| commit | 544fe9bf1782a027b3f31bf4c10a050d783e32ac (patch) | |
| tree | a8dd60b9ae45efea4875fdb827070531f0199717 /lib/mat.py | |
| parent | 9ea6dc6960cebfa70d18ba8ee49d775ea91c9b34 (diff) | |
Rename mat-cli to mat-gui
Diffstat (limited to 'lib/mat.py')
| -rw-r--r-- | lib/mat.py | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/lib/mat.py b/lib/mat.py new file mode 100644 index 0000000..53d02d8 --- /dev/null +++ b/lib/mat.py | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | |||
| 3 | ''' | ||
| 4 | Metadata anonymisation toolkit library | ||
| 5 | ''' | ||
| 6 | |||
| 7 | import os | ||
| 8 | import subprocess | ||
| 9 | import logging | ||
| 10 | import mimetypes | ||
| 11 | import xml.sax | ||
| 12 | |||
| 13 | import hachoir_core.cmd_line | ||
| 14 | import hachoir_parser | ||
| 15 | |||
| 16 | import strippers | ||
| 17 | |||
| 18 | __version__ = '0.2.2' | ||
| 19 | __author__ = 'jvoisin' | ||
| 20 | |||
| 21 | #Silence | ||
| 22 | LOGGING_LEVEL = logging.CRITICAL | ||
| 23 | hachoir_core.config.quiet = True | ||
| 24 | fname = '' | ||
| 25 | |||
| 26 | #Verbose | ||
| 27 | #LOGGING_LEVEL = logging.DEBUG | ||
| 28 | #hachoir_core.config.quiet = False | ||
| 29 | #logname = 'report.log' | ||
| 30 | |||
| 31 | logging.basicConfig(filename=fname, level=LOGGING_LEVEL) | ||
| 32 | |||
| 33 | |||
| 34 | def get_sharedir(): | ||
| 35 | ''' | ||
| 36 | An ugly hack to find where is the "FORMATS" file. | ||
| 37 | ''' | ||
| 38 | if os.path.isfile('FORMATS'): | ||
| 39 | return '' | ||
| 40 | elif os.path.exists('/usr/local/share/mat/'): | ||
| 41 | return '/usr/local/share/mat/' | ||
| 42 | elif os.path.exists('/usr/share/mat/'): | ||
| 43 | return '/usr/share/mat' | ||
| 44 | |||
| 45 | |||
| 46 | class 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 | |||
| 85 | def 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 | |||
| 103 | def 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) | ||
