phml.core.formats.format

 1from typing import Optional
 2
 3from phml.core.nodes import AST, NODE
 4
 5
 6class Format:
 7    """Base class for built-in file formats. Each sub class contains a `parse` and
 8    `compile` method. The parse method should take a string or dict and return
 9    """
10
11    extension: str | list[str] = "txt"
12    """The extension or extensions for the file format. When writing to a file and
13    extensions is a list then the first extensions in the list is used for the file
14    extension.
15    """
16
17    @classmethod
18    def suffix(cls) -> str:
19        """The prefered extension/suffix for the file format."""
20
21        if isinstance(cls.extension, list):
22            return f".{cls.extension[0]}"
23        return f".{cls.extension}"
24
25    @classmethod
26    def is_format(cls, _extension: str) -> bool:
27        """Determine if an extension is of the current format."""
28
29        if isinstance(cls.extension, list):
30            return _extension.lstrip(".") in cls.extension
31        return _extension.lstrip(".") == cls.extension
32
33    @classmethod
34    def parse(cls, data: ..., auto_close: bool = True) -> str:
35        """Parse the given data into a phml.core.nodes.AST."""
36        raise Exception("Base class Format's parse method should never be called")
37
38    @classmethod
39    def compile(
40        cls,
41        ast: AST,
42        components: Optional[dict[str, dict[str, list | NODE]]] = None,
43        **kwargs,
44    ) -> AST:
45        """Compile and process the given ast and return the resulting ast."""
46        raise Exception(f"{cls.__class__.__name__} \
47does not support compiling and returning a phml ast.")
48
49    @classmethod
50    def render(
51        cls,
52        ast: AST,
53        components: Optional[dict[str, dict[str, list | NODE]]] = None,
54        indent: int = 0,
55        **kwargs,
56    ) -> str:
57        """Compile the given phml.core.nodes.AST into string of a given format."""
58        raise Exception("Base class Format's render method should never be called")
class Format:
 7class Format:
 8    """Base class for built-in file formats. Each sub class contains a `parse` and
 9    `compile` method. The parse method should take a string or dict and return
10    """
11
12    extension: str | list[str] = "txt"
13    """The extension or extensions for the file format. When writing to a file and
14    extensions is a list then the first extensions in the list is used for the file
15    extension.
16    """
17
18    @classmethod
19    def suffix(cls) -> str:
20        """The prefered extension/suffix for the file format."""
21
22        if isinstance(cls.extension, list):
23            return f".{cls.extension[0]}"
24        return f".{cls.extension}"
25
26    @classmethod
27    def is_format(cls, _extension: str) -> bool:
28        """Determine if an extension is of the current format."""
29
30        if isinstance(cls.extension, list):
31            return _extension.lstrip(".") in cls.extension
32        return _extension.lstrip(".") == cls.extension
33
34    @classmethod
35    def parse(cls, data: ..., auto_close: bool = True) -> str:
36        """Parse the given data into a phml.core.nodes.AST."""
37        raise Exception("Base class Format's parse method should never be called")
38
39    @classmethod
40    def compile(
41        cls,
42        ast: AST,
43        components: Optional[dict[str, dict[str, list | NODE]]] = None,
44        **kwargs,
45    ) -> AST:
46        """Compile and process the given ast and return the resulting ast."""
47        raise Exception(f"{cls.__class__.__name__} \
48does not support compiling and returning a phml ast.")
49
50    @classmethod
51    def render(
52        cls,
53        ast: AST,
54        components: Optional[dict[str, dict[str, list | NODE]]] = None,
55        indent: int = 0,
56        **kwargs,
57    ) -> str:
58        """Compile the given phml.core.nodes.AST into string of a given format."""
59        raise Exception("Base class Format's render method should never be called")

Base class for built-in file formats. Each sub class contains a parse and compile method. The parse method should take a string or dict and return

Format()
extension: str | list[str] = 'txt'

The extension or extensions for the file format. When writing to a file and extensions is a list then the first extensions in the list is used for the file extension.

@classmethod
def suffix(cls) -> str:
18    @classmethod
19    def suffix(cls) -> str:
20        """The prefered extension/suffix for the file format."""
21
22        if isinstance(cls.extension, list):
23            return f".{cls.extension[0]}"
24        return f".{cls.extension}"

The prefered extension/suffix for the file format.

@classmethod
def is_format(cls, _extension: str) -> bool:
26    @classmethod
27    def is_format(cls, _extension: str) -> bool:
28        """Determine if an extension is of the current format."""
29
30        if isinstance(cls.extension, list):
31            return _extension.lstrip(".") in cls.extension
32        return _extension.lstrip(".") == cls.extension

Determine if an extension is of the current format.

@classmethod
def parse(cls, data: Ellipsis, auto_close: bool = True) -> str:
34    @classmethod
35    def parse(cls, data: ..., auto_close: bool = True) -> str:
36        """Parse the given data into a phml.core.nodes.AST."""
37        raise Exception("Base class Format's parse method should never be called")

Parse the given data into a phml.core.nodes.AST.

39    @classmethod
40    def compile(
41        cls,
42        ast: AST,
43        components: Optional[dict[str, dict[str, list | NODE]]] = None,
44        **kwargs,
45    ) -> AST:
46        """Compile and process the given ast and return the resulting ast."""
47        raise Exception(f"{cls.__class__.__name__} \
48does not support compiling and returning a phml ast.")

Compile and process the given ast and return the resulting ast.

@classmethod
def render( cls, ast: phml.core.nodes.AST.AST, components: Optional[dict[str, dict[str, list | phml.core.nodes.nodes.Root | phml.core.nodes.nodes.Element | phml.core.nodes.nodes.Text | phml.core.nodes.nodes.Comment | phml.core.nodes.nodes.DocType | phml.core.nodes.nodes.Parent | phml.core.nodes.nodes.Node | phml.core.nodes.nodes.Literal]]] = None, indent: int = 0, **kwargs) -> str:
50    @classmethod
51    def render(
52        cls,
53        ast: AST,
54        components: Optional[dict[str, dict[str, list | NODE]]] = None,
55        indent: int = 0,
56        **kwargs,
57    ) -> str:
58        """Compile the given phml.core.nodes.AST into string of a given format."""
59        raise Exception("Base class Format's render method should never be called")

Compile the given phml.core.nodes.AST into string of a given format.