Skip to content

Commit

Permalink
webui: add wrapper function for context vars
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Jul 27, 2024
1 parent a99fee2 commit 6e58a29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 1 addition & 2 deletions libresvip/plugins/acep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class AcepPattern(BaseModel):
clip_pos: int = Field(0, alias="clipPos")
clip_dur: int = Field(0, alias="clipDur")
enabled: Optional[bool] = True
color: Optional[str] = "#91bcdc"
color: Optional[str] = None
extra_info: dict[str, Any] = Field(default_factory=dict, alias="extraInfo")


Expand Down Expand Up @@ -328,7 +328,6 @@ class AcepChord(BaseModel):


class AcepChordPattern(AcepPattern):
color: str = "#91bcdc"
chords: list[AcepChord] = Field(default_factory=list)


Expand Down
29 changes: 24 additions & 5 deletions libresvip/web/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def page_layout(lang: Optional[str] = None) -> None:
app.on_shutdown(save_settings)
else:
settings = LibreSvipWebUserSettings()
ui_settings_ctx.set(settings)
request = request_contextvar.get()
session_id = request.session["id"]

Expand Down Expand Up @@ -242,8 +241,23 @@ def save_settings() -> None:
context.client.on_disconnect(save_settings)

translation = get_translation()
lazy_translation.set(translation)

def set_context_vars() -> None:
lazy_translation.set(translation)
if app.native.main_window is not None:
ui_settings_ctx.set(settings)

def context_vars_wrapper(func: Any) -> Any:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
set_context_vars()
return func(*args, **kwargs)

return wrapper

set_context_vars()

@context_vars_wrapper
def plugin_info(attr_name: str) -> None:
attr = getattr(selected_formats, attr_name)
with ui.row().classes("w-full h-full"):
Expand Down Expand Up @@ -315,6 +329,7 @@ def panel_header(attr_name: str, title: str, prefix: str, icon: str) -> None:
),
)

@context_vars_wrapper
def options_form(attr_prefix: str, method: str) -> None:
attr = getattr(selected_formats, attr_prefix + "_format")
conversion_plugin = plugin_manager.plugin_registry[attr]
Expand Down Expand Up @@ -423,6 +438,7 @@ def options_form(attr_prefix: str, method: str) -> None:

output_options = ui.refreshable(functools.partial(options_form, "output", "dump"))

@context_vars_wrapper
def middleware_options_form(attr: str, toggler: Switch) -> None:
conversion_plugin = middleware_manager.plugin_registry[attr]
field_types = {}
Expand Down Expand Up @@ -609,6 +625,7 @@ def filter_input_ext(self) -> None:
self.tasks_container.refresh()

@ui.refreshable
@context_vars_wrapper
def tasks_container(self) -> None:
with ui.scroll_area().classes("w-full"):
for i, info in enumerate(self.files_to_convert.values()):
Expand Down Expand Up @@ -700,6 +717,7 @@ def remove_row() -> None:
"round",
).tooltip(_("Remove"))

@context_vars_wrapper
async def _add_task(
self,
name: str,
Expand Down Expand Up @@ -777,11 +795,9 @@ def output_format(self, value: str) -> None:
def task_count(self) -> int:
return len(self.files_to_convert)

@context_vars_wrapper
def convert_one(self, task: ConversionTask, *sub_tasks: list[ConversionTask]) -> None:
task.reset()
lazy_translation.set(translation)
if app.native.main_window is None:
ui_settings_ctx.set(settings)
task.running = True
try:
with CatchWarnings() as w:
Expand Down Expand Up @@ -870,6 +886,7 @@ def convert_one(self, task: ConversionTask, *sub_tasks: list[ConversionTask]) ->
task.error = traceback.format_exc()
task.running = False

@context_vars_wrapper
async def batch_convert(self) -> None:
loop = asyncio.get_event_loop()
running_tasks = []
Expand Down Expand Up @@ -972,6 +989,7 @@ def _export_all(self) -> Optional[tuple[bytes, int, dict[str, str], str]]:
"application/zip",
)

@context_vars_wrapper
async def add_upload(self) -> None:
nonlocal select_input
if app.native.main_window is not None and hasattr(
Expand All @@ -992,6 +1010,7 @@ async def add_upload(self) -> None:
else:
ui.run_javascript("add_upload()")

@context_vars_wrapper
async def save_file(self, file_name: str = "") -> None:
nonlocal select_output
if app.native.main_window is not None and hasattr(
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ upath = [
ruamel_yaml = ["ruamel.yaml>=0.18.6"]
lxml = ["lxml>=5.2.2"]
webui = [
"nicegui[native]>=1.4.30",
"libresvip[upath]",
"nicegui[native]>=1.4.30",
"libresvip[upath]",
]
desktop = [
"pyside6<6.8.0,>=6.7.2",
Expand Down

0 comments on commit 6e58a29

Please sign in to comment.