Skip to content

Commit a59248e

Browse files
committed
Migrated 'Observer' class to 'murfey.util.client' as well
1 parent 0daa002 commit a59248e

File tree

6 files changed

+81
-80
lines changed

6 files changed

+81
-80
lines changed

src/murfey/client/analyser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
from murfey.client.instance_environment import MurfeyInstanceEnvironment
2323
from murfey.client.rsync import RSyncerUpdate, TransferResult
2424
from murfey.client.tui.forms import FormDependency
25-
from murfey.util import Observer
26-
from murfey.util.client import get_machine_config_client
25+
from murfey.util.client import Observer, get_machine_config_client
2726
from murfey.util.mdoc import get_block
2827
from murfey.util.models import PreprocessingParametersTomo, ProcessingParametersSPA
2928

src/murfey/client/rsync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from urllib.parse import ParseResult
2020

2121
from murfey.client.tui.status_bar import StatusBar
22-
from murfey.util import Observer
22+
from murfey.util.client import Observer
2323

2424
logger = logging.getLogger("murfey.client.rsync")
2525

src/murfey/client/watchdir.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from pathlib import Path
1515
from typing import List, NamedTuple, Optional
1616

17-
import murfey.util
1817
from murfey.client.tui.status_bar import StatusBar
18+
from murfey.util.client import Observer
1919

2020
log = logging.getLogger("murfey.client.watchdir")
2121

@@ -26,7 +26,7 @@ class _FileInfo(NamedTuple):
2626
settling_time: Optional[float] = None
2727

2828

