diff options
| author | jvoisin | 2015-07-25 17:14:23 +0200 |
|---|---|---|
| committer | jvoisin | 2015-07-25 17:14:23 +0200 |
| commit | 6ba3e3f20d7d52895bc44f9fc35b068cfce47133 (patch) | |
| tree | 15df2aca17d56d941c6376ef729e0c1fea4c396f /libmat/misc.py | |
| parent | 85e6279d16af063e5150c7cf4bd491185b8ae788 (diff) | |
_MASSIVE_ pep8 revamp
Thank you so much PyCharm
Diffstat (limited to 'libmat/misc.py')
| -rw-r--r-- | libmat/misc.py | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/libmat/misc.py b/libmat/misc.py index 450f381..b1a551c 100644 --- a/libmat/misc.py +++ b/libmat/misc.py | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ''' Care about misc formats | 1 | """ Care about misc formats |
| 2 | ''' | 2 | """ |
| 3 | 3 | ||
| 4 | import parser | 4 | import parser |
| 5 | 5 | ||
| @@ -7,33 +7,34 @@ from bencode import bencode | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | class TorrentStripper(parser.GenericParser): | 9 | class TorrentStripper(parser.GenericParser): |
| 10 | ''' Represent a torrent file with the help | 10 | """ Represent a torrent file with the help |
| 11 | of the bencode lib from Petru Paler | 11 | of the bencode lib from Petru Paler |
| 12 | ''' | 12 | """ |
| 13 | |||
| 13 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | 14 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): |
| 14 | super(TorrentStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) | 15 | super(TorrentStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) |
| 15 | self.fields = frozenset(['announce', 'info', 'name', 'path', 'piece length', 'pieces', | 16 | self.fields = frozenset(['announce', 'info', 'name', 'path', 'piece length', 'pieces', |
| 16 | 'length', 'files', 'announce-list', 'nodes', 'httpseeds', 'private', 'root hash']) | 17 | 'length', 'files', 'announce-list', 'nodes', 'httpseeds', 'private', 'root hash']) |
| 17 | 18 | ||
| 18 | def __get_key_recursively(self, dictionary): | 19 | def __get_key_recursively(self, dictionary): |
| 19 | ''' Get recursively all keys from a dict and | 20 | """ Get recursively all keys from a dict and |
| 20 | its subdicts | 21 | its subdicts |
| 21 | ''' | 22 | """ |
| 22 | for i, j in list(dictionary.items()): | 23 | for i, j in list(dictionary.items()): |
| 23 | if isinstance(j, dict): | 24 | if isinstance(j, dict): |
| 24 | return set([i]).union(self.__get_key_recursively(j)) | 25 | return {i}.union(self.__get_key_recursively(j)) |
| 25 | return set([i]) | 26 | return {i} |
| 26 | 27 | ||
| 27 | def is_clean(self): | 28 | def is_clean(self): |
| 28 | ''' Check if the file is clean from harmful metadata | 29 | """ Check if the file is clean from harmful metadata |
| 29 | ''' | 30 | """ |
| 30 | with open(self.filename, 'r') as f: | 31 | with open(self.filename, 'r') as f: |
| 31 | decoded = bencode.bdecode(f.read()) | 32 | decoded = bencode.bdecode(f.read()) |
| 32 | return self.fields.issuperset(self.__get_key_recursively(decoded)) | 33 | return self.fields.issuperset(self.__get_key_recursively(decoded)) |
| 33 | 34 | ||
| 34 | def __get_meta_recursively(self, dictionary): | 35 | def __get_meta_recursively(self, dictionary): |
| 35 | ''' Get recursively all harmful metadata | 36 | """ Get recursively all harmful metadata |
| 36 | ''' | 37 | """ |
| 37 | d = dict() | 38 | d = dict() |
| 38 | for i, j in list(dictionary.items()): | 39 | for i, j in list(dictionary.items()): |
| 39 | if i not in self.fields: | 40 | if i not in self.fields: |
| @@ -43,15 +44,15 @@ class TorrentStripper(parser.GenericParser): | |||
| 43 | return d | 44 | return d |
| 44 | 45 | ||
| 45 | def get_meta(self): | 46 | def get_meta(self): |
| 46 | ''' Return a dict with all the meta of the file | 47 | """ Return a dict with all the meta of the file |
| 47 | ''' | 48 | """ |
| 48 | with open(self.filename, 'r') as f: | 49 | with open(self.filename, 'r') as f: |
| 49 | decoded = bencode.bdecode(f.read()) | 50 | decoded = bencode.bdecode(f.read()) |
| 50 | return self.__get_meta_recursively(decoded) | 51 | return self.__get_meta_recursively(decoded) |
| 51 | 52 | ||
| 52 | def __remove_all_recursively(self, dictionary): | 53 | def __remove_all_recursively(self, dictionary): |
| 53 | ''' Remove recursively all compromizing fields | 54 | """ Remove recursively all compromizing fields |
| 54 | ''' | 55 | """ |
| 55 | d = dict() | 56 | d = dict() |
| 56 | for i, j in [i for i in list(dictionary.items()) if i in self.fields]: | 57 | for i, j in [i for i in list(dictionary.items()) if i in self.fields]: |
| 57 | if isinstance(j, dict): | 58 | if isinstance(j, dict): |
| @@ -61,8 +62,8 @@ class TorrentStripper(parser.GenericParser): | |||
| 61 | return d | 62 | return d |
| 62 | 63 | ||
| 63 | def remove_all(self): | 64 | def remove_all(self): |
| 64 | ''' Remove all comprimizing fields | 65 | """ Remove all comprimizing fields |
| 65 | ''' | 66 | """ |
| 66 | decoded = '' | 67 | decoded = '' |
| 67 | with open(self.filename, 'r') as f: | 68 | with open(self.filename, 'r') as f: |
| 68 | decoded = bencode.bdecode(f.read()) | 69 | decoded = bencode.bdecode(f.read()) |
