Package org.openrewrite.python.internal
Class PyProjectHelper
java.lang.Object
org.openrewrite.python.internal.PyProjectHelper
Shared utilities for Python dependency recipes operating on pyproject.toml files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcorrespondingPyprojectPath(String uvLockPath) Derive the pyproject.toml path that corresponds to a uv.lock path.static @Nullable StringextractPackageName(String pep508Spec) Extract the package name from a PEP 508 dependency spec string.static @Nullable PythonResolutionResult.DependencyfindDependencyInScope(PythonResolutionResult marker, String packageName, @Nullable String scope, @Nullable String groupName) Find a dependency in the specified scope of the marker.static booleanisInsideDependencyArray(org.openrewrite.Cursor cursor, @Nullable String scope, @Nullable String groupName) Check whether a cursor path represents a position inside a dependency array for the given scope and optional group name.static booleanisInsidePdmOverridesTable(org.openrewrite.Cursor cursor) Check whether a cursor path represents a position inside the[tool.pdm.overrides]table in a pyproject.toml.static booleanisInsideProjectDependencies(org.openrewrite.Cursor cursor) Check whether a cursor path represents a position inside the[project].dependenciesarray in a pyproject.toml.static @Nullable org.openrewrite.toml.tree.Toml.DocumentmaybeUpdateUvLock(org.openrewrite.toml.tree.Toml.Document document, org.openrewrite.ExecutionContext ctx) If there is regenerated uv.lock content for this document, reparse the document from that content.static StringnormalizeVersionConstraint(String version) Normalize a version constraint so it is valid PEP 508.static org.openrewrite.toml.tree.Toml.DocumentregenerateLockAndRefreshMarker(org.openrewrite.toml.tree.Toml.Document updated, org.openrewrite.ExecutionContext ctx) After modifying a pyproject.toml document, regenerate the uv.lock file and refresh thePythonResolutionResultmarker.static org.openrewrite.toml.tree.Toml.DocumentreparseToml(org.openrewrite.toml.tree.Toml.Document original, String newContent) Reparse a TOML document from new content while preserving the original document's identity (id) and markers.
-
Constructor Details
-
PyProjectHelper
public PyProjectHelper()
-
-
Method Details
-
normalizeVersionConstraint
Normalize a version constraint so it is valid PEP 508. When the value does not start with a comparison operator (>=,<=, etc.) we default to>=. -
extractPackageName
Extract the package name from a PEP 508 dependency spec string. The name is the first token before any version specifier, extras, or marker. -
correspondingPyprojectPath
Derive the pyproject.toml path that corresponds to a uv.lock path. -
maybeUpdateUvLock
public static @Nullable org.openrewrite.toml.tree.Toml.Document maybeUpdateUvLock(org.openrewrite.toml.tree.Toml.Document document, org.openrewrite.ExecutionContext ctx) If there is regenerated uv.lock content for this document, reparse the document from that content. Returnsnullwhen no update is needed (either no regenerated content exists, or the current document already matches).After reparsing, the stored content is normalized to the printer output so that subsequent recipe cycles see identical content and do not trigger spurious changes.
-
reparseToml
public static org.openrewrite.toml.tree.Toml.Document reparseToml(org.openrewrite.toml.tree.Toml.Document original, String newContent) Reparse a TOML document from new content while preserving the original document's identity (id) and markers. -
regenerateLockAndRefreshMarker
public static org.openrewrite.toml.tree.Toml.Document regenerateLockAndRefreshMarker(org.openrewrite.toml.tree.Toml.Document updated, org.openrewrite.ExecutionContext ctx) After modifying a pyproject.toml document, regenerate the uv.lock file and refresh thePythonResolutionResultmarker. Returns the updated document.Lock file state is shared across all Python dependency recipes via the
ExecutionContext, so sequential recipes in a composite correctly build on each other's lock regeneration results.- Parameters:
updated- the modified pyproject.toml documentctx- the execution context holding shared lock file state- Returns:
- the document with refreshed marker (and possibly a warning markup)
-
isInsideProjectDependencies
public static boolean isInsideProjectDependencies(org.openrewrite.Cursor cursor) Check whether a cursor path represents a position inside the[project].dependenciesarray in a pyproject.toml. -
isInsideDependencyArray
public static boolean isInsideDependencyArray(org.openrewrite.Cursor cursor, @Nullable String scope, @Nullable String groupName) Check whether a cursor path represents a position inside a dependency array for the given scope and optional group name.Scope values use TOML dotted-key path syntax:
nullor"project.dependencies"→[project].dependencies"build-system.requires"→[build-system].requires"project.optional-dependencies"→[project.optional-dependencies].<groupName>"dependency-groups"→[dependency-groups].<groupName>"tool.uv.constraint-dependencies"→[tool.uv].constraint-dependencies"tool.uv.override-dependencies"→[tool.uv].override-dependencies
-
findDependencyInScope
public static @Nullable PythonResolutionResult.Dependency findDependencyInScope(PythonResolutionResult marker, String packageName, @Nullable String scope, @Nullable String groupName) Find a dependency in the specified scope of the marker.- Parameters:
marker- the resolution result markerpackageName- the package name to findscope- the scope to search (null means project.dependencies)groupName- the group name (required for optional-dependencies and dependency-groups)- Returns:
- the dependency, or null if not found
-
isInsidePdmOverridesTable
public static boolean isInsidePdmOverridesTable(org.openrewrite.Cursor cursor) Check whether a cursor path represents a position inside the[tool.pdm.overrides]table in a pyproject.toml.
-