Don't redownload voice files if sha256 sum matches
This commit is contained in:
parent
fbd26a02d5
commit
c3a0bf3865
2 changed files with 98 additions and 14 deletions
|
|
@ -13,6 +13,11 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
"""Utility methods for Mimic 3"""
|
||||
import hashlib
|
||||
import re
|
||||
import typing
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
|
@ -24,3 +29,35 @@ def audio_float_to_int16(
|
|||
audio_norm = np.clip(audio_norm, -max_wav_value, max_wav_value)
|
||||
audio_norm = audio_norm.astype("int16")
|
||||
return audio_norm
|
||||
|
||||
|
||||
def wildcard_to_regex(template: str, wildcard: str = "*") -> re.Pattern:
|
||||
"""Convert a string with wildcards into a regex pattern"""
|
||||
wildcard_escaped = re.escape(wildcard)
|
||||
|
||||
pattern_parts = ["^"]
|
||||
for i, template_part in enumerate(re.split(f"({wildcard_escaped})", template)):
|
||||
if (i % 2) == 0:
|
||||
# Fixed string
|
||||
pattern_parts.append(re.escape(template_part))
|
||||
else:
|
||||
# Wildcard separator
|
||||
pattern_parts.append(".*")
|
||||
|
||||
pattern_parts.append("$")
|
||||
pattern_str = "".join(pattern_parts)
|
||||
|
||||
return re.compile(pattern_str)
|
||||
|
||||
|
||||
def file_sha256_sum(fp: typing.BinaryIO, block_bytes: int = 4096) -> str:
|
||||
"""Return the sha256 sum of a (possibly large) file"""
|
||||
current_hash = hashlib.sha256()
|
||||
|
||||
# Read in blocks in case file is very large
|
||||
block = fp.read(block_bytes)
|
||||
while len(block) > 0:
|
||||
current_hash.update(block)
|
||||
block = fp.read(block_bytes)
|
||||
|
||||
return current_hash.hexdigest()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue