Skip to content

Commit

Permalink
Merge pull request #298 from AsherGlick/removing_jsonschema
Browse files Browse the repository at this point in the history
Removing the old json schema based validator
  • Loading branch information
AsherGlick authored May 26, 2024
2 parents 4813721 + f3cd838 commit d5f02a2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 321 deletions.
4 changes: 2 additions & 2 deletions xml_converter/generators/generate_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def generate_cpp_variable_data(
cpp_includes.cpp_absolute_includes.add("type_traits")

for filepath, document in sorted(data.items()):
fieldval = document.metadata_dataclass
fieldval = document.metadata
attribute_name: str = attribute_name_from_markdown_data(fieldval.name)

if doc_type in fieldval.applies_to_as_str():
Expand Down Expand Up @@ -467,7 +467,7 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> List[st
for filepath in attribute_names:
attribute_components: List[AttributeComponent] = []
xml_bundled_components: List[str] = []
attribute_data: MetadataType = data[filepath].metadata_dataclass
attribute_data: MetadataType = data[filepath].metadata
attribute_name = attribute_name_from_markdown_data(attribute_data.name)

proto_field_type: str = ""
Expand Down
103 changes: 5 additions & 98 deletions xml_converter/generators/main.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,15 @@
from jsonschema import validate # type:ignore
from jsonschema.exceptions import ValidationError # type:ignore
import frontmatter # type:ignore
from typing import Any, Dict, List, Tuple, Final, Set
from typing import Any, Dict, List, Tuple, Set
import os
import markdown
from dataclasses import dataclass
from jinja2 import FileSystemLoader, Environment
from schema import string_t, array_t, enum_t, union_t, union_partial_t, pattern_dictionary_t, object_t, boolean_t, DefType
from protobuf_types import get_proto_field_type
from util import capitalize, Document
from generate_cpp import write_cpp_classes, write_attribute
import argparse
from metadata import parse_data, MetadataType

XML_ATTRIBUTE_REGEX: Final[str] = "^[A-Za-z]+$"
PROTO_FIELD_REGEX: Final[str] = "^[a-z_.]+$"
INTERNAL_VARIABLE_REGEX: Final[str] = "^[a-z_]+$"
ATTRIBUTE_NAME_REGEX: Final[str] = "^[A-Za-z ]+$"
CUSTOM_SERIALIZER_REGEX: Final[str] = r"^(?:read|write)(?:\.xml|\.proto)(?:\.icon|\.trail)?$"

shared_required_fields: Dict[str, DefType] = {
"type": string_t(),
"name": string_t(pattern=ATTRIBUTE_NAME_REGEX),
"applies_to": array_t(enum_t(["Icon", "Trail", "Category"])),
"xml_fields": array_t(string_t(pattern=XML_ATTRIBUTE_REGEX)),
"protobuf_field": string_t(pattern=PROTO_FIELD_REGEX),
}

shared_optional_fields: Dict[str, DefType] = {
"custom_functions": pattern_dictionary_t({CUSTOM_SERIALIZER_REGEX: object_t({
"function": string_t(),
"side_effects": array_t(string_t()),
})}),
"examples": array_t(string_t())
}

schema = union_t({
"Int32": union_partial_t(required=shared_required_fields, optional=shared_optional_fields),
"Fixed32": union_partial_t(required=shared_required_fields, optional=shared_optional_fields),
"Float32": union_partial_t(required=shared_required_fields, optional=shared_optional_fields),
"String": union_partial_t(required=shared_required_fields, optional=shared_optional_fields),
"Boolean": union_partial_t(required=shared_required_fields, optional=shared_optional_fields),
"MultiflagValue": union_partial_t(
required={**shared_required_fields, **{
"flags": pattern_dictionary_t({INTERNAL_VARIABLE_REGEX: array_t(string_t())}),
}},
optional=shared_optional_fields,
),
"Enum": union_partial_t(
required={**shared_required_fields, **{
"values": pattern_dictionary_t({INTERNAL_VARIABLE_REGEX: array_t(string_t())})
}},
optional=shared_optional_fields,
),
"CompoundValue": union_partial_t(
required={**shared_required_fields, **{
"xml_bundled_components": array_t(string_t()),
"xml_separate_components": array_t(string_t()),
"components": array_t(object_t({
"name": string_t(),
"type": enum_t(["Int32", "Fixed32", "Float32"]),
"xml_fields": array_t(string_t(XML_ATTRIBUTE_REGEX)),
"protobuf_field": string_t(PROTO_FIELD_REGEX),
})),
}},
optional=shared_optional_fields,
),
"CompoundCustomClass": union_partial_t(
required={**shared_required_fields, **{
"class": string_t(),
"xml_bundled_components": array_t(string_t()),
"xml_separate_components": array_t(string_t()),
"components": array_t(object_t({
"name": string_t(),
"type": enum_t(["Int32", "Fixed32", "Float32"]),
"xml_fields": array_t(string_t(XML_ATTRIBUTE_REGEX)),
"protobuf_field": string_t(PROTO_FIELD_REGEX),
})),
}},
optional=shared_optional_fields,
),
"Custom": union_partial_t(
required={**shared_required_fields, **{"class": string_t()}},
optional={
**shared_optional_fields,
"uses_file_path": boolean_t(), # This will eventually be part of a struct that is passed into everything
}
),
})


def validate_front_matter_schema(front_matter: Any) -> str:
try:
validate(front_matter, schema)
except ValidationError as e:
return "Error Message: {} (Path: {}".format(e.message, e.json_path)
return ""


@dataclass
class FieldRow:
Expand Down Expand Up @@ -136,16 +49,10 @@ def load_input_doc(self, dir_path: str) -> None:
print(filepath)
raise e

metadata_dataclass = parse_data(document.metadata)

error = validate_front_matter_schema(document.metadata)
if error != "":
print(filepath)
print(error)
metadata = parse_data(document.metadata)

self.data[filepath] = Document(
metadata=document.metadata,
metadata_dataclass=metadata_dataclass,
metadata=metadata,
content=document.content
)

Expand Down Expand Up @@ -181,7 +88,7 @@ def write_webdocs(self, output_directory: str) -> None:

for subpage in categories[page]:
content[subpage] = markdown.markdown(self.data[subpage].content, extensions=['extra', 'codehilite'])
metadata[subpage] = self.data[subpage].metadata_dataclass
metadata[subpage] = self.data[subpage].metadata

generated_doc, field_rows = self.generate_auto_docs(metadata, content)

Expand All @@ -196,7 +103,7 @@ def write_webdocs(self, output_directory: str) -> None:
))