29-
class DirWatcher(murfey.util.Observer):
29+
class DirWatcher(Observer):
3030
def __init__(
3131
self,
3232
path: str | os.PathLike,

src/murfey/client/watchdir_multigrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from pathlib import Path
88
from typing import List
99

10-
import murfey.util
10+
from murfey.util.client import Observer
1111

1212
log = logging.getLogger("murfey.client.watchdir_multigrid")
1313

1414

15-
class MultigridDirWatcher(murfey.util.Observer):
15+
class MultigridDirWatcher(Observer):
1616
def __init__(
1717
self,
1818
path: str | os.PathLike,

src/murfey/util/__init__.py

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

3-
import asyncio
4-
import inspect
53
import logging
64
from pathlib import Path
75
from queue import Queue
86
from threading import Thread
9-
from typing import Awaitable, Callable, Optional
7+
from typing import Optional
108
from uuid import uuid4
119

1210
from werkzeug.utils import secure_filename
@@ -65,75 +63,6 @@ def posix_path(path: Path) -> str:
6563
return str(path)
6664

6765

68-
class Observer:
69-
"""
70-
A helper class implementing the observer pattern supporting both
71-
synchronous and asynchronous notification calls and both synchronous and
72-
asynchronous callback functions.
73-
"""
74-
75-
# The class here should be derived from typing.Generic[P]
76-
# with P = ParamSpec("P"), and the notify/anotify functions should use
77-
# *args: P.args, **kwargs: P.kwargs.
78-
# However, ParamSpec is Python 3.10+ (PEP 612), so we can't use that yet.
79-
80-
def __init__(self):
81-
self._listeners: list[Callable[..., Awaitable[None] | None]] = []
82-
self._secondary_listeners: list[Callable[..., Awaitable[None] | None]] = []
83-
self._final_listeners: list[Callable[..., Awaitable[None] | None]] = []
84-
super().__init__()
85-
86-
def subscribe(
87-
self,
88-
fn: Callable[..., Awaitable[None] | None],
89-
secondary: bool = False,
90-
final: bool = False,
91-
):
92-
if final:
93-
self._final_listeners.append(fn)
94-
elif secondary:
95-
self._secondary_listeners.append(fn)
96-
else:
97-
self._listeners.append(fn)
98-
99-
async def anotify(
100-
self, *args, secondary: bool = False, final: bool = False, **kwargs
101-
) -> None:
102-
awaitables: list[Awaitable] = []
103-
listeners = (
104-
self._secondary_listeners
105-
if secondary
106-
else self._final_listeners if final else self._listeners
107-
)
108-
for notify_function in listeners:
109-
result = notify_function(*args, **kwargs)
110-
if result is not None and inspect.isawaitable(result):
111-
awaitables.append(result)
112-
if awaitables:
113-
await self._await_all(awaitables)
114-
115-
@staticmethod
116-
async def _await_all(awaitables: list[Awaitable]):
117-
for awaitable in asyncio.as_completed(awaitables):
118-
await awaitable
119-
120-
def notify(
121-
self, *args, secondary: bool = False, final: bool = False, **kwargs
122-
) -> None:
123-
awaitables: list[Awaitable] = []
124-
listeners = (
125-
self._secondary_listeners
126-
if secondary
127-
else self._final_listeners if final else self._listeners
128-
)
129-
for notify_function in listeners:
130-
result = notify_function(*args, **kwargs)
131-
if result is not None and inspect.isawaitable(result):
132-
awaitables.append(result)
133-
if awaitables:
134-
asyncio.run(self._await_all(awaitables))
135-
136-
13766
class Processor:
13867
def __init__(self, name: Optional[str] = None):
13968
self._in: Queue = Queue()

src/murfey/util/client.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
and set default directories to work with.
55
"""
66

7+
from __future__ import annotations
8+
9+
import asyncio
710
import configparser
811
import copy
12+
import inspect
913
import json
1014
import logging
1115
import os
1216
import shutil
1317
from functools import lru_cache, partial
1418
from pathlib import Path
15-
from typing import Callable, Optional, Union
19+
from typing import Awaitable, Callable, Optional, Union
1620
from urllib.parse import ParseResult, urlparse, urlunparse
1721

1822
import requests
@@ -169,3 +173,72 @@ def _check_dict_structure(d1: dict, d2: dict) -> bool:
169173
if _check_dict_structure(settings, settings_copy):
170174
with open(p, "w") as sf:
171175
json.dump(settings_copy, sf)
176+
177+
178+
class Observer:
179+
"""
180+
A helper class implementing the observer pattern supporting both
181+
synchronous and asynchronous notification calls and both synchronous and
182+
asynchronous callback functions.
183+
"""
184+
185+
# The class here should be derived from typing.Generic[P]
186+
# with P = ParamSpec("P"), and the notify/anotify functions should use
187+
# *args: P.args, **kwargs: P.kwargs.
188+
# However, ParamSpec is Python 3.10+ (PEP 612), so we can't use that yet.
189+
190+
def __init__(self):
191+
self._listeners: list[Callable[..., Awaitable[None] | None]] = []
192+
self._secondary_listeners: list[Callable[..., Awaitable[None] | None]] = []
193+
self._final_listeners: list[Callable[..., Awaitable[None] | None]] = []
194+
super().__init__()
195+
196+
def subscribe(
197+
self,
198+
fn: Callable[..., Awaitable[None] | None],
199+
secondary: bool = False,
200+
final: bool = False,
201+
):
202+
if final:
203+
self._final_listeners.append(fn)
204+
elif secondary:
205+
self._secondary_listeners.append(fn)
206+
else:
207+
self._listeners.append(fn)
208+
209+
async def anotify(
210+
self, *args, secondary: bool = False, final: bool = False, **kwargs
211+
) -> None:
212+
awaitables: list[Awaitable] = []
213+
listeners = (
214+
self._secondary_listeners
215+
if secondary
216+
else self._final_listeners if final else self._listeners
217+
)
218+
for notify_function in listeners:
219+
result = notify_function(*args, **kwargs)
220+
if result is not None and inspect.isawaitable(result):
221+
awaitables.append(result)
222+
if awaitables:
223+
await self._await_all(awaitables)
224+
225+
@staticmethod
226+
async def _await_all(awaitables: list[Awaitable]):
227+
for awaitable in asyncio.as_completed(awaitables):
228+
await awaitable
229+
230+
def notify(
231+
self, *args, secondary: bool = False, final: bool = False, **kwargs
232+
) -> None:
233+
awaitables: list[Awaitable] = []
234+
listeners = (
235+
self._secondary_listeners
236+
if secondary
237+
else self._final_listeners if final else self._listeners
238+
)
239+
for notify_function in listeners:
240+
result = notify_function(*args, **kwargs)
241+
if result is not None and inspect.isawaitable(result):
242+
awaitables.append(result)
243+
if awaitables:
244+
asyncio.run(self._await_all(awaitables))

0 commit comments

Comments
 (0)