Skip to content

Commit a7dd786

Browse files
committed
Fix incorrect parsing of versions from dependencies if e.g. extras are stated to be installed #48 #42
1 parent c4d5afb commit a7dd786

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

codemeta/parsers/python.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import importlib
44
import re
5+
import ast
56
from typing import Union, IO
67

78
if sys.version_info.minor < 8:
@@ -31,6 +32,11 @@ def splitdependencies(s: str):
3132
"""Split a string of multiple (python) dependencies"""
3233
begin = 0
3334
depth = 0
35+
if s and s.find("{") != -1 and s.strip()[-1] == "}":
36+
# dependency is a dict-like structures usually from pyproject.toml {'version':.. , 'extras':... }, it does not contain multiple, return as is
37+
yield s
38+
return
39+
#normal behaviour; PEP-style dependency parsing
3440
for i, c in enumerate(s):
3541
if c == "(":
3642
depth += 1
@@ -53,6 +59,7 @@ def parsedependency(s: str):
5359
s.find("~") if s.find("~") != -1 else 999999,
5460
s.find("=") if s.find("=") != -1 else 999999,
5561
s.find("^") if s.find("^") != -1 else 999999,
62+
s.find("{") if s.find("{") != -1 else 999999,
5663
)
5764
if end != 999999:
5865
identifier = s[:end]
@@ -66,7 +73,20 @@ def parsedependency(s: str):
6673
break
6774
if versionbegin != -1:
6875
operator = s[end:versionbegin].strip()
69-
if operator in ("=", "=="):
76+
if s[versionbegin] == "{":
77+
version = s[versionbegin:].strip()
78+
if version and version[0] == "{" and version[-1] == "}":
79+
try:
80+
data = ast.literal_eval(version)
81+
if 'version' in data:
82+
return identifier, data['version']
83+
else:
84+
print("Unable to parse dependency version (key not found):", version,file=sys.stderr)
85+
version = ""
86+
except:
87+
print("Unable to parse dependency version: ", version,file=sys.stderr)
88+
version = ""
89+
elif operator in ("=", "=="):
7090
version = s[versionbegin:].strip()
7191
else:
7292
version = operator + " " + s[versionbegin:].strip()

0 commit comments

Comments
 (0)