Skip to content

Commit 3dac7b2

Browse files
Fix linting
1 parent 4ce9104 commit 3dac7b2

File tree

16 files changed

+182
-181
lines changed

16 files changed

+182
-181
lines changed

meta/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"""
66
__all__ = ['generate_tag_data', 'build_tags']
77

8-
from .scrape_tags import main as generate_tag_data
98
from .generate_tag_defs import main as build_tags
9+
from .scrape_tags import main as generate_tag_data

meta/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
21
from pathlib import Path
3-
from . import build_tags
42

3+
from . import build_tags
54

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

87

98
def main():
10-
build_tags(open(OUTPUT_FILE, "w"))
9+
with open(OUTPUT_FILE, "w") as f:
10+
build_tags(f)
1111

1212

13-
if __name__ == '__main__':
13+
if __name__ == "__main__":
1414
main()

meta/generate_tag_defs.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
44
Code for generating tag definitions
55
"""
6-
from pathlib import Path
6+
77
import sys
8+
from pathlib import Path
89
from typing import TextIO
9-
from .scrape_tags import main as generate_tag_data, TagInfo
10+
1011
from pyhtml.__util import increase_indent
1112

13+
from .scrape_tags import TagInfo
14+
from .scrape_tags import main as generate_tag_data
1215

13-
TEMPLATES_FOLDER = Path('./meta/templates')
16+
TEMPLATES_FOLDER = Path("./meta/templates")
1417

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

4346
# Generate default attributes dictionary
44-
default_attrs = repr({
45-
attr.name: attr.default
46-
for attr in tag.attributes
47-
})
47+
default_attrs = repr({attr.name: attr.default for attr in tag.attributes})
4848

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

71-
attr_args = '\n'.join(attr_args_gen).strip()
72-
attr_unions = '\n'.join(attr_unions_gen).strip()
73-
attr_docs_outer = '\n'.join(increase_indent(attr_docs_gen, 4)).strip()
74-
attr_docs_inner = '\n'.join(increase_indent(attr_docs_gen, 8)).strip()
71+
attr_args = "\n".join(attr_args_gen).strip()
72+
attr_unions = "\n".join(attr_unions_gen).strip()
73+
attr_docs_outer = "\n".join(increase_indent(attr_docs_gen, 4)).strip()
74+
attr_docs_inner = "\n".join(increase_indent(attr_docs_gen, 8)).strip()
7575

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

8582
# Now we just need to replace in all of the templated attributes
86-
text = text\
87-
.replace("{name}", tag.name)\
88-
.replace("{base}", tag.base)\
89-
.replace("{description}", tag.description)\
90-
.replace("{link}", tag.mdn_link)\
91-
.replace("{attr_args}", attr_args)\
92-
.replace("{attr_unions}", attr_unions)\
93-
.replace("{attr_docs_outer}", attr_docs_outer)\
94-
.replace("{attr_docs_inner}", attr_docs_inner)\
95-
.replace("{kw_only}", kw_only)\
83+
text = (
84+
text.replace("{name}", tag.name)
85+
.replace("{base}", tag.base)
86+
.replace("{description}", tag.description)
87+
.replace("{link}", tag.mdn_link)
88+
.replace("{attr_args}", attr_args)
89+
.replace("{attr_unions}", attr_unions)
90+
.replace("{attr_docs_outer}", attr_docs_outer)
91+
.replace("{attr_docs_inner}", attr_docs_inner)
92+
.replace("{kw_only}", kw_only)
9693
.replace("{default_attrs}", default_attrs)
94+
)
9795

9896
print(text, file=output)
9997

@@ -118,7 +116,7 @@ def main(output: TextIO):
118116
"""
119117
tags = generate_tag_data()
120118

121-
with open(TEMPLATES_FOLDER.joinpath('main.py')) as f:
119+
with open(TEMPLATES_FOLDER.joinpath("main.py")) as f:
122120
print(f.read(), file=output)
123121

124122
for tag in tags:
@@ -164,5 +162,5 @@ def main(output: TextIO):
164162
print(")")
165163

166164

167-
if __name__ == '__main__':
165+
if __name__ == "__main__":
168166
main(output=sys.stdout)

meta/scrape_tags.py

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
77
It also embeds suggested kwargs for some elements, using info from tags.yml.
88
"""
9-
from dataclasses import dataclass
10-
from typing import Optional, TypedDict, Any, Union
11-
from typing_extensions import NotRequired
9+
10+
import sys
1211
from collections.abc import Iterator
12+
from dataclasses import dataclass
13+
from typing import Any, Optional, TypedDict, Union
14+
1315
import requests
1416
import yaml
15-
import sys
16-
17+
from typing_extensions import NotRequired
1718

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

9091
DESCRIPTION_LOOKUPS = {
9192
"htmlelement": htmlElementReplace,
92-
"glossary": glossaryReplace,
93+
"glossary": glossaryReplace,
9394
"cssxref": cssXrefReplace,
9495
"domxref": domXrefReplace,
9596
}
@@ -121,6 +122,7 @@ class TagsYmlItem(TypedDict):
121122
"""
122123
A tag which has suggested keys
123124
"""
125+
124126
skip: NotRequired[bool]
125127
"""Whether to skip this tag when generating tags"""
126128

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

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

238240
return tags
239241

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

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

260262
key, args = element_text.split("(")
@@ -283,7 +285,7 @@ def format_description(description: str, ele: str) -> str:
283285
description = (
284286
description[:start]
285287
+ DESCRIPTION_LOOKUPS[key.lower()](lookup, presentation)
286-
+ description[end+2:]
288+
+ description[end + 2 :]
287289
)
288290

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

