phml.virtual_python

Virtual Python

This module serves to solve the problem of processing python in scopes and to evaluate python expressions.

These expressions and scopes are python "blocks" that are injected into html which then creates my language phml.

Here are examples of the python blocks:

  1. Python element. This is treated as python files similarly to how <script> elements are treated as javascript files.
<python>
    from datetime import datetime

    current_time = datetime.now().strftime('%H:%M:%S')
</python>
  1. Inline python block. Mainly used for retreiving values or creating conditions. The local variables in the blocks are given from the python elements and from kwargs passed to the parser
<p>{current_time}</p>
  1. Multiline python blocks. Same as inline python blocks just that they take up multiple lines. You can write more logic in these blocks, but there local variables are not retained. By default phml will return the last local variable similar to how Jupyter or the python in cli works.
<p>
    Hello, everyone my name is {firstname}. I
    am a {work_position}.
<p>
<p>Here is a list of people and what they like</p>
<p>
    {
        result = []
        for i, person, like in enumerate(zip(people, likes)):
            result.append(f"{i}. {person} likes {like}")
        result = "\n".join(result)
    }
</p>
 1# pylint: skip-file
 2'''Virtual Python
 3
 4This module serves to solve the problem of processing python
 5in scopes and to evaluate python expressions.
 6
 7These expressions and scopes are python "blocks" that are injected
 8into html which then creates my language phml.
 9
10Here are examples of the python blocks:
11
121. Python element. This is treated as python files similarly to how
13`<script>` elements are treated as javascript files.
14
15```html
16<python>
17    from datetime import datetime
18
19    current_time = datetime.now().strftime('%H:%M:%S')
20</python>
21```
22
232. Inline python block. Mainly used for retreiving values
24or creating conditions. The local variables in the blocks are given
25from the python elements and from kwargs passed to the parser
26
27```html
28<p>{current_time}</p>
29```
30
313. Multiline python blocks. Same as inline python blocks just that they
32take up multiple lines. You can write more logic in these blocks, but
33there local variables are not retained. By default phml will return the last
34local variable similar to how Jupyter or the python in cli works.
35
36```html
37<p>
38    Hello, everyone my name is {firstname}. I
39    am a {work_position}.
40<p>
41<p>Here is a list of people and what they like</p>
42<p>
43    {
44        result = []
45        for i, person, like in enumerate(zip(people, likes)):
46            result.append(f"{i}. {person} likes {like}")
47        result = "\\n".join(result)
48    }
49</p>
50```
51'''
52
53from .import_objects import Import, ImportFrom
54from .vp import VirtualPython, get_vp_result, process_vp_blocks
55
56__all__ = ["VirtualPython", "get_vp_result", "process_vp_blocks", "Import", "ImportFrom"]
class VirtualPython:
18class VirtualPython:
19    """Represents a python string. Extracts the imports along
20    with the locals.
21    """
22
23    def __init__(
24        self,
25        content: Optional[str] = None,
26        imports: Optional[list] = None,
27        local_env: Optional[dict] = None,
28    ):
29        self.content = content or ""
30        self.imports = imports or []
31        self.locals = local_env or {}
32
33        if self.content != "":
34            import ast  # pylint: disable=import-outside-toplevel
35
36            self.__normalize_indent()
37
38            # Extract imports from content
39            for node in ast.parse(self.content).body:
40                if isinstance(node, ast.ImportFrom):
41                    self.imports.append(ImportFrom.from_node(node))
42                elif isinstance(node, ast.Import):
43                    self.imports.append(Import.from_node(node))
44
45            # Retreive locals from content
46            exec(self.content, globals(), self.locals)  # pylint: disable=exec-used
47
48    def __normalize_indent(self):
49        self.content = self.content.split("\n")
50        offset = len(self.content[0]) - len(self.content[0].lstrip())
51        lines = [line[offset:] for line in self.content]
52        joiner = "\n"
53        self.content = joiner.join(lines)
54
55    def __add__(self, obj: VirtualPython) -> VirtualPython:
56        local_env = {**self.locals}
57        local_env.update(obj.locals)
58        return VirtualPython(
59            imports=[*self.imports, *obj.imports],
60            local_env=local_env,
61        )
62
63    def __repr__(self) -> str:
64        return f"VP(imports: {len(self.imports)}, locals: {len(self.locals.keys())})"

Represents a python string. Extracts the imports along with the locals.

VirtualPython( content: Optional[str] = None, imports: Optional[list] = None, local_env: Optional[dict] = None)
23    def __init__(
24        self,
25        content: Optional[str] = None,
26        imports: Optional[list] = None,
27        local_env: Optional[dict] = None,
28    ):
29        self.content = content or ""
30        self.imports = imports or []
31        self.locals = local_env or {}
32
33        if self.content != "":
34            import ast  # pylint: disable=import-outside-toplevel
35
36            self.__normalize_indent()
37
38            # Extract imports from content
39            for node in ast.parse(self.content).body:
40                if isinstance(node, ast.ImportFrom):
41                    self.imports.append(ImportFrom.from_node(node))
42                elif isinstance(node, ast.Import):
43                    self.imports.append(Import.from_node(node))
44
45            # Retreive locals from content
46            exec(self.content, globals(), self.locals)  # pylint: disable=exec-used
def get_vp_result(expr: str, **kwargs) -> Any:
 80def get_vp_result(expr: str, **kwargs) -> Any:
 81    """Execute the given python expression, while using
 82    the kwargs as the local variables.
 83
 84    This will collect the result of the expression and return it.
 85    """
 86    # Find all assigned vars in expression
 87    avars = []
 88    for assign in walk(parse(expr)):
 89        if isinstance(assign, Assign):
 90            avars.extend(parse_ast_assign(assign.targets))
 91
 92    # Find all variables being used that are not are not assigned
 93    used_vars = [
 94        name.id for name in walk(parse(expr)) if isinstance(name, Name) and name.id not in avars
 95    ]
 96
 97    # For all variables used if they are not in kwargs then they == None
 98    for var in used_vars:
 99        if var not in kwargs:
