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:
19class VirtualPython:
20    """Represents a python string. Extracts the imports along
21    with the locals.
22    """
23
24    def __init__(
25        self,
26        content: Optional[str] = None,
27        imports: Optional[list] = None,
28        local_env: Optional[dict] = None,
29    ):
30        self.content = content or ""
31        self.imports = imports or []
32        self.locals = local_env or {}
33
34        if self.content != "":
35            import ast  # pylint: disable=import-outside-toplevel
36
37            self.__normalize_indent()
38
39            # Extract imports from content
40            for node in ast.parse(self.content).body:
41                if isinstance(node, ast.ImportFrom):
42                    self.imports.append(ImportFrom.from_node(node))
43                elif isinstance(node, ast.Import):
44                    self.imports.append(Import.from_node(node))
45
46            # Retreive locals from content
47            exec(self.content, globals(), self.locals)  # pylint: disable=exec-used
48
49    def __normalize_indent(self):
50        self.content = self.content.split("\n")
51        offset = len(self.content[0]) - len(self.content[0].lstrip())
52        lines = [line[offset:] for line in self.content]
53        joiner = "\n"
54        self.content = joiner.join(lines)
55
56    def __add__(self, obj: VirtualPython) -> VirtualPython:
57        local_env = {**self.locals}
58        local_env.update(obj.locals)
59        return VirtualPython(
60            imports=[*self.imports, *obj.imports],
61            local_env=local_env,
62        )
63
64    def __repr__(self) -> str:
65        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)
24    def __init__(
25        self,
26        content: Optional[str] = None,
27        imports: Optional[list] = None,
28        local_env: Optional[dict] = None,
29    ):
30        self.content = content or ""
31        self.imports = imports or []
32        self.locals = local_env or {}
33
34        if self.content != "":
35            import ast  # pylint: disable=import-outside-toplevel
36
37            self.__normalize_indent()
38
39            # Extract imports from content
40            for node in ast.parse(self.content).body:
41                if isinstance(node, ast.ImportFrom):
42                    self.imports.append(ImportFrom.from_node(node))
43                elif isinstance(node, ast.Import):
44                    self.imports.append(Import.from_node(node))
45
46            # Retreive locals from content
47            exec(self.content, globals(), self.locals)  # pylint: disable=exec-used
def get_vp_result(expr: str, **kwargs) -> Any:
 81def get_vp_result(expr: str, **kwargs) -> Any:
 82    """Execute the given python expression, while using
 83    the kwargs as the local variables.
 84
 85    This will collect the result of the expression and return it.
 86    """
 87
 88    if len(expr.split("\n")) > 1:
 89        # Find all assigned vars in expression
 90        avars = []
 91        assignment = None
 92        for assign in walk(parse(expr)):
 93            if isinstance(assign, Assign):
 94                assignment = parse_ast_assign(assign.targets)
 95                avars.extend(parse_ast_assign(assign.targets))
 96
 97        # Find all variables being used that are not are not assigned
 98        used_vars = [
 99            name.id
100            for name in walk(parse(expr))
101            if isinstance(name, Name) and name.id not in avars and name.id not in built_in_funcs
102        ]
103
104        # For all variables used if they are not in kwargs then they == None
105        for var in used_vars:
106            if var not in kwargs:
107                kwargs[var] = None
108
109        source = compile(f"{expr}\n", f"{expr}", "exec")
110        exec(source, globals(), kwargs)  # pylint: disable=exec-used
111        # Get the last assignment and use it as the result
112        return kwargs[assignment[-1]]
113
114    # For all variables used if they are not in kwargs then they == None
115    for var in [name.id for name in walk(parse(expr)) if isinstance(name, Name)]:
116        if var not in kwargs:
117            kwargs[var] = None
118
119    source = compile(f"phml_vp_result = {expr}", expr, "exec")
120    exec(source, globals(), kwargs)  # pylint: disable=exec-used
121    return kwargs["phml_vp_result"] if "phml_vp_result" in kwargs else 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( pvb_value: str, virtual_python: phml.virtual_python.VirtualPython, **kwargs) -> str:
149def process_vp_blocks(pvb_value: str, virtual_python: VirtualPython, **kwargs) -> str:
150    """Process a lines python blocks. Use the VirtualPython locals,
151    and kwargs as local variables for each python block. Import
152    VirtualPython imports in this methods scope.
153
154    Args:
155        value (str): The line to process.
156        virtual_python (VirtualPython): Parsed locals and imports from all python blocks.
157        **kwargs (Any): The extra data to pass to the exec function.
158
159    Returns:
160        str: The processed line as str.
161    """
162
163    # Bring vp imports into scope
164    for imp in virtual_python.imports:
165        exec(str(imp))  # pylint: disable=exec-used
166
167    expressions = extract_expressions(pvb_value)
168    kwargs.update(virtual_python.locals)
169    if expressions is not None:
170        for expr in expressions:
171            result = get_vp_result(expr, **kwargs)
172            if isinstance(result, bool):
173                pvb_value = result
174            else:
175                pvb_value = sub(r"\{[^}]+\}", str(result), pvb_value, 1)
176
177    return pvb_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.

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

Generates a new import object from a python ast Import.

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

Import: A new import object.

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

Generates a new import object from a python ast Import.

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

Import: A new import object.