mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
implement text.shorten_path/filename methods
This commit is contained in:
parent
882b1f31b1
commit
db479f881d
@ -8,7 +8,9 @@
|
||||
|
||||
"""Collection of functions that work in strings/text"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
import os.path
|
||||
import html.parser
|
||||
import urllib.parse
|
||||
import platform
|
||||
@ -40,6 +42,17 @@ def clean_path_posix(path):
|
||||
except AttributeError:
|
||||
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):
|
||||
try:
|
||||
first = txt.index(begin, pos) + len(begin)
|
||||
|
Loading…
Reference in New Issue
Block a user