100            kwargs[var] = None
101
102    try:
103        if len(expr.split("\n")) > 1:
104            exec(expr, {}, kwargs)  # pylint: disable=exec-used
105            return kwargs["result"] or kwargs["results"]
106    except NameError as exception:
107        print(exception, expr, kwargs)
108        return None
109
110    try:
111        exec(f"phml_vp_result = {expr}", {}, kwargs)  # pylint: disable=exec-used
112        return kwargs["phml_vp_result"]
113    except NameError as exception:
114        print(exception, expr, kwargs)
115        return None

Execute the given python expression, while using the kwargs as the local variables.

This will collect the result of the expression and return it.

def process_vp_blocks( value: str, virtual_python: phml.virtual_python.VirtualPython, **kwargs) -> str:
143def process_vp_blocks(value: str, virtual_python: VirtualPython, **kwargs) -> str:
144    """Process a lines python blocks. Use the VirtualPython locals,
145    and kwargs as local variables for each python block. Import
146    VirtualPython imports in this methods scope.
147
148    Args:
149        value (str): The line to process.
150        virtual_python (VirtualPython): Parsed locals and imports from all python blocks.
151        **kwargs (Any): The extra data to pass to the exec function.
152
153    Returns:
154        str: The processed line as str.
155    """
156
157    # Bring vp imports into scope
158    for imp in virtual_python.imports:
159        exec(str(imp))  # pylint: disable=exec-used
160
161    expressions = extract_expressions(value)
162    kwargs.update(virtual_python.locals)
163    if expressions is not None:
164        for expr in expressions:
165            result = get_vp_result(expr, **kwargs)
166            if isinstance(result, bool):
167                value = result
168            else:
169                value = sub(r"\{.*\}", str(result), value)
170
171    return value

Process a lines python blocks. Use the VirtualPython locals, and kwargs as local variables for each python block. Import VirtualPython imports in this methods scope.

Args
  • value (str): The line to process.
  • virtual_python (VirtualPython): Parsed locals and imports from all python blocks.
  • **kwargs (Any): The extra data to pass to the exec function.
Returns

str: The processed line as str.

class Import(phml.virtual_python.import_objects.PythonImport):
19class Import(PythonImport):
20    """Helper object that stringifies the python ast Import.
21    This is mainly to locally import things dynamically.
22    """
23
24    def __init__(self, modules: list[str]):
25        super().__init__()
26        self.modules = modules
27
28    @classmethod
29    def from_node(cls, imp) -> Import:
30        """Generates a new import object from a python ast Import.
31
32        Args:
33            imp (ast.Import): Python ast object
34
35        Returns:
36            Import: A new import object.
37        """
38        return Import([alias.name for alias in imp.names])
39
40    def __repr__(self) -> str:
41        return f"Import(modules=[{', '.join(self.modules)}]"
42
43    def __str__(self) -> str:
44        return f"import {', '.join(self.modules)}"

Helper object that stringifies the python ast Import. This is mainly to locally import things dynamically.

Import(modules: list[str])
24    def __init__(self, modules: list[str]):
25        super().__init__()
26        self.modules = modules
@classmethod
def from_node(cls, imp) -> phml.virtual_python.Import:
28    @classmethod
29    def from_node(cls, imp) -> Import:
30        """Generates a new import object from a python ast Import.
31
32        Args:
33            imp (ast.Import): Python ast object
34
35        Returns:
36            Import: A new import object.
37        """
38        return Import([alias.name for alias in imp.names])

Generates a new import object from a python ast Import.

Args
  • imp (ast.Import): Python ast object
Returns

Import: A new import object.

class ImportFrom(phml.virtual_python.import_objects.PythonImport):
47class ImportFrom(PythonImport):
48    """Helper object that stringifies the python ast ImportFrom.
49    This is mainly to locally import things dynamically.
50    """
51
52    def __init__(self, module: str, names: list[str]):
53        super().__init__()
54        self.module = module
55        self.names = names
56
57    @classmethod
58    def from_node(cls, imp) -> Import:
59        """Generates a new import object from a python ast Import.
60
61        Args:
62            imp (ast.Import): Python ast object
63
64        Returns:
65            Import: A new import object.
66        """
67        return ImportFrom(imp.module, [alias.name for alias in imp.names])
68
69    def __repr__(self) -> str:
70        return f"ImportFrom(module={self.module}, names=[{', '.join(self.names)}])"
71
72    def __str__(self) -> str:
73        return f"from {self.module} import {', '.join(self.names)}"

Helper object that stringifies the python ast ImportFrom. This is mainly to locally import things dynamically.

ImportFrom(module: str, names: list[str])
52    def __init__(self, module: str, names: list[str]):
53        super().__init__()
54        self.module = module
55        self.names = names
@classmethod
def from_node(cls, imp) -> phml.virtual_python.Import:
57    @classmethod
58    def from_node(cls, imp) -> Import:
59        """Generates a new import object from a python ast Import.
60
61        Args:
62            imp (ast.Import): Python ast object
63
64        Returns:
65            Import: A new import object.
66        """
67        return ImportFrom(imp.module, [alias.name for alias in imp.names])

Generates a new import object from a python ast Import.

Args
  • imp (ast.Import): Python ast object
Returns

Import: A new import object.