Skip to content

Commit

Permalink
🔖 v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Sep 1, 2024
1 parent 188a947 commit 5bf9feb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ targetmod.NeedMixin().hello()

#### Operate Compile

Remember clean the target cache before mixin.

```python
# targetmodule
def generate(name):
Expand All @@ -143,19 +141,21 @@ class StaticMap:

# mixin
from typing import Any, Union
from saleyo.decorator.compile import CompileToken
from saleyo.decorator.compile import CompileToken, CompileBoundary


@CompileToken(lambda info: "targetmodule.py" in str(info.filename))
def mixin(token: Union[str, bytes, Any]):
def mixin_a(token: Union[str, bytes, Any]):
if not isinstance(token, bytes):
return
return token.replace(b"hell world", b"hello world")
return token.replace(b"static' tag", b"bye")


with CompileBoundary(): # Force to compile
from targetmodule import StaticMap

import targetmodule # noqa: E402
print(StaticMap().FIELD) # hello bye

print(targetmodule.StaticMap().FIELD)

>>> hello hello world
```
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "saleyo"
version = "1.2.2"
version = "1.3.0"
description = "Saleyo is a lightwight scalable Python AOP framework, easy to use and integrate."
authors = [{ name = "H2Sxxa", email = "[email protected]" }]
dependencies = []
Expand Down
33 changes: 33 additions & 0 deletions src/saleyo/base/compile_broadcast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict
from dataclasses import dataclass
from typing import Any, Callable, Dict, Optional, Tuple, Union
import sys

_initialize_flag = False

Expand Down Expand Up @@ -111,3 +112,35 @@ def broadcast(
builtins.compile = broadcast

_initialize_flag = True


class CompileBoundary:
"""
Module Inside will be force compile
## Example
```python
with CompileBoundary():
import module_a
import module_b
import module_c
```
"""

origin: bool

def __init__(self) -> None:
self.origin = sys.dont_write_bytecode

def __enter__(self):
self.activate()

def __exit__(self, *_):
self.deactivate()

def activate(self):
sys.dont_write_bytecode = True

def deactivate(self):
sys.dont_write_bytecode = self.origin
1 change: 1 addition & 0 deletions src/saleyo/decorator/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
add_listen_compile,
initialize_compile_broadcast,
remove_listen_compile,
CompileBoundary as CompileBoundary,
)


Expand Down
12 changes: 6 additions & 6 deletions tests/reload/test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Any, Union
from saleyo.decorator.compile import CompileToken
from saleyo.decorator.compile import CompileToken, CompileBoundary


@CompileToken(lambda info: "targetmodule.py" in str(info.filename))
def mixin(token: Union[str, bytes, Any]):
def mixin_a(token: Union[str, bytes, Any]):
if not isinstance(token, bytes):
return
return token.replace(b"static' tag", b"hello world")
return token.replace(b"static' tag", b"bye")


import targetmodule # noqa: E402
with CompileBoundary():
from targetmodule import StaticMap

print(dir(targetmodule))
print(targetmodule.StaticMap().FIELD)
print(StaticMap().FIELD) # hello bye

0 comments on commit 5bf9feb

Please sign in to comment.