1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# -*- coding: utf-8 -*-
#********************************************************************************
#Copyright © 2023 Wcq
#File Name: MD5SumPropertiesModel.py
#Author: Wcq
#Email: wcq-062821@163.com
#Created: 2023-02-25 12:47:04
#Last Update: 2023-04-01 11:04:35
# By: Wcq
#Description:
#********************************************************************************
import hashlib
from urllib.parse import unquote
from gi.repository import Nautilus, Gio, GObject
from typing import List
class MD5SumPropertiesModel(GObject.GObject, Nautilus.PropertiesModelProvider):
def get_sha256_sum(self, f):
ret = 0
with open(f, 'rb') as fpr:
ret = hashlib.sha256(fpr.read()).hexdigest()
return ret
def get_md5_sum(self, f):
ret = 0
with open(f, 'rb') as fpr:
ret = hashlib.md5(fpr.read()).hexdigest()
return ret
def get_models(
self,
files: List[Nautilus.FileInfo],
) -> List[Nautilus.PropertiesModel]:
if len(files) != 1:
return []
file = files[0]
if file.get_uri_scheme() != "file":
return []
if file.is_directory():
return []
filename = unquote(file.get_uri()[7:])
section_model = Gio.ListStore.new(item_type=Nautilus.PropertiesItem)
section_model.append(
Nautilus.PropertiesItem(
name="MD5 sum of the %s"%filename,
value=self.get_md5_sum(filename),
)
)
section_model.append(
Nautilus.PropertiesItem(
name="SHA256 of the %s"%filename,
value=self.get_sha256_sum(filename),
)
)
return [
Nautilus.PropertiesModel(
title="HASH_VALUE",
model=section_model,
),
]
|