generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 16
DocGen: add complex categories #126
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50db755
First stab at data class.
Laren-AWS fab90c2
Update basics template.
Laren-AWS 5415c30
Add good draft of complex categories.yaml.
Laren-AWS 33eef83
Update categories.yaml.
Laren-AWS 832d40a
Load and process complex categories.
Laren-AWS ecbed04
Work around Actions/Api discrepancy for categories and fix unit tests.
Laren-AWS 52ea08d
Placate typing.
Laren-AWS 7a5f119
More placating of the type checker.
Laren-AWS 3c621d8
Run black.
Laren-AWS 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import Any, Dict, List, Optional | ||
from dataclasses import dataclass, field | ||
|
||
from aws_doc_sdk_examples_tools import metadata_errors | ||
from .metadata_errors import ( | ||
MetadataErrors, | ||
) | ||
|
||
|
||
@dataclass | ||
class TitleInfo: | ||
title: Optional[str] = field(default=None) | ||
title_abbrev: Optional[str] = field(default=None) | ||
synopsis: Optional[str] = field(default=None) | ||
title_suffixes: str | Dict[str, str] = field(default_factory=dict) | ||
|
||
@classmethod | ||
def from_yaml(cls, yaml: Dict[str, str] | None) -> Optional[TitleInfo]: | ||
if yaml is None: | ||
return None | ||
|
||
title = yaml.get("title") | ||
title_suffixes: str | Dict[str, str] = yaml.get("title_suffixes", {}) | ||
title_abbrev = yaml.get("title_abbrev") | ||
synopsis = yaml.get("synopsis") | ||
|
||
return cls( | ||
title=title, | ||
title_suffixes=title_suffixes, | ||
title_abbrev=title_abbrev, | ||
synopsis=synopsis, | ||
) | ||
|
||
|
||
@dataclass | ||
class CategoryWithNoDisplayError(metadata_errors.MetadataError): | ||
def message(self): | ||
return "Category has no display value" | ||
|
||
|
||
@dataclass | ||
class Category: | ||
key: str | ||
display: str | ||
defaults: Optional[TitleInfo] = field(default=None) | ||
overrides: Optional[TitleInfo] = field(default=None) | ||
description: Optional[str] = field(default=None) | ||
|
||
def validate(self, errors: MetadataErrors): | ||
if not self.display: | ||
errors.append(CategoryWithNoDisplayError(id=self.key)) | ||
|
||
@classmethod | ||
def from_yaml( | ||
Laren-AWS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cls, key: str, yaml: Dict[str, Any] | ||
) -> tuple[Category, MetadataErrors]: | ||
errors = MetadataErrors() | ||
display = str(yaml.get("display")) | ||
defaults = TitleInfo.from_yaml(yaml.get("defaults")) | ||
overrides = TitleInfo.from_yaml(yaml.get("overrides")) | ||
description = yaml.get("description") | ||
|
||
return ( | ||
cls( | ||
key=key, | ||
display=display, | ||
defaults=defaults, | ||
overrides=overrides, | ||
description=description, | ||
), | ||
errors, | ||
) | ||
|
||
|
||
def parse( | ||
file: Path, yaml: Dict[str, Any] | ||
) -> tuple[List[str], Dict[str, Category], MetadataErrors]: | ||
categories: Dict[str, Category] = {} | ||
errors = MetadataErrors() | ||
|
||
standard_cats = yaml.get("standard_categories", []) | ||
# Work around inconsistency where some tools use 'Actions' and DocGen uses 'Api' to refer to single-action examples. | ||
for i in range(len(standard_cats)): | ||
if standard_cats[i] == "Actions": | ||
standard_cats[i] = "Api" | ||
for key, yaml_cat in yaml.get("categories", {}).items(): | ||
if yaml_cat is None: | ||
errors.append(metadata_errors.MissingCategoryBody(id=key, file=file)) | ||
else: | ||
category, cat_errs = Category.from_yaml(key, yaml_cat) | ||
categories[key] = category | ||
for error in cat_errs: | ||
error.file = file | ||
error.id = key | ||
errors.extend(cat_errs) | ||
|
||
return standard_cats, categories, errors | ||
|
||
|
||
if __name__ == "__main__": | ||
from pprint import pp | ||
import yaml | ||
|
||
path = Path(__file__).parent / "config" / "categories.yaml" | ||
with open(path) as file: | ||
meta = yaml.safe_load(file) | ||
standard_cats, cats, errs = parse(path, meta) | ||
pp(standard_cats) | ||
pp(cats) |
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,73 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from pathlib import Path | ||
from typing import Dict, List, Tuple | ||
import pytest | ||
import yaml | ||
|
||
from aws_doc_sdk_examples_tools import metadata_errors | ||
from .categories import ( | ||
parse, | ||
Category, | ||
TitleInfo, | ||
) | ||
|
||
|
||
def load( | ||
path: str, | ||
) -> Tuple[List[str], Dict[str, Category], metadata_errors.MetadataErrors]: | ||
root = Path(__file__).parent | ||
filename = root / "test_resources" / path | ||
with open(filename) as file: | ||
meta = yaml.safe_load(file) | ||
return parse(filename, meta) | ||
|
||
|
||
def test_empty_categories(): | ||
_, _, errs = load("empty_categories.yaml") | ||
assert [*errs] == [ | ||
metadata_errors.MissingCategoryBody( | ||
file=Path(__file__).parent / "test_resources/empty_categories.yaml", | ||
id="EmptyCat", | ||
) | ||
] | ||
|
||
|
||
def test_categories(): | ||
_, categories, _ = load("categories.yaml") | ||
assert categories == { | ||
"Actions": Category( | ||
key="Actions", | ||
display="Actions test", | ||
overrides=TitleInfo( | ||
title="Title override", | ||
title_suffixes={ | ||
"cli": " with a CLI", | ||
"sdk": " with an &AWS; SDK", | ||
"sdk_cli": " with an &AWS; SDK or CLI", | ||
}, | ||
title_abbrev="Title abbrev override", | ||
synopsis="synopsis test.", | ||
), | ||
description="test description.", | ||
), | ||
"Basics": Category( | ||
key="Basics", | ||
display="Basics", | ||
defaults=TitleInfo( | ||
title="Title default", | ||
title_abbrev="Title abbrev default", | ||
), | ||
description="default description.", | ||
), | ||
"TributaryLite": Category( | ||
key="TributaryLite", | ||
display="Tea light", | ||
description="light your way.", | ||
), | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__, "-vv"]) |
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,34 @@ | ||
standard_categories: ["Hello", "Basics", "Actions", "Scenarios"] | ||
categories: | ||
Hello: | ||
display: "Hello" | ||
overrides: | ||
title: "Hello {{.ServiceEntity.Short}}" | ||
title_abbrev: "Hello {{.ServiceEntity.Short}}" | ||
synopsis: "get started using {{.ServiceEntity.Short}}." | ||
Actions: | ||
display: "Actions" | ||
overrides: | ||
title: "Use <code>{{.Action}}</code>" | ||
title_suffixes: | ||
cli: " with a CLI" | ||
sdk: " with an &AWS; SDK" | ||
sdk_cli: " with an &AWS; SDK or CLI" | ||
title_abbrev: "<code>{{.Action}}</code>" | ||
synopsis: "use <code>{{.Action}}</code>." | ||
description: "are code excerpts from larger programs and must be run in context. While actions | ||
show you how to call individual service functions, you can see actions in context in their related scenarios." | ||
Basics: | ||
display: "Basics" | ||
defaults: | ||
title: "Learn the basics of {{.ServiceEntity.Short}} with an &AWS; SDK" | ||
title_abbrev: "Learn the basics" | ||
description: "are code examples that show you how to perform the essential operations within a service." | ||
Scenarios: | ||
display: "Scenarios" | ||
description: "are code examples that show you how to accomplish specific tasks by | ||
calling multiple functions within a service or combined with other &AWS-services;." | ||
TributaryLite: | ||
display: "&AWS; community contributions" | ||
description: "are examples that were created and are maintained by multiple teams across &AWS;. | ||
To provide feedback, use the mechanism provided in the linked repositories." |
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
Oops, something went wrong.
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.