Coverage for phml\utils\misc\component.py: 3%
33 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-30 09:38 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-30 09:38 -0600
1from pathlib import Path
3from phml.nodes import AST, Element
5__all__ = ["tag_from_file", "filename_from_path", "parse_component"]
8def tag_from_file(filename: str) -> str:
9 """Generates a tag name some-tag-name from a filename.
10 Assumes filenames of:
11 * snakecase - some_file_name
12 * camel case - someFileName
13 * pascal case - SomeFileName
14 """
15 from re import finditer
17 tokens = []
18 for token in finditer(r"(\b|[A-Z]|_)([a-z]+)", filename):
19 first, rest = token.groups()
20 if first.isupper():
21 rest = first + rest
22 tokens.append(rest.lower())
24 return "-".join(tokens)
27def filename_from_path(file: Path) -> str:
28 """Get the filename without the suffix from a pathlib.Path."""
30 if file.is_file():
31 return file.name.replace(file.suffix, "")
32 else:
33 raise TypeError(f"Expected {type(Path)} not {type(file)}")
36def parse_component(ast: AST) -> dict[str, Element]:
37 from phml.utils import test, visit_children
39 result = {"python": [], "script": [], "style": [], "component": None}
40 for node in visit_children(ast.tree):
41 if test(node, ["element", {"tag": "python"}]):
42 result["python"].append(node)
43 elif test(node, ["element", {"tag": "script"}]):
44 result["script"].append(node)
45 elif test(node, ["element", {"tag": "style"}]):
46 result["style"].append(node)
47 elif test(node, "element"):
48 if result["component"] is None:
49 result["component"] = node
50 else:
51 raise Exception(
52 """\
53Components may only have one wrapping element. All other element in the root must be either a script,\
54style, or python tag.\
55"""
56 )
58 if result["component"] is None:
59 raise Exception("Must have at least one element in a component.")
61 return result