Skip to content
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

yaml literals #70

Merged
merged 26 commits into from
Dec 3, 2022
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
as per Ned's suggestion
fkuep committed Nov 29, 2022
commit 1d0719bf0778bf59c4abe7959279632b73565d44
22 changes: 3 additions & 19 deletions src/scriv/literals.py
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ def find_literal(file_name: str, literal_name: str) -> Optional[str]:
raise Exception(msg)
with open(file_name, encoding="utf-8") as f:
data = tomli.loads(f.read())
return find_toml_value(data, literal_name)
return find_nested_value(data, literal_name)
elif ext == ".yml" or ext == ".yaml":
if yaml is None:
msg = (
@@ -48,7 +48,7 @@ def find_literal(file_name: str, literal_name: str) -> Optional[str]:
raise Exception(msg)
with open(file_name, encoding="utf-8") as f:
data = yaml.safe_load(f)
return find_yaml_value(data, literal_name)
return find_nested_value(data, literal_name)
else:
raise Exception(f"Can't read literals from files like {file_name!r}")

@@ -96,7 +96,7 @@ def check_value(self, value):
self.value = value.s


def find_toml_value(data: MutableMapping[str, Any], name: str) -> Optional[str]:
def find_nested_value(data: MutableMapping[str, Any], name: str) -> Optional[str]:
"""
Use a period-separated name to traverse a dictionary.

@@ -113,19 +113,3 @@ def find_toml_value(data: MutableMapping[str, Any], name: str) -> Optional[str]:
return current_object
return None

def find_yaml_value(data: MutableMapping[str, Any], name: str) -> Optional[str]:
"""
Use a period-separated name to traverse a dictionary.

Only string values are supported.
"""
current_object = data
for key in name.split("."):
try:
current_object = current_object[key]
except (KeyError, TypeError):
return None

if isinstance(current_object, str):
return current_object
return None