(value, safechar=True)
| 607 | |
| 608 | |
| 609 | def parse_name(value, safechar=True): |
| 610 | path = urllib.unquote(decode(value)) |
| 611 | |
| 612 | try: |
| 613 | path = path.encode('latin1').decode('utf8') |
| 614 | except (UnicodeEncodeError, UnicodeDecodeError): |
| 615 | pass |
| 616 | |
| 617 | #: 'unicode-escape' that work with unicode strings too |
| 618 | path = re.sub(r'\\u(\d{4})', lambda x: unichr(int(x.group(1))), path, flags=re.I) |
| 619 | |
| 620 | #: Decode HTML escape |
| 621 | path = html_unescape(path) |
| 622 | |
| 623 | #: Decode rfc2047 |
| 624 | path = rfc2047_dec(path) |
| 625 | |
| 626 | #: Remove redundant '/' |
| 627 | path = re.sub(r'(?<!:)/{2,}', '/', path).strip().lstrip('.') |
| 628 | |
| 629 | url_p = urlparse.urlparse(path.rstrip('/')) |
| 630 | name = (url_p.path.split('/')[-1] or |
| 631 | url_p.query.split('=', 1)[::-1][0].split('&', 1)[0] or |
| 632 | url_p.netloc.split('.', 1)[0]) |
| 633 | |
| 634 | name = os.path.basename(name) |
| 635 | |
| 636 | return safename(name) if safechar else name |
| 637 | |
| 638 | |
| 639 | def parse_size(value, unit=""): #: returns bytes |
no test coverage detected