-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Abstract SamConfig #5208
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
Merged
Merged
feat: Abstract SamConfig #5208
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bb64e9c
Abstract SamConfig and decouple TOML logic
16ba0ed
Fix documentation and comments
da30267
Generalize exception for FileManager
c352d7e
Remove FileManager logic to its own file
7bb0dc3
Fix bug in setting a default FileManager
0b7e613
Implement requested changes
d4549cc
Include supported extensions in log call
58c270e
Implement requested changes
1e6bde8
Update docstrings
29b7e94
Refactor changes to preserve TOML comments
1b4e583
Allow file document to update properly
1da7c89
Remove duplicate data
3c41bc0
Add put comment for FileManager
24d1564
Implement requested changes
f222284
Format files according to standard
33bb999
Implement helper method for dict-like to TOMLDocument
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,161 @@ | ||
| """ | ||
| Class to represent the parsing of different file types into Python objects. | ||
| """ | ||
|
|
||
|
|
||
| import logging | ||
| from abc import ABC, abstractmethod | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import tomlkit | ||
|
|
||
| from samcli.lib.config.exceptions import FileParseException | ||
|
|
||
| LOG = logging.getLogger(__name__) | ||
| COMMENT_KEY = "__comment__" | ||
|
|
||
|
|
||
| class FileManager(ABC): | ||
Leo10Gama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Abstract class to be overridden by file managers for specific file extensions. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def read(filepath: Path) -> Any: | ||
| """ | ||
| Read a file at a given path. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| filepath: Path | ||
| The Path object that points to the file to be read. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| A dictionary-like representation of the contents at the filepath location, along with a specialized | ||
| representation of the file that was read, if there is a specialization of it. | ||
| """ | ||
| raise NotImplementedError("Read method not implemented.") | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def write(document: dict, filepath: Path): | ||
| """ | ||
| Write a dictionary or dictionary-like object to a given file. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| document: dict | ||
| The object to write. | ||
| filepath: Path | ||
| The final location for the document to be written. | ||
| """ | ||
| raise NotImplementedError("Write method not implemented.") | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def put_comment(document: Any, comment: str) -> Any: | ||
| """ | ||
| Put a comment in a document object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| document: Any | ||
| The object to write | ||
| comment: str | ||
| The comment to include in the document. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The new document, with the comment added to it. | ||
| """ | ||
| raise NotImplementedError("Put comment method not implemented.") | ||
|
|
||
|
|
||
| class TomlFileManager(FileManager): | ||
| """ | ||
| Static class to read and write toml files. | ||
| """ | ||
|
|
||
| file_format = "TOML" | ||
|
|
||
| @staticmethod | ||
| def read(filepath: Path) -> Any: | ||
| """ | ||
| Read a TOML file at the given path. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| filepath: Path | ||
| The Path object that points to the file to be read. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| A dictionary-like tomlkit.TOMLDocument object, which represents the contents of the TOML file at the | ||
| provided location. | ||
| """ | ||
| toml_doc = tomlkit.document() | ||
| try: | ||
| txt = filepath.read_text() | ||
| toml_doc = tomlkit.loads(txt) | ||
| except OSError as e: | ||
| LOG.debug(f"OSError occurred while reading {TomlFileManager.file_format} file: {str(e)}") | ||
| except tomlkit.exceptions.TOMLKitError as e: | ||
mildaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| raise FileParseException(e) from e | ||
|
|
||
| return toml_doc | ||
|
|
||
| @staticmethod | ||
| def write(document: dict, filepath: Path): | ||
| """ | ||
| Write the contents of a dictionary or tomlkit.TOMLDocument to a TOML file at the provided location. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| document: dict | ||
| The object to write. | ||
| filepath: Path | ||
| The final location for the TOML file to be written. | ||
| """ | ||
| if not document: | ||
| LOG.debug("Nothing for TomlFileManager to write.") | ||
| return | ||
Leo10Gama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| toml_document = TomlFileManager._to_toml(document) | ||
|
|
||
| if toml_document.get(COMMENT_KEY, None): # Remove dunder comments that may be residue from other formats | ||
| toml_document.add(tomlkit.comment(toml_document[COMMENT_KEY])) | ||
| toml_document.pop(COMMENT_KEY) | ||
|
|
||
| filepath.write_text(tomlkit.dumps(toml_document)) | ||
|
|
||
| @staticmethod | ||
| def put_comment(document: dict, comment: str) -> Any: | ||
| """ | ||
| Put a comment in a document object. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| document: Any | ||
| The tomlkit.TOMLDocument object to write | ||
| comment: str | ||
| The comment to include in the document. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The new TOMLDocument, with the comment added to it. | ||
| """ | ||
| document = TomlFileManager._to_toml(document) | ||
| document.add(tomlkit.comment(comment)) | ||
| return document | ||
|
|
||
| @staticmethod | ||
| def _to_toml(document: dict) -> tomlkit.TOMLDocument: | ||
| """Ensure that a dictionary-like object is a TOMLDocument.""" | ||
| return tomlkit.parse(tomlkit.dumps(document)) | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.