summaryrefslogtreecommitdiff
path: root/lib/misc.py
diff options
context:
space:
mode:
authorjvoisin2012-02-01 22:56:04 +0100
committerjvoisin2012-02-01 22:56:04 +0100
commit544fe9bf1782a027b3f31bf4c10a050d783e32ac (patch)
treea8dd60b9ae45efea4875fdb827070531f0199717 /lib/misc.py
parent9ea6dc6960cebfa70d18ba8ee49d775ea91c9b34 (diff)
Rename mat-cli to mat-gui
Diffstat (limited to 'lib/misc.py')
-rw-r--r--lib/misc.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/misc.py b/lib/misc.py
new file mode 100644
index 0000000..d084861
--- /dev/null
+++ b/lib/misc.py
@@ -0,0 +1,63 @@
1'''
2 Care about misc formats
3'''
4
5import parser
6
7from bencode import bencode
8
9
10class TorrentStripper(parser.GenericParser):
11 '''
12 Represent a torrent file with the help
13 of the bencode lib from Petru Paler
14 '''
15 def __init__(self, filename, parser, mime, backup, add2archive):
16 super(TorrentStripper, self).__init__(filename, parser, mime,
17 backup, add2archive)
18 self.fields = ['comment', 'creation date', 'created by']
19
20 def is_clean(self):
21 '''
22 Check if the file is clean from harmful metadatas
23 '''
24 with open(self.filename, 'r') as f:
25 decoded = bencode.bdecode(f.read())
26 for key in self.fields:
27 try:
28 if decoded[key] != '':
29 return False
30 except:
31 pass
32 return True
33
34 def get_meta(self):
35 '''
36 Return a dict with all the meta of the file
37 '''
38 metadata = {}
39 with open(self.filename, 'r') as f:
40 decoded = bencode.bdecode(f.read())
41 for key in self.fields:
42 try:
43 if decoded[key] != '':
44 metadata[key] = decoded[key]
45 except:
46 pass
47 return metadata
48
49 def remove_all(self):
50 '''
51 Remove all the files that are compromizing
52 '''
53 with open(self.filename, 'r') as f:
54 decoded = bencode.bdecode(f.read())
55 for key in self.fields:
56 try:
57 decoded[key] = ''
58 except:
59 pass
60 with open(self.output, 'w') as f: # encode the decoded torrent
61 f.write(bencode.bencode(decoded)) # and write it in self.output
62 self.do_backup()
63 return True