Move "sanitize_windows_path" to PFERD.transform

This commit is contained in:
I-Al-Istannen 2020-11-12 20:52:46 +01:00
parent 98834c9c95
commit cd90a60dee
2 changed files with 19 additions and 14 deletions

View File

@ -5,6 +5,8 @@ only files whose names match a regex, or renaming files from one numbering
scheme to another. scheme to another.
""" """
import os
import re
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import PurePath from pathlib import PurePath
from typing import Callable, List, Optional, TypeVar from typing import Callable, List, Optional, TypeVar
@ -45,7 +47,8 @@ def apply_transform(
# Transform combinators # Transform combinators
keep = lambda path: path def keep(path: PurePath) -> Optional[PurePath]:
return path
def attempt(*args: Transform) -> Transform: def attempt(*args: Transform) -> Transform:
def inner(path: PurePath) -> Optional[PurePath]: def inner(path: PurePath) -> Optional[PurePath]:
@ -125,3 +128,15 @@ def re_rename(regex: Regex, target: str) -> Transform:
return path.with_name(target.format(*groups)) return path.with_name(target.format(*groups))
return None return None
return inner return inner
def sanitize_windows_path(path: PurePath) -> Optional[PurePath]:
"""
A small function to escape characters that are forbidden in windows path names.
This method is a no-op on other operating systems.
"""
# Escape windows illegal path characters
if os.name == 'nt':
sanitized_parts = [re.sub(r'[<>:"/|?]', "_", x) for x in list(path.parts)]
return PurePath(*sanitized_parts)
return path

View File

@ -5,27 +5,17 @@ A simple script to download a course by name from ILIAS.
""" """
import argparse import argparse
import os from pathlib import Path
import re
from pathlib import Path, PurePath
from typing import Optional
from urllib.parse import urlparse from urllib.parse import urlparse
from PFERD import Pferd from PFERD import Pferd
from PFERD.cookie_jar import CookieJar from PFERD.cookie_jar import CookieJar
from PFERD.ilias import (IliasCrawler, IliasElementType, from PFERD.ilias import (IliasCrawler, IliasElementType,
KitShibbolethAuthenticator) KitShibbolethAuthenticator)
from PFERD.transform import sanitize_windows_path
from PFERD.utils import to_path from PFERD.utils import to_path
def sanitize_path(path: PurePath) -> Optional[PurePath]:
# Escape windows illegal path characters
if os.name == 'nt':
sanitized_parts = [re.sub(r'[<>:"/|?]', "_", x) for x in list(path.parts)]
return PurePath(*sanitized_parts)
return path
def main() -> None: def main() -> None:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--test-run", action="store_true") parser.add_argument("--test-run", action="store_true")
@ -71,7 +61,7 @@ def main() -> None:
full_url=args.url, full_url=args.url,
cookies=args.cookies, cookies=args.cookies,
dir_filter=dir_filter, dir_filter=dir_filter,
transform=sanitize_path transform=sanitize_windows_path
) )