-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: add version to samconfig.toml
file
#1581
Changes from all commits
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,7 @@ | ||
""" | ||
Exceptions to be used by samconfig.py | ||
""" | ||
|
||
|
||
class SamConfigVersionException(Exception): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
|
||
import tomlkit | ||
|
||
from samcli.lib.config.version import SAM_CONFIG_VERSION, VERSION_KEY | ||
from samcli.lib.config.exceptions import SamConfigVersionException | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
DEFAULT_CONFIG_FILE_NAME = "samconfig.toml" | ||
|
@@ -21,7 +24,6 @@ class SamConfig: | |
""" | ||
|
||
document = None | ||
VERSION = "0.1" | ||
|
||
def __init__(self, config_dir, filename=None): | ||
""" | ||
|
@@ -101,8 +103,8 @@ def put(self, cmd_names, section, key, value, env=DEFAULT_ENV): | |
If the data is invalid | ||
""" | ||
|
||
self._read() | ||
if not self.document: | ||
self._read() | ||
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. bugfix: do not attempt to read everytime a put is made, read once and load. |
||
# Empty document prepare the initial structure. | ||
self.document.update({env: {self._to_key(cmd_names): {section: {key: value}}}}) | ||
# Only update appropriate key value pairs within a section | ||
|
@@ -149,15 +151,16 @@ def config_dir(template_file_path=None): | |
return os.getcwd() | ||
|
||
def _read(self): | ||
if self.document: | ||
return self.document | ||
|
||
try: | ||
txt = self.filepath.read_text() | ||
self.document = tomlkit.loads(txt) | ||
except OSError: | ||
self.document = tomlkit.document() | ||
|
||
if not self.document: | ||
try: | ||
txt = self.filepath.read_text() | ||
self.document = tomlkit.loads(txt) | ||
self._version_sanity_check(self._version()) | ||
except OSError: | ||
self.document = tomlkit.document() | ||
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. nitpick: Probably add a debug log before default to 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. Sure. Edit: actually lets think about that a bit more, if there is no config file we say we couldn't find it every-time under debug, I'm not sure if thats the behavior we want. |
||
|
||
if self.document.body: | ||
self._version_sanity_check(self._version()) | ||
return self.document | ||
|
||
def _write(self): | ||
|
@@ -166,8 +169,21 @@ def _write(self): | |
if not self.exists(): | ||
open(self.filepath, "a+").close() | ||
|
||
current_version = self._version() if self._version() else SAM_CONFIG_VERSION | ||
try: | ||
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. Just curious: any reason why we are not checking for key to be present and add only if it isn't? 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. 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. we do. |
||
self.document.add(VERSION_KEY, current_version) | ||
except tomlkit.exceptions.KeyAlreadyPresent: | ||
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. We check it here. |
||
# NOTE(TheSriram): Do not attempt to re-write an existing version | ||
pass | ||
self.filepath.write_text(tomlkit.dumps(self.document)) | ||
|
||
def _version(self): | ||
return self.document.get(VERSION_KEY, None) | ||
|
||
def _version_sanity_check(self, version): | ||
if not isinstance(version, float): | ||
raise SamConfigVersionException(f"'{VERSION_KEY}' key is not present or is in unrecognized format. ") | ||
|
||
@staticmethod | ||
def _to_key(cmd_names): | ||
# construct a parsed name that is of the format: a_b_c_d | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Constants and helper functions for samconfig.toml's versioning. | ||
""" | ||
|
||
SAM_CONFIG_VERSION = 0.1 | ||
VERSION_KEY = "version" |
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.
Why change Exception type? What is difference between SamConfigVersionException and ConfigException?
Also probably include in exception message that this is due to Invalid Version instead of Invalid Syntax.
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.
One is a click exception, and the other isnt. str(ex) gives us version based information.