Skip to content

Commit

Permalink
Load cross-content and bring sdk, services definitions into validator
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Jan 11, 2024
1 parent 546da23 commit 6f5061a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
34 changes: 22 additions & 12 deletions .tools/validation/doc_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# from os import glob

from metadata import Example, parse as parse_examples
from metadata_errors import MetadataErrors
from metadata_errors import MetadataErrors, MissingCrossContent
from metadata_validator import validate_metadata
from project_validator import check_files, verify_sample_files
from sdks import Sdk, parse as parse_sdks
Expand All @@ -20,13 +20,14 @@

@dataclass
class DocGen:
root_path: Path
root: Path
errors: MetadataErrors
sdks: dict[str, Sdk] = field(default_factory=dict)
services: dict[str, Service] = field(default_factory=dict)
snippets: dict[str, Snippet] = field(default_factory=dict)
snippet_files: set[str] = field(default_factory=set)
examples: list[Example] = field(default_factory=list)
cross_blocks: set[str] = field(default_factory=set)

def collect_snippets(self, snippets_root: Path | None):
if snippets_root is None:
Expand All @@ -38,43 +39,52 @@ def collect_snippets(self, snippets_root: Path | None):
@classmethod
def from_root(cls, root: Path) -> Self:
errors = MetadataErrors()
metadata = root / ".doc_gen/metadata"

with open(metadata / "sdks.yaml", encoding="utf-8") as file:
with open(Path(__file__).parent / "sdks.yaml", encoding="utf-8") as file:
meta = yaml.safe_load(file)
sdks, errs = parse_sdks("sdks.yaml", meta)
errors.extend(errs)

with open(metadata / "services.yaml", encoding="utf-8") as file:
with open(Path(__file__).parent / "services.yaml", encoding="utf-8") as file:
meta = yaml.safe_load(file)
services, service_errors = parse_services("services.yaml", meta)
errors.extend(service_errors)

metadata = root / ".doc_gen/metadata"
cross = set(
[path.name for path in (metadata.parent / "cross-content").glob("*.xml")]
)

doc_gen = cls(
root=root,
sdks=sdks,
services=services,
errors=errors,
root_path=root,
cross_blocks=cross,
)

for path in metadata.glob("*_metadata.yaml"):
for path in metadata.glob("cross_metadata.yaml"):
with open(path) as file:
ex, errs = parse_examples(
path.name, yaml.safe_load(file), doc_gen.sdks, doc_gen.services
path.name,
yaml.safe_load(file),
doc_gen.sdks,
doc_gen.services,
doc_gen.cross_blocks,
)
doc_gen.examples.extend(ex)
errors.extend(errs)

return doc_gen

def validate(self):
check_files(self.root_path, self.errors)
verify_sample_files(self.root_path, self.errors)
validate_metadata(self.root_path, self.errors)
check_files(self.root, self.errors)
verify_sample_files(self.root, self.errors)
validate_metadata(self.root, self.errors)
validate_snippets(
self.examples,
self.snippets,
self.snippet_files,
self.errors,
self.root_path,
self.root,
)
35 changes: 27 additions & 8 deletions .tools/validation/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class Version:

@classmethod
def from_yaml(
cls, yaml: dict[str, Any], services: dict[str, Service]
cls,
yaml: dict[str, Any],
services: dict[str, Service],
cross_content_blocks: set[str],
) -> tuple[Self, MetadataErrors]:
errors = MetadataErrors()

Expand Down Expand Up @@ -112,6 +115,9 @@ def from_yaml(
if add_services and block_content is not None:
errors.append(metadata_errors.APIExampleCannotAddService())

if block_content is not None and block_content not in cross_content_blocks:
errors.append(metadata_errors.MissingCrossContent(block=block_content))

return (
cls(
sdk_version,
Expand All @@ -133,7 +139,12 @@ class Language:

@classmethod
def from_yaml(
cls, name: str, yaml: Any, sdks: dict[str, Sdk], services: dict[str, Service]
cls,
name: str,
yaml: Any,
sdks: dict[str, Sdk],
services: dict[str, Service],
blocks: set[str],
) -> tuple[Self, MetadataErrors]:
errors = MetadataErrors()
if name not in sdks:
Expand All @@ -146,7 +157,7 @@ def from_yaml(

versions: list[Version] = []
for version in yaml_versions:
version, version_errors = Version.from_yaml(version, services)
version, version_errors = Version.from_yaml(version, services, blocks)
errors.extend(version_errors)
versions.append(version)

Expand Down Expand Up @@ -180,7 +191,11 @@ class Example:

@classmethod
def from_yaml(
cls, yaml: Any, sdks: dict[str, Sdk], services: dict[str, Service]
cls,
yaml: Any,
sdks: dict[str, Sdk],
services: dict[str, Service],
blocks: set[str],
) -> tuple[Self, MetadataErrors]:
errors = MetadataErrors()

Expand All @@ -205,7 +220,7 @@ def from_yaml(
else:
for name in yaml_languages:
language, errs = Language.from_yaml(
name, yaml_languages[name], sdks, services
name, yaml_languages[name], sdks, services, blocks
)
languages[language.name] = language
errors.extend(errs)
Expand Down Expand Up @@ -286,14 +301,18 @@ def idFormat(id: str, services: dict[str, Service]) -> bool:


def parse(
file: str, yaml: dict[str, Any], sdks: dict[str, Sdk], services: dict[str, Service]
file: str,
yaml: dict[str, Any],
sdks: dict[str, Sdk],
services: dict[str, Service],
blocks: set[str],
) -> tuple[list[Example], MetadataErrors]:
examples: list[Example] = []
errors = MetadataErrors()
for id in yaml:
if not idFormat(id, services):
errors.append(metadata_errors.NameFormat(file=file, id=id))
example, example_errors = Example.from_yaml(yaml[id], sdks, services)
example, example_errors = Example.from_yaml(yaml[id], sdks, services, blocks)
for error in example_errors:
error.file = file
error.id = id
Expand All @@ -317,7 +336,7 @@ def main():
)
with open(path) as file:
meta = yaml.safe_load(file)
(examples, errors) = parse(path.name, meta, {}, {})
(examples, errors) = parse(path.name, meta, {}, {}, set())
if len(errors) > 0:
print(f"{errors}")
else:
Expand Down
8 changes: 8 additions & 0 deletions .tools/validation/metadata_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def message(self):
return "name does not match the required format of 'svc_Operation', 'svc_Operation_Specialization', or 'cross_Title'"


@dataclass
class MissingCrossContent(MetadataParseError):
block: str = ""

def message(self):
return f"missing cross content block {self.block}"


@dataclass
class FieldError(MetadataParseError):
field: str = ""
Expand Down
6 changes: 2 additions & 4 deletions .tools/validation/metadata_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ def validate_files(


def validate_metadata(doc_gen_root: Path, errors: MetadataErrors) -> MetadataErrors:
with open(doc_gen_root / ".doc_gen" / "metadata" / "sdks.yaml") as sdks_file:
with open(Path(__file__).parent / "sdks.yaml") as sdks_file:
sdks_yaml: dict[str, Any] = yaml.safe_load(sdks_file)

with open(
doc_gen_root / ".doc_gen" / "metadata" / "services.yaml"
) as services_file:
with open(Path(__file__).parent / "services.yaml") as services_file:
services_yaml = yaml.safe_load(services_file)

SdkVersion.sdks = sdks_yaml
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 6f5061a

Please sign in to comment.