summaryrefslogtreecommitdiff
path: root/libmat/misc.py
diff options
context:
space:
mode:
authorjvoisin2014-06-08 13:39:18 +0200
committerjvoisin2014-06-08 13:39:18 +0200
commitaf36529554c39a2eefcc2c8723715e2d25b401b8 (patch)
treef54b964520bab44d1dfac725086211eaf22d3763 /libmat/misc.py
parentef5a32cfd3c0555ffe5ddf413eeaae61622ebb4b (diff)
Rename the MAT folder to libmat.
This commit fixes some issues for dump operating systems who doesn't handle capitalization.
Diffstat (limited to 'libmat/misc.py')
-rw-r--r--libmat/misc.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/libmat/misc.py b/libmat/misc.py
new file mode 100644
index 0000000..450f381
--- /dev/null
+++ b/libmat/misc.py
@@ -0,0 +1,76 @@
1''' Care about misc formats
2'''
3
4import parser
5
6from bencode import bencode
7
8
9class TorrentStripper(parser.GenericParser):
10 ''' Represent a torrent file with the help
11 of the bencode lib from Petru Paler
12 '''
13 def __init__(self, filename, parser, mime, backup, is_writable, **kwargs):
14 super(TorrentStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs)
15 self.fields = frozenset(['announce', 'info', 'name', 'path', 'piece length', 'pieces',
16 'length', 'files', 'announce-list', 'nodes', 'httpseeds', 'private', 'root hash'])
17
18 def __get_key_recursively(self, dictionary):
19 ''' Get recursively all keys from a dict and
20 its subdicts
21 '''
22 for i, j in list(dictionary.items()):
23 if isinstance(j, dict):
24 return set([i]).union(self.__get_key_recursively(j))
25 return set([i])
26
27 def is_clean(self):
28 ''' Check if the file is clean from harmful metadata
29 '''
30 with open(self.filename, 'r') as f:
31 decoded = bencode.bdecode(f.read())
32 return self.fields.issuperset(self.__get_key_recursively(decoded))
33
34 def __get_meta_recursively(self, dictionary):
35 ''' Get recursively all harmful metadata
36 '''
37 d = dict()
38 for i, j in list(dictionary.items()):
39 if i not in self.fields:
40 d[i] = j
41 elif isinstance(j, dict):
42 d = dict(d.items() + list(self.__get_meta_recursively(j).items()))
43 return d
44
45 def get_meta(self):
46 ''' Return a dict with all the meta of the file
47 '''
48 with open(self.filename, 'r') as f:
49 decoded = bencode.bdecode(f.read())
50 return self.__get_meta_recursively(decoded)
51
52 def __remove_all_recursively(self, dictionary):
53 ''' Remove recursively all compromizing fields
54 '''
55 d = dict()
56 for i, j in [i for i in list(dictionary.items()) if i in self.fields]:
57 if isinstance(j, dict):
58 d = dict(list(d.items()) + list(self.__get_meta_recursively(j).items()))
59 else:
60 d[i] = j
61 return d
62
63 def remove_all(self):
64 ''' Remove all comprimizing fields
65 '''
66 decoded = ''
67 with open(self.filename, 'r') as f:
68 decoded = bencode.bdecode(f.read())
69
70 cleaned = {i: j for i, j in list(decoded.items()) if i in self.fields}
71
72 with open(self.output, 'w') as f: # encode the decoded torrent
73 f.write(bencode.bencode(cleaned)) # and write it in self.output
74
75 self.do_backup()
76 return True