File: parser.py

package info (click to toggle)
mat 0.6.1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,888 kB
  • ctags: 263
  • sloc: python: 1,629; makefile: 7; sh: 6
file content (66 lines) | stat: -rw-r--r-- 1,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
""" Parent class of all parser
"""

import os
import shutil
import tempfile


import mat

NOMETA = frozenset((
    '.bmp',   # "raw" image
    '.rdf',   # text
    '.txt',   # plain text
    '.xml',   # formated text (XML)
    '.rels',  # openXML formated text
))

FIELD = object()


class GenericParser(object):
    """ Parent class of all parsers
    """
    def __init__(self, filename, mime, backup, is_writable, **kwargs):
        self.filename = ''
        self.mime = mime
        self.backup = backup
        self.is_writable = is_writable
        self.filename = filename
        self.basename = os.path.basename(filename)
        self.output = tempfile.mkstemp()[1]

    def __del__(self):
        """ Remove tempfile if it was not used
        """
        if os.path.exists(self.output):
            mat.secure_remove(self.output)

    def is_clean(self):
        """
            Check if the file is clean from harmful metadatas
        """
        raise NotImplementedError

    def remove_all(self):
        """ Remove all compromising fields
        """
        raise NotImplementedError

    def create_backup_copy(self):
        """ Create a backup copy
        """
        shutil.copy2(self.filename, self.filename + '.bak')

    def do_backup(self):
        """ Keep a backup of the file if asked.

            The process of double-renaming is not very elegant,
            but it greatly simplify new strippers implementation.
        """
        if self.backup:
            shutil.move(self.filename, self.filename + '.bak')
        else:
            mat.secure_remove(self.filename)
        shutil.move(self.output, self.filename)