From ce212cba93dc4401d870cd5e30e19ed36612c379 Mon Sep 17 00:00:00 2001 From: JoeLametta Date: Mon, 22 Oct 2018 08:00:00 +0000 Subject: [PATCH] Fix Unicode strings processing --- whipper/common/common.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/whipper/common/common.py b/whipper/common/common.py index 2686dead..853204b3 100644 --- a/whipper/common/common.py +++ b/whipper/common/common.py @@ -23,6 +23,7 @@ import os.path import math import subprocess +import unicodedata from whipper.extern import asyncsub @@ -156,13 +157,15 @@ class MissingFrames(Exception): def truncate_filename(path): """ Truncate filename to the max. len. allowed by the path's filesystem - Should handle unicode correctly too + Hopefully it handles Unicode strings correctly """ p, f = os.path.split(os.path.normpath(path)) f, e = os.path.splitext(f) - fn_lim = os.pathconf(p, 'PC_NAME_MAX') - length = fn_lim - len(e) - return os.path.join(p, f[:length] + e) + fn_lim = os.pathconf(p, 'PC_NAME_MAX') # max filenmae length in bytes + max = fn_lim - len(e.encode('utf-8')) + f = unicodedata.normalize('NFC', f) + f_trunc = unicode(f.encode('utf-8')[:max], 'utf-8', errors='ignore') + return os.path.join(p, f_trunc + e) def shrinkPath(path):