diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 21:38:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 21:38:38 +0000 |
commit | 2e2851dc13d73352530dd4495c7e05603b2e520d (patch) | |
tree | 622b9cd8e5d32091c9aa9e4937b533975a40356c /deluge/plugins/Blocklist/deluge_blocklist/detect.py | |
parent | Initial commit. (diff) | |
download | deluge-2e2851dc13d73352530dd4495c7e05603b2e520d.tar.xz deluge-2e2851dc13d73352530dd4495c7e05603b2e520d.zip |
Adding upstream version 2.1.2~dev0+20240219.upstream/2.1.2_dev0+20240219upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'deluge/plugins/Blocklist/deluge_blocklist/detect.py')
-rw-r--r-- | deluge/plugins/Blocklist/deluge_blocklist/detect.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/deluge/plugins/Blocklist/deluge_blocklist/detect.py b/deluge/plugins/Blocklist/deluge_blocklist/detect.py new file mode 100644 index 0000000..43ad305 --- /dev/null +++ b/deluge/plugins/Blocklist/deluge_blocklist/detect.py @@ -0,0 +1,48 @@ +# +# Copyright (C) 2009-2010 John Garland <johnnybg+deluge@gmail.com> +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +from .decompressers import BZipped2, GZipped, Zipped +from .readers import EmuleReader, PeerGuardianReader, SafePeerReader + +COMPRESSION_TYPES = {b'PK': 'Zip', b'\x1f\x8b': 'GZip', b'BZ': 'BZip2'} + +DECOMPRESSERS = {'Zip': Zipped, 'GZip': GZipped, 'BZip2': BZipped2} + +READERS = { + 'Emule': EmuleReader, + 'SafePeer': SafePeerReader, + 'PeerGuardian': PeerGuardianReader, +} + + +class UnknownFormatError(Exception): + pass + + +def detect_compression(filename): + with open(filename, 'rb') as _file: + magic_number = _file.read(2) + return COMPRESSION_TYPES.get(magic_number, '') + + +def detect_format(filename, compression=''): + file_format = '' + for reader in READERS: + if create_reader(reader, compression)(filename).is_valid(): + file_format = reader + break + return file_format + + +def create_reader(file_format, compression=''): + reader = READERS.get(file_format) + if reader and compression: + decompressor = DECOMPRESSERS.get(compression) + if decompressor: + reader = decompressor(reader) + return reader |