-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pyproject.toml writer #150
Changes from all commits
5e0a782
46248dd
ee7c8f3
d6ce06f
20a838f
d0827ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import tomlkit | ||
from typing import Optional | ||
from copy import deepcopy | ||
from codemodder.dependency import Dependency | ||
from codemodder.change import Action, Change, ChangeSet, PackageAction, Result | ||
from codemodder.dependency_management.base_dependency_writer import DependencyWriter | ||
from codemodder.diff import create_diff_and_linenums | ||
|
||
|
||
class PyprojectWriter(DependencyWriter): | ||
def add_to_file( | ||
self, dependencies: list[Dependency], dry_run: bool = False | ||
) -> Optional[ChangeSet]: | ||
pyproject = self._parse_file() | ||
original = deepcopy(pyproject) | ||
|
||
try: | ||
pyproject["project"]["dependencies"].extend( | ||
[f"{dep.requirement}" for dep in dependencies] | ||
) | ||
except tomlkit.exceptions.NonExistentKey: | ||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we probably ought to have some kind of debug logging either here or at the caller. |
||
|
||
diff, added_line_nums = create_diff_and_linenums( | ||
tomlkit.dumps(original).split("\n"), tomlkit.dumps(pyproject).split("\n") | ||
) | ||
|
||
if not dry_run: | ||
with open(self.path, "w", encoding="utf-8") as f: | ||
tomlkit.dump(pyproject, f) | ||
|
||
changes = [ | ||
Change( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be factored out into the base class now since it includes some fiddly logic that will be pretty much the same for each writer. |
||
lineNumber=added_line_nums[i], | ||
description=dep.build_description(), | ||
# Contextual comments should be added to the right side of split diffs | ||
properties={ | ||
"contextual_description": True, | ||
"contextual_description_position": "right", | ||
}, | ||
packageActions=[ | ||
PackageAction(Action.ADD, Result.COMPLETED, str(dep.requirement)) | ||
], | ||
) | ||
for i, dep in enumerate(dependencies) | ||
] | ||
return ChangeSet( | ||
str(self.path.relative_to(self.parent_directory)), | ||
diff, | ||
changes=changes, | ||
) | ||
|
||
def _parse_file(self): | ||
with open(self.path, encoding="utf-8") as f: | ||
return tomlkit.load(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel pretty strongly that these kinds of values should be represented by an enum. The fact that it is being used as cases for
match
really emphasizes that to me.