implement text.shorten_path/filename methods
This commit is contained in:
@@ -8,7 +8,9 @@
|
|||||||
|
|
||||||
"""Collection of functions that work in strings/text"""
|
"""Collection of functions that work in strings/text"""
|
||||||
|
|
||||||
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import os.path
|
||||||
import html.parser
|
import html.parser
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import platform
|
import platform
|
||||||
@@ -40,6 +42,17 @@ def clean_path_posix(path):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
def shorten_path(path, limit=255, encoding=sys.getfilesystemencoding()):
|
||||||
|
"""Shorten a path segment to at most 'limit' bytes"""
|
||||||
|
return (path.encode(encoding)[:limit]).decode(encoding, "ignore")
|
||||||
|
|
||||||
|
def shorten_filename(filename, limit=255, encoding=sys.getfilesystemencoding()):
|
||||||
|
"""Shorten a filename to at most 'limit' bytes while preserving extension"""
|
||||||
|
name, extension = os.path.splitext(filename)
|
||||||
|
bext = extension.encode(encoding)
|
||||||
|
bname = name.encode(encoding)[:limit-len(bext)]
|
||||||
|
return bname.decode(encoding, "ignore") + extension
|
||||||
|
|
||||||
def extract(txt, begin, end, pos=0):
|
def extract(txt, begin, end, pos=0):
|
||||||
try:
|
try:
|
||||||
first = txt.index(begin, pos) + len(begin)
|
first = txt.index(begin, pos) + len(begin)
|
||||||
|
|||||||
Reference in New Issue
Block a user