entry variables tested

This commit is contained in:
Jesse Bannon 2023-12-28 10:16:03 -08:00
parent 8f8cbe6784
commit 511df8fb5b
7 changed files with 84 additions and 30 deletions

View file

@ -1,3 +1,4 @@
Entry Variables Entry Variables
=============== ===============
@ -436,4 +437,4 @@ The upload_date_index padded two digits
ytdl_sub_input_url ytdl_sub_input_url
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
The input URL used in ytdl-sub to create this entry. The input URL used in ytdl-sub to create this entry.

View file

@ -33,6 +33,15 @@ def get_file_extension(file_name: Path | str) -> str:
return file_name.rsplit(".", maxsplit=1)[-1] return file_name.rsplit(".", maxsplit=1)[-1]
def get_md5_hash(contents: str | bytes) -> str:
"""
Helper function to compute md5 hash
"""
if isinstance(contents, str):
contents = contents.encode("utf-8")
return hashlib.md5(contents).hexdigest()
def get_file_md5_hash(full_file_path: Path | str) -> str: def get_file_md5_hash(full_file_path: Path | str) -> str:
""" """
Parameters Parameters
@ -45,7 +54,7 @@ def get_file_md5_hash(full_file_path: Path | str) -> str:
md5 hash of its contents md5 hash of its contents
""" """
with open(full_file_path, "rb") as file: with open(full_file_path, "rb") as file:
return hashlib.md5(file.read()).hexdigest() return get_md5_hash(file.read())
def files_equal(full_file_path_a: Path | str, full_file_path_b: Path | str) -> bool: def files_equal(full_file_path_a: Path | str, full_file_path_b: Path | str) -> bool:

View file

View file

@ -0,0 +1,11 @@
from tools.docgen.entry_variables import EntryVariableDocGen
from ytdl_sub.utils.file_handler import get_file_md5_hash
from ytdl_sub.utils.file_handler import get_md5_hash
class TestDocGen:
def test_entry_variables_generated(self):
md5_hash = get_file_md5_hash(EntryVariableDocGen.LOCATION)
expected_md5_hash = get_md5_hash(EntryVariableDocGen.generate_and_maybe_write_to_file())
assert md5_hash == expected_md5_hash

32
tools/docgen/docgen.py Normal file
View file

@ -0,0 +1,32 @@
from abc import abstractmethod
from pathlib import Path
REGENERATE_DOCS: bool = True
class DocGen:
"""
Home-made auto doc generation
"""
LOCATION: Path
@classmethod
@abstractmethod
def generate(cls) -> str:
"""
Generate the docs as a single string
"""
@classmethod
def generate_and_maybe_write_to_file(cls) -> str:
"""
Maybe writes the docs to their file if the global is set to True, and returns
the generated docs
"""
contents = cls.generate()
if REGENERATE_DOCS:
with open(cls.LOCATION, "w", encoding="utf-8") as out:
out.write(contents)
return contents

View file

@ -1,7 +1,9 @@
from pathlib import Path
from typing import Any from typing import Any
from typing import Dict from typing import Dict
from typing import Type from typing import Type
from tools.docgen.docgen import DocGen
from tools.docgen.utils import camel_case_to_human from tools.docgen.utils import camel_case_to_human
from tools.docgen.utils import get_function_docs from tools.docgen.utils import get_function_docs
from tools.docgen.utils import properties from tools.docgen.utils import properties
@ -9,33 +11,37 @@ from tools.docgen.utils import section
from ytdl_sub.entries.script.variable_definitions import VariableDefinitions from ytdl_sub.entries.script.variable_definitions import VariableDefinitions
def variable_class_to_name(obj: Type[Any]) -> str: class EntryVariableDocGen(DocGen):
assert "VariableDefinitions" in obj.__name__, f"{obj.__name__} doesnt have VariableDefinitions"
return (
camel_case_to_human(obj.__name__)
.replace("Variable Definitions", "Variables")
.replace("Ytdl Sub", "Ytdl-Sub")
)
LOCATION = Path("docs/source/config_reference/scripting/entry_variables.rst")
def generate_variable_docs() -> str: @classmethod
docs = section("Entry Variables", level=0) def _variable_class_to_name(cls, obj: Type[Any]) -> str:
assert (
"VariableDefinitions" in obj.__name__
), f"{obj.__name__} doesnt have VariableDefinitions"
return (
camel_case_to_human(obj.__name__)
.replace("Variable Definitions", "Variables")
.replace("Ytdl Sub", "Ytdl-Sub")
)
parent_objs: Dict[str, Type[Any]] = { @classmethod
variable_class_to_name(obj): obj for obj in VariableDefinitions.__bases__ def generate(cls) -> str:
} docs = section("Entry Variables", level=0)
for name in sorted(parent_objs.keys()): parent_objs: Dict[str, Type[Any]] = {
docs += section(name, level=1) cls._variable_class_to_name(obj): obj for obj in VariableDefinitions.__bases__
}
for variable_function_name in properties(parent_objs[name]): for name in sorted(parent_objs.keys()):
docs += get_function_docs( docs += section(name, level=1)
function_name=variable_function_name,
obj=parent_objs[name],
level=2,
)
return docs for variable_function_name in properties(parent_objs[name]):
docs += get_function_docs(
function_name=variable_function_name,
obj=parent_objs[name],
level=2,
)
return docs
print(generate_variable_docs())

View file

@ -43,8 +43,3 @@ def get_function_docs(
docs += inspect.cleandoc(getattr(obj, function_name).__doc__) docs += inspect.cleandoc(getattr(obj, function_name).__doc__)
docs += "\n" docs += "\n"
return docs return docs
def to_out_dir(name: str, docs: str) -> None:
with open(Path("tools") / "docgen" / "out" / name, "w", encoding="utf-8") as out:
out.write(docs)