From 390004bcd46badd57958dd5bc27208f7eca0d9b5 Mon Sep 17 00:00:00 2001 From: lixiang_yewu Date: Mon, 11 Sep 2023 01:48:00 +0000 Subject: [PATCH] update pyporter/utils.py. Signed-off-by: lixiang_yewu --- pyporter/utils.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/pyporter/utils.py b/pyporter/utils.py index 7db69e1..0966147 100644 --- a/pyporter/utils.py +++ b/pyporter/utils.py @@ -24,28 +24,30 @@ import re # return ns[0] # TODO: this should be more compatible for https://peps.python.org/pep-0508/ -def transform_module_name(input_str): - # Extracting the module name from the input string - module_name = re.match(r"([a-zA-Z0-9_-]+)", input_str).group(1).strip() - version_names = input_str[len(module_name):].strip() - # Extracting the version constraint from the input string - version_constraint = version_names.split(",") - package_name = "python3-" + module_name - if len(version_constraint) > 1: - constraints_string = " with ".join([ - f"{package_name}{constraint}" for constraint in version_constraint - ]) - result_string = f"({constraints_string})" +def transform_module_name(input_str: str) -> str: + """ + Transform input module name and version constraint to a refined string. + """ + match = re.match(r"([a-zA-Z0-9_-]+)(.*)", input_str) + if match: + module_name, version_names = match.groups() + version_constraint = version_names.strip(',').split(",") + package_name = "python3-" + module_name.strip() + if version_constraint: + constraints_string = " with ".join([f"{package_name}{constraint.strip()}" for constraint in version_constraint]) + result_string = f"({constraints_string})" + else: + result_string = f"({package_name})" + return result_string else: - result_string = f"({package_name}{version_constraint[0]})" - - return result_string - + return "" # Handle the case where the input doesn't match the expected pattern def refine_requires(req: str) -> str: """ - return only requires without ';' (thus no extra) + Return only requires without ';' (thus no extra). """ - ra = req.split(";", 1) - # Do not add requires which has ;, which is often has very complicated precondition - return transform_module_name(ra[0]) + split_requirements = req.split(";", 1) + if split_requirements: + return transform_module_name(split_requirements[0]) + else: + return "" \ No newline at end of file -- Gitee