-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move constraint parsing of generic constraints from __init_…
…_ into parser module and rename module for constraint parsing of version constraints from helpers to parser
- Loading branch information
Showing
6 changed files
with
89 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from poetry.core.constraints.generic.any_constraint import AnyConstraint | ||
from poetry.core.constraints.generic.constraint import Constraint | ||
from poetry.core.constraints.generic.union_constraint import UnionConstraint | ||
|
||
|
||
if TYPE_CHECKING: | ||
from poetry.core.constraints.generic.base_constraint import BaseConstraint | ||
|
||
|
||
BASIC_CONSTRAINT = re.compile(r"^(!?==?)?\s*([^\s]+?)\s*$") | ||
|
||
|
||
def parse_constraint(constraints: str) -> BaseConstraint: | ||
if constraints == "*": | ||
return AnyConstraint() | ||
|
||
or_constraints = re.split(r"\s*\|\|?\s*", constraints.strip()) | ||
or_groups = [] | ||
for constraints in or_constraints: | ||
and_constraints = re.split( | ||
r"(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints | ||
) | ||
constraint_objects = [] | ||
|
||
if len(and_constraints) > 1: | ||
for constraint in and_constraints: | ||
constraint_objects.append(parse_single_constraint(constraint)) | ||
else: | ||
constraint_objects.append(parse_single_constraint(and_constraints[0])) | ||
|
||
if len(constraint_objects) == 1: | ||
constraint = constraint_objects[0] | ||
else: | ||
constraint = constraint_objects[0] | ||
for next_constraint in constraint_objects[1:]: | ||
constraint = constraint.intersect(next_constraint) | ||
|
||
or_groups.append(constraint) | ||
|
||
if len(or_groups) == 1: | ||
return or_groups[0] | ||
else: | ||
return UnionConstraint(*or_groups) | ||
|
||
|
||
def parse_single_constraint(constraint: str) -> Constraint: | ||
# Basic comparator | ||
m = BASIC_CONSTRAINT.match(constraint) | ||
if m: | ||
op = m.group(1) | ||
if op is None: | ||
op = "==" | ||
|
||
version = m.group(2).strip() | ||
|
||
return Constraint(version, op) | ||
|
||
raise ValueError(f"Could not parse version constraint: {constraint}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from poetry.core.constraints.version import parse_constraint | ||
from poetry.core.constraints.version.parser import parse_constraint | ||
from poetry.core.constraints.version.parser import parse_single_constraint | ||
|
||
|
||
__all__ = ["parse_constraint"] | ||
__all__ = ["parse_constraint", "parse_single_constraint"] |