56 lines
1.7 KiB
Diff
56 lines
1.7 KiB
Diff
|
From 5a1bff33c692be2fc1add61e642111976858c672 Mon Sep 17 00:00:00 2001
|
||
|
From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?=
|
||
|
<congdanhqx@gmail.com>
|
||
|
Date: Mon, 27 Jul 2020 17:31:06 +0700
|
||
|
Subject: [PATCH] tools/magic: refactor get_magics function
|
||
|
MIME-Version: 1.0
|
||
|
Content-Type: text/plain; charset=UTF-8
|
||
|
Content-Transfer-Encoding: 8bit
|
||
|
|
||
|
In a later patch, we will add support for python-magic.
|
||
|
Refactore get_magics function to prepare for that change.
|
||
|
|
||
|
Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
|
||
|
---
|
||
|
pass_import/tools.py | 14 ++++++++++----
|
||
|
1 file changed, 10 insertions(+), 4 deletions(-)
|
||
|
|
||
|
diff --git pass_import/tools.py pass_import/tools.py
|
||
|
index 257f01a..815ff8b 100644
|
||
|
--- pass_import/tools.py
|
||
|
+++ pass_import/tools.py
|
||
|
@@ -39,20 +39,26 @@ def get_magics(path):
|
||
|
with open(path, 'rb') as file:
|
||
|
header = file.read(2048)
|
||
|
|
||
|
- res = magic.detect_from_content(header)
|
||
|
+ if hasattr(magic, 'detect_from_content'):
|
||
|
+ res = magic.detect_from_content(header)
|
||
|
+ mime_type = res.mime_type
|
||
|
+ magic_name = res.name
|
||
|
+ else:
|
||
|
+ return None, None
|
||
|
+
|
||
|
mime_to_format = {
|
||
|
'application/pgp': 'gpg',
|
||
|
'application/x-sqlite3': 'sqlite3'
|
||
|
}
|
||
|
name_to_format = {'KDBX': 'kdbx', 'openssl': 'openssl', 'PGP': 'gpg'}
|
||
|
|
||
|
- frmt = mime_to_format.get(res.mime_type, None)
|
||
|
+ frmt = mime_to_format.get(mime_type, None)
|
||
|
for name in name_to_format:
|
||
|
- if name in res.name:
|
||
|
+ if name in magic_name:
|
||
|
frmt = name_to_format[name]
|
||
|
|
||
|
encoding = None # res.encoding
|
||
|
- if 'UTF-8 Unicode (with BOM)' in res.name:
|
||
|
+ if 'UTF-8 Unicode (with BOM)' in magic_name:
|
||
|
encoding = 'utf-8-sig'
|
||
|
|
||
|
return frmt, encoding
|
||
|
--
|
||
|
2.28.0.163.g6104cc2f0b
|
||
|
|