Skip to content

Commit c1cf148

Browse files
committed
refactor: ♻️ move hash id generation to a helper function
1 parent 364a6a8 commit c1cf148

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/controller/base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from configparser import ConfigParser
44
from datetime import datetime, timezone
55
from enum import IntEnum
6-
from hashlib import blake2b # requires python>=3.6, otherwise install pyblake2
76
from urllib.parse import urlparse
87

98
import jsonschema
@@ -199,7 +198,7 @@ def get_all(cls, size=10, from_=0, query_data=None):
199198
query_body = query_data["body"]
200199
search = search.query(query_type, **query_body)
201200
search = search.source(False)
202-
search = search[from_ : from_ + size]
201+
search = search[from_: from_ + size]
203202

204203
for hit in search:
205204
try: # unlikely but possible
@@ -243,9 +242,7 @@ def get(cls, _id):
243242
@property
244243
def _id(self):
245244
# can be a cached property in python 3.8+
246-
_bytes = str(self._url).encode("utf8")
247-
_hash = blake2b(_bytes, digest_size=16)
248-
return _hash.hexdigest()
245+
return decoder.get_id(self._url)
249246

250247
@property
251248
def url(self):

src/utils/decoder.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gzip
44
import json
5+
from hashlib import blake2b
56

67
import yaml
78

@@ -62,7 +63,7 @@ def to_dict(stream, ext=None, ctype=None):
6263
pass
6364
if isinstance(stream, str):
6465
if stream.startswith("export default "):
65-
stream = stream[len("export default ") :]
66+
stream = stream[len("export default "):]
6667

6768
# brute force
6869
return to_yaml(stream)
@@ -79,3 +80,12 @@ def compress(stream):
7980

8081
def decompress(stream):
8182
return gzip.decompress(stream) if stream else None
83+
84+
85+
# -------------
86+
# Hashed _id
87+
# -------------
88+
def get_id(url):
89+
_bytes = str(url).encode("utf8")
90+
_hash = blake2b(_bytes, digest_size=16)
91+
return _hash.hexdigest()

0 commit comments

Comments
 (0)