297299
# Read in header row
298-
assert next(lines).startswith('| ---')
300+
assert next(lines).startswith("| ---")
299301

300302
# Now grab each line
301303
tags: list[TagMdnInfo] = []
302304
try:
303-
while (line := next(lines)).startswith('|'):
304-
_, tag_base, description, _ = line.split('|')
305+
while (line := next(lines)).startswith("|"):
306+
_, tag_base, description, _ = line.split("|")
305307

306308
tag_base = tag_base.strip()
307309

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

313-
tag_name = tag_base\
314-
.removeprefix('{{HTMLElement("')\
315-
.removesuffix('")}}')
315+
tag_name = tag_base.removeprefix('{{HTMLElement("').removesuffix(
316+
'")}}'
317+
)
316318

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

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

353-
if line.replace(' ', '') == '|Element|Description|':
355+
if line.replace(" ", "") == "|Element|Description|":
354356
# Start of table
355357
tags.extend(parse_markdown_table(lines))
356358

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

391393
tag_data = tags[tag_name]
392394

393-
if 'attributes' not in tag_data:
395+
if "attributes" not in tag_data:
394396
return []
395397

396398
attrs = []
397-
for name, value in tag_data['attributes'].items():
399+
for name, value in tag_data["attributes"].items():
398400
if isinstance(value, str):
399401
doc: Optional[str] = value
400402
default: Optional[str] = None
401403
type = "AttributeType"
402404
else:
403405
doc = value.get("doc")
404-
if "default" in value:
405-
# NOTE: This is safe, as it is only ever run at compile time
406-
# This code is not distributed to users' systems
407-
default = eval(value["default"])
408-
else:
409-
default = None
406+
# NOTE: This is safe, as it is only ever run at compile time
407+
# This code is not distributed to users' systems
408+
default = eval(value["default"]) if "default" in value else None
410409
type = value.get("type", "AttributeType")
411410
attrs.append(Attr(name, doc, type, default))
412411
return attrs
@@ -423,7 +422,7 @@ def get_tag_rename(tags: TagsYaml, tag_name: str) -> str:
423422
if "rename" not in tag:
424423
return tag_name
425424
else:
426-
return tag['rename']
425+
return tag["rename"]
427426

428427

429428
def get_tag_base_class(tags: TagsYaml, tag_name: str) -> str:
@@ -436,7 +435,7 @@ def get_tag_base_class(tags: TagsYaml, tag_name: str) -> str:
436435
if "base" not in tag:
437436
return "Tag"
438437
else:
439-
return tag['base']
438+
return tag["base"]
440439

441440

442441
def get_tag_skip(tags: TagsYaml, tag_name: str) -> bool:
@@ -446,7 +445,7 @@ def get_tag_skip(tags: TagsYaml, tag_name: str) -> bool:
446445
if tag_name not in tags:
447446
return False
448447
tag = tags[tag_name]
449-
return tag.get('skip', False)
448+
return tag.get("skip", False)
450449

451450

452451
def get_tag_escape_children(tags: TagsYaml, tag_name: str) -> bool:
@@ -456,7 +455,7 @@ def get_tag_escape_children(tags: TagsYaml, tag_name: str) -> bool:
456455
if tag_name not in tags:
457456
return True
458457
tag = tags[tag_name]
459-
return tag.get('escape_children', True)
458+
return tag.get("escape_children", True)
460459

461460

462461
def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
@@ -466,7 +465,7 @@ def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
466465
if tag_name not in tags:
467466
return None
468467
tag = tags[tag_name]
469-
return tag.get('pre_content', None)
468+
return tag.get("pre_content", None)
470469

471470

472471
def make_mdn_link(tag: str) -> str:
@@ -488,15 +487,17 @@ def elements_to_element_structs(
488487
if get_tag_skip(tag_attrs, name):
489488
continue
490489

491-
output.append(TagInfo(
492-
name=get_tag_rename(tag_attrs, name),
493-
description=description,
494-
base=get_tag_base_class(tag_attrs, name),
495-
mdn_link=make_mdn_link(name),
496-
escape_children=get_tag_escape_children(tag_attrs, name),
497-
attributes=attr_entries_to_object(tag_attrs, name),
498-
pre_content=get_tag_pre_content(tag_attrs, name)
499-
))
490+
output.append(
491+
TagInfo(
492+
name=get_tag_rename(tag_attrs, name),
493+
description=description,
494+
base=get_tag_base_class(tag_attrs, name),
495+
mdn_link=make_mdn_link(name),
496+
escape_children=get_tag_escape_children(tag_attrs, name),
497+
attributes=attr_entries_to_object(tag_attrs, name),
498+
pre_content=get_tag_pre_content(tag_attrs, name),
499+
)
500+
)
500501

501502
return output
502503

@@ -523,9 +524,9 @@ def print_elements(parsed: list[TagInfo]):
523524
print()
524525
print(ele.attributes)
525526
print()
526-
print('------------------')
527+
print("------------------")
527528
print()
528529

529530

530-
if __name__ == '__main__':
531+
if __name__ == "__main__":
531532
print_elements(main())

pyhtml/__tag_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
Tag base class, including rendering logic
55
"""
66
from typing import Optional, TypeVar
7-
from . import __util as util
8-
from .__types import ChildrenType, AttributeType
97

8+
from . import __util as util
9+
from .__types import AttributeType, ChildrenType
1010

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

0 commit comments

Comments
 (0)