def get_examples(self, field_type: str, field_key: str) -> List[str]:
field_examples = self.data[field_key].metadata_dataclass.examples
field_examples = self.data[field_key].metadata.examples
if len(field_examples) > 0:
return [f'"{x}"' for x in field_examples]

Expand Down
7 changes: 6 additions & 1 deletion xml_converter/generators/metadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from dataclasses import dataclass, field
import enum
from typing import Dict, List, Literal, Optional, Any, Union
from typing import Dict, List, Literal, Optional, Any, Union, Final
import classnotation

XML_ATTRIBUTE_REGEX: Final[str] = "^[A-Za-z]+$"
PROTO_FIELD_REGEX: Final[str] = "^[a-z_.]+$"
INTERNAL_VARIABLE_REGEX: Final[str] = "^[a-z_]+$"
ATTRIBUTE_NAME_REGEX: Final[str] = "^[A-Za-z ]+$"


class NodeType(enum.Enum):
icon = "Icon"
Expand Down
202 changes: 0 additions & 202 deletions xml_converter/generators/schema.py

This file was deleted.

7 changes: 1 addition & 6 deletions xml_converter/generators/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from typing import Dict, Any
from metadata import MetadataType


Expand Down Expand Up @@ -57,11 +56,7 @@ def normalize(word: str) -> str:
return "".join(normalized_word_array)


SchemaType = Dict[str, Any]


@dataclass
class Document:
metadata: SchemaType
metadata_dataclass: MetadataType
metadata: MetadataType
content: str
Loading

0 comments on commit d5f02a2

Please sign in to comment.