summaryrefslogtreecommitdiff
path: root/lib/parser.py
blob: 2b7b52e338d30250a9a9d6455779792161ea4026 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''
    Parent class of all parser
'''

import hachoir_core.error
import hachoir_parser
import hachoir_editor
import sys
import os
import subprocess
import mimetypes
import mat

POSTFIX = ".cleaned"

class Generic_parser(object):
    def __init__(self, realname, filename, parser, editor, backup):
        self.filename = filename
        self.realname = realname
        self.shortname = os.path.basename(filename)
        self.mime = mimetypes.guess_type(filename)[0]
        self.parser = parser
        self.editor = editor
        self.backup = backup

    def is_clean(self):
        '''
            Check if the file is clean from harmful metadatas
        '''
        for field in self.editor:
            if self._should_remove(field):
                return False
        return True

    def remove_all(self):
        '''
            Remove all the files that are compromizing
        '''
        for field in self.editor:
            if self._should_remove(field):
                self._remove(field)
        hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX)
        if self.backup is False:
            mat.secure_remove(self.filename) #remove the old file
            os.rename(self.filename+ POSTFIX, self.filename)#rename the new

    def remove_all_ugly(self):
        '''
            If the remove_all() is not efficient enough,
            this method is implemented :
            It is efficient, but destructive.
            In a perfect world, with nice fileformat,
            this method does not exist.
        '''
        self.remove_all()


    def _remove(self, field):
        '''
            Remove the given field
        '''
        del self.editor[field.name]

    def search(self, value):
        return self.__search(value, self.editor)

    def __search(self, value, graph):
        '''
            Search a given file
        '''
        for node in graph:
            try:
                iter(node)
                return node.value + self.__search(value, node)
            except:
                if node.name == value:
                    return value
        return False


    def get_meta(self):
        '''
            return a dict with all the meta of the file
        '''
        metadata = {}
        for field in self.editor:
            if self._should_remove(field):
                try:
                    metadata[field.name] = field.value
                except:
                    metadata[field.name] = "harmful content"
        return metadata

    def _should_remove(self, key):
        '''
            return True if the field is compromizing
            abstract method
        '''
        raise NotImplementedError()