Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Feb 5, 2025
1 parent 4ce9104 commit 3dac7b2
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 181 deletions.
2 changes: 1 addition & 1 deletion meta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"""
__all__ = ['generate_tag_data', 'build_tags']

from .scrape_tags import main as generate_tag_data
from .generate_tag_defs import main as build_tags
from .scrape_tags import main as generate_tag_data
8 changes: 4 additions & 4 deletions meta/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

from pathlib import Path
from . import build_tags

from . import build_tags

OUTPUT_FILE = Path("pyhtml/__tags/generated.py")


def main():
build_tags(open(OUTPUT_FILE, "w"))
with open(OUTPUT_FILE, "w") as f:
build_tags(f)


if __name__ == '__main__':
if __name__ == "__main__":
main()
52 changes: 25 additions & 27 deletions meta/generate_tag_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
Code for generating tag definitions
"""
from pathlib import Path

import sys
from pathlib import Path
from typing import TextIO
from .scrape_tags import main as generate_tag_data, TagInfo

from pyhtml.__util import increase_indent

from .scrape_tags import TagInfo
from .scrape_tags import main as generate_tag_data

TEMPLATES_FOLDER = Path('./meta/templates')
TEMPLATES_FOLDER = Path("./meta/templates")

NO_ESCAPE_CHILDREN = """
def _escape_children(self) -> bool:
Expand Down Expand Up @@ -41,10 +44,7 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
text = get_template_class(tag.base)

# Generate default attributes dictionary
default_attrs = repr({
attr.name: attr.default
for attr in tag.attributes
})
default_attrs = repr({attr.name: attr.default for attr in tag.attributes})

# Generate attribute arguments, unions and documentation
# To get a better idea of these, look inside the template files to see
Expand All @@ -68,32 +68,30 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
else:
attr_docs_gen.append(f"* `{attr.name}`: {attr.doc}")

attr_args = '\n'.join(attr_args_gen).strip()
attr_unions = '\n'.join(attr_unions_gen).strip()
attr_docs_outer = '\n'.join(increase_indent(attr_docs_gen, 4)).strip()
attr_docs_inner = '\n'.join(increase_indent(attr_docs_gen, 8)).strip()
attr_args = "\n".join(attr_args_gen).strip()
attr_unions = "\n".join(attr_unions_gen).strip()
attr_docs_outer = "\n".join(increase_indent(attr_docs_gen, 4)).strip()
attr_docs_inner = "\n".join(increase_indent(attr_docs_gen, 8)).strip()

# Determine whether the class should mandate keyword-only args
# If there are no named attributes, we set it to '' to avoid a syntax error
# Otherwise, we add a `*,` to prevent kwargs from being used as positional
# args in self-closing tags
if len(tag.attributes):
kw_only = '*,'
else:
kw_only = ''
kw_only = "*," if len(tag.attributes) else ""

# Now we just need to replace in all of the templated attributes
text = text\
.replace("{name}", tag.name)\
.replace("{base}", tag.base)\
.replace("{description}", tag.description)\
.replace("{link}", tag.mdn_link)\
.replace("{attr_args}", attr_args)\
.replace("{attr_unions}", attr_unions)\
.replace("{attr_docs_outer}", attr_docs_outer)\
.replace("{attr_docs_inner}", attr_docs_inner)\
.replace("{kw_only}", kw_only)\
text = (
text.replace("{name}", tag.name)
.replace("{base}", tag.base)
.replace("{description}", tag.description)
.replace("{link}", tag.mdn_link)
.replace("{attr_args}", attr_args)
.replace("{attr_unions}", attr_unions)
.replace("{attr_docs_outer}", attr_docs_outer)
.replace("{attr_docs_inner}", attr_docs_inner)
.replace("{kw_only}", kw_only)
.replace("{default_attrs}", default_attrs)
)

print(text, file=output)

Expand All @@ -118,7 +116,7 @@ def main(output: TextIO):
"""
tags = generate_tag_data()

with open(TEMPLATES_FOLDER.joinpath('main.py')) as f:
with open(TEMPLATES_FOLDER.joinpath("main.py")) as f:
print(f.read(), file=output)

for tag in tags:
Expand Down Expand Up @@ -164,5 +162,5 @@ def main(output: TextIO):
print(")")


if __name__ == '__main__':
if __name__ == "__main__":
main(output=sys.stdout)
81 changes: 41 additions & 40 deletions meta/scrape_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
It also embeds suggested kwargs for some elements, using info from tags.yml.
"""
from dataclasses import dataclass
from typing import Optional, TypedDict, Any, Union
from typing_extensions import NotRequired

import sys
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any, Optional, TypedDict, Union

import requests
import yaml
import sys

from typing_extensions import NotRequired

TAGS_YAML = "meta/tags.yml"
"""File location to load custom tag data from"""
Expand Down Expand Up @@ -89,7 +90,7 @@ def domXrefReplace(lookup: str, presentation: Optional[str] = None) -> str:

DESCRIPTION_LOOKUPS = {
"htmlelement": htmlElementReplace,
"glossary": glossaryReplace,
"glossary": glossaryReplace,
"cssxref": cssXrefReplace,
"domxref": domXrefReplace,
}
Expand Down Expand Up @@ -121,6 +122,7 @@ class TagsYmlItem(TypedDict):
"""
A tag which has suggested keys
"""

skip: NotRequired[bool]
"""Whether to skip this tag when generating tags"""

Expand Down Expand Up @@ -233,7 +235,7 @@ def handle_header_elements(description) -> list[TagMdnInfo]:
tags = []

for i in range(6):
tags.append((f"h{i+1}", description))
tags.append((f"h{i + 1}", description))

return tags

Expand All @@ -254,7 +256,7 @@ def format_description(description: str, ele: str) -> str:
while (start := description.find("{{")) != -1:
end = description.find("}}", start)

element_text = description[start+2:end]
element_text = description[start + 2 : end]
# In format key("arg1", "arg2")

key, args = element_text.split("(")
Expand Down Expand Up @@ -283,7 +285,7 @@ def format_description(description: str, ele: str) -> str:
description = (
description[:start]
+ DESCRIPTION_LOOKUPS[key.lower()](lookup, presentation)
+ description[end+2:]
+ description[end + 2 :]
)

return description
Expand All @@ -295,13 +297,13 @@ def parse_markdown_table(lines: Iterator[str]) -> list[TagMdnInfo]:
"""

# Read in header row
assert next(lines).startswith('| ---')
assert next(lines).startswith("| ---")

# Now grab each line
tags: list[TagMdnInfo] = []
try:
while (line := next(lines)).startswith('|'):
_, tag_base, description, _ = line.split('|')
while (line := next(lines)).startswith("|"):
_, tag_base, description, _ = line.split("|")

tag_base = tag_base.strip()

Expand All @@ -310,9 +312,9 @@ def parse_markdown_table(lines: Iterator[str]) -> list[TagMdnInfo]:
tags.extend(handle_header_elements(description))
continue

tag_name = tag_base\
.removeprefix('{{HTMLElement("')\
.removesuffix('")}}')
tag_name = tag_base.removeprefix('{{HTMLElement("').removesuffix(
'")}}'
)

description = format_description(description.strip(), tag_name)

Expand Down Expand Up @@ -350,7 +352,7 @@ def parse_markdown(lines: Iterator[str]) -> list[TagMdnInfo]:
print("Skip obsolete tags", file=sys.stderr)
break

if line.replace(' ', '') == '|Element|Description|':
if line.replace(" ", "") == "|Element|Description|":
# Start of table
tags.extend(parse_markdown_table(lines))

Expand Down Expand Up @@ -390,23 +392,20 @@ def attr_entries_to_object(tags: TagsYaml, tag_name: str) -> list[Attr]:

tag_data = tags[tag_name]

if 'attributes' not in tag_data:
if "attributes" not in tag_data:
return []

attrs = []
for name, value in tag_data['attributes'].items():
for name, value in tag_data["attributes"].items():
if isinstance(value, str):
doc: Optional[str] = value
default: Optional[str] = None
type = "AttributeType"
else:
doc = value.get("doc")
if "default" in value:
# NOTE: This is safe, as it is only ever run at compile time
# This code is not distributed to users' systems
default = eval(value["default"])
else:
default = None
# NOTE: This is safe, as it is only ever run at compile time
# This code is not distributed to users' systems
default = eval(value["default"]) if "default" in value else None
type = value.get("type", "AttributeType")
attrs.append(Attr(name, doc, type, default))
return attrs
Expand All @@ -423,7 +422,7 @@ def get_tag_rename(tags: TagsYaml, tag_name: str) -> str:
if "rename" not in tag:
return tag_name
else:
return tag['rename']
return tag["rename"]


def get_tag_base_class(tags: TagsYaml, tag_name: str) -> str:
Expand All @@ -436,7 +435,7 @@ def get_tag_base_class(tags: TagsYaml, tag_name: str) -> str:
if "base" not in tag:
return "Tag"
else:
return tag['base']
return tag["base"]


def get_tag_skip(tags: TagsYaml, tag_name: str) -> bool:
Expand All @@ -446,7 +445,7 @@ def get_tag_skip(tags: TagsYaml, tag_name: str) -> bool:
if tag_name not in tags:
return False
tag = tags[tag_name]
return tag.get('skip', False)
return tag.get("skip", False)


def get_tag_escape_children(tags: TagsYaml, tag_name: str) -> bool:
Expand All @@ -456,7 +455,7 @@ def get_tag_escape_children(tags: TagsYaml, tag_name: str) -> bool:
if tag_name not in tags:
return True
tag = tags[tag_name]
return tag.get('escape_children', True)
return tag.get("escape_children", True)


def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
Expand All @@ -466,7 +465,7 @@ def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
if tag_name not in tags:
return None
tag = tags[tag_name]
return tag.get('pre_content', None)
return tag.get("pre_content", None)


def make_mdn_link(tag: str) -> str:
Expand All @@ -488,15 +487,17 @@ def elements_to_element_structs(
if get_tag_skip(tag_attrs, name):
continue

output.append(TagInfo(
name=get_tag_rename(tag_attrs, name),
description=description,
base=get_tag_base_class(tag_attrs, name),
mdn_link=make_mdn_link(name),
escape_children=get_tag_escape_children(tag_attrs, name),
attributes=attr_entries_to_object(tag_attrs, name),
pre_content=get_tag_pre_content(tag_attrs, name)
))
output.append(
TagInfo(
name=get_tag_rename(tag_attrs, name),
description=description,
base=get_tag_base_class(tag_attrs, name),
mdn_link=make_mdn_link(name),
escape_children=get_tag_escape_children(tag_attrs, name),
attributes=attr_entries_to_object(tag_attrs, name),
pre_content=get_tag_pre_content(tag_attrs, name),
)
)

return output

Expand All @@ -523,9 +524,9 @@ def print_elements(parsed: list[TagInfo]):
print()
print(ele.attributes)
print()
print('------------------')
print("------------------")
print()


if __name__ == '__main__':
if __name__ == "__main__":
print_elements(main())
4 changes: 2 additions & 2 deletions pyhtml/__tag_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Tag base class, including rendering logic
"""
from typing import Optional, TypeVar
from . import __util as util
from .__types import ChildrenType, AttributeType

from . import __util as util
from .__types import AttributeType, ChildrenType

SelfType = TypeVar('SelfType', bound='Tag')

Expand Down
Loading

0 comments on commit 3dac7b2

Please sign in to comment.