Skip to content

Commit

Permalink
feat: add get_file_hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
HADB committed Sep 11, 2024
1 parent 4c62af2 commit 78f0176
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Support .json, .yaml, .ini files.
Support auto reloading while config file changes.

```python
config_json = Config(os.path.abspath("config.json"))
config_yaml = Config(os.path.abspath("config.yaml"))
config_ini = Config(os.path.abspath("config.ini"))
config_json = Config("configs/config.json")
config_yaml = Config("configs/config.yaml")
config_ini = Config("configs/config.ini")

print(config_ini["app"]["config_a"])
print(config_yaml["movie"]["name"])
Expand All @@ -30,7 +30,7 @@ logger = Logger(name="my-logger", level=logging.INFO)

logger.debug("debug log")
logger.info("info log")
logger.warn("warn log")
logger.warning("warning log")
logger.error("error log")
```

Expand Down Expand Up @@ -74,3 +74,10 @@ Webhook group robot
robot = GroupRobot(webhook="your robot's webhook path")
robot.send(data)
```

## hash

```python
from yuanfen import hash
get_file_hash("path/to/file")
```
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ classifiers = [
]
packages = [{ include = "yuanfen", from = "src" }]

[tool.poetry.urls]
Homepage = "https://github.com/YuanfenNet/yf-lib-py"

[tool.poetry.dependencies]
python = "^3.9"
chardet = "^5.2.0"
Expand Down
3 changes: 2 additions & 1 deletion src/yuanfen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import ip, time
from . import hash, ip, time
from .config import Config
from .email import Email
from .env import APP_ENV
Expand All @@ -20,6 +20,7 @@
"RedisLock",
"SuccessResponse",
"Version",
"hash",
"ip",
"time",
]
11 changes: 11 additions & 0 deletions src/yuanfen/hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hashlib


def get_file_hash(file_path, algorithm="sha256", chunk_size=8192, length=None):
hash_obj = hashlib.new(algorithm)
with open(file_path, "rb") as f:
while chunk := f.read(chunk_size):
hash_obj.update(chunk)
if length:
return hash_obj.hexdigest()[:length]
return hash_obj.hexdigest()

0 comments on commit 78f0176

Please sign in to comment.