util: Consolidate parse functions into util

- Move functions to the 'util' to centralize common
functionality, reducing code duplication and improving
maintainability across the codebase.

Signed-off-by: Renata Ravanelli <rravanel@redhat.com>
This commit is contained in:
Renata Ravanelli 2024-02-27 13:14:35 -03:00 committed by Michael Vogt
parent 4f8ff6bc99
commit 6d4d1962eb
4 changed files with 111 additions and 102 deletions

View file

@ -1,7 +1,10 @@
"""Helpers related to parsing"""
import os
import re
from typing import Union
from typing import Union, Dict
from urllib.parse import ParseResult, urlparse
def parse_size(s: str) -> Union[int, str]:
@ -31,3 +34,71 @@ def parse_size(s: str) -> Union[int, str]:
return "unlimited"
raise TypeError(f"invalid size value: '{s}'")
def parse_mount(url: ParseResult, args: Dict):
"""
Parses the mount URL to extract the root path.
Parameters:
- url (ParseResult): The ParseResult object obtained from urlparse.
- args (Dict): A dictionary containing arguments including mounts information.
"""
name = url.netloc
if name:
root = args["mounts"].get(name, {}).get("path")
if not root:
raise ValueError(f"Unknown mount '{name}'")
else:
root = args["paths"]["mounts"]
return root
def parse_input(url: ParseResult, args: Dict):
"""
Parses the input URL to extract the root path.
Parameters:
- url (ParseResult): The ParseResult object obtained from urlparse.
- args (Dict): A dictionary containing arguments including mounts information.
"""
name = url.netloc
root = args["inputs"].get(name, {}).get("path")
if root is None:
raise ValueError(f"Unknown input '{name}'")
return root
def parse_location(location, args):
"""
Parses the location URL to derive the corresponding file path.
Parameters:
- location (str): The location URL to be parsed.
- args (Dict): A dictionary containing arguments including tree and mount information.
"""
url = urlparse(location)
scheme = url.scheme
if scheme == "tree":
root = args["tree"]
elif scheme == "mount":
root = parse_mount(url, args)
elif scheme == "input":
root = parse_input(url, args)
else:
raise ValueError(f"Unsupported scheme '{scheme}'")
assert url.path.startswith("/")
path = os.path.relpath(url.path, "/")
path = os.path.join(root, path)
path = os.path.normpath(path)
if url.path.endswith("/"):
path = os.path.join(path, ".")
return path