nautilus 右键属性显示HASH值 (MD5 SHA256)

nautilus 右键属性显示HASH值 (MD5 SHA256)

进入 ~/.local/share/nautilus-python/extensions 目录

如果没有就使用 mkdir -p ~/.local/share/nautilus-python/extensions 新建一个

在extensions 目录新建一个 MD5SumPropertiesModel.py 文件, 并填入以下内容

 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,
            ),
        ]

😠 注意: class 名字必须和文件名字一样才能被识别

重启 nautilus 即可生效

调试插件方法

1
2
3
mkdir /tmp/testing
export TMPDIR=/tmp/testing
nautilus --no-desktop

这样启动nautilus 的话, 插件里的print 可以直接在终端输出, 方便调试

Licensed under CC BY-NC-SA 4.0