Skip to content

Commit 31b12a4

Browse files
ruff formatting
1 parent 0c390ba commit 31b12a4

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/labthings_fastapi/decorators/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def __get__(self, obj, objtype=None):
9292
getter=func,
9393
)
9494

95+
9596
def thing_setting(func: Callable) -> SettingDescriptor:
9697
"""Mark a method of a Thing as a Setting.
9798

src/labthings_fastapi/descriptors/property.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ def setter(self, func: Callable) -> Self:
226226

227227

228228
class SettingDescriptor(PropertyDescriptor):
229-
230229
@property
231230
def persistent(self):
232231
return True
@@ -241,7 +240,7 @@ def get_raw(self, obj):
241240
Used to get the raw data for saving settings. Any setting that returns
242241
a non-JSON serialisable class should implement a `raw` flag in its getter
243242
"""
244-
#TODO check if has a raw option rather than use a try
243+
# TODO check if has a raw option rather than use a try
245244
if self._getter:
246245
try:
247246
return self._getter(obj, raw=True)

src/labthings_fastapi/server/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def add_thing(self, thing: Thing, path: str):
8181
# TODO: check for illegal things in `path` - potential security issue.
8282
settings_folder = os.path.join(self.settings_folder, path.lstrip("/"))
8383
os.makedirs(settings_folder, exist_ok=True)
84-
thing.attach_to_server(self, path, os.path.join(settings_folder, "settings.json"))
84+
thing.attach_to_server(
85+
self, path, os.path.join(settings_folder, "settings.json")
86+
)
8587

8688
@asynccontextmanager
8789
async def lifespan(self, app: FastAPI):

src/labthings_fastapi/thing.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
_LOGGER = logging.getLogger(__name__)
3838

39+
3940
class Thing:
4041
"""Represents a Thing, as defined by the Web of Things standard.
4142
@@ -90,7 +91,9 @@ async def __aexit__(self, exc_t, exc_v, exc_tb):
9091
if hasattr(self, "__exit__"):
9192
return await run_sync(self.__exit__, exc_t, exc_v, exc_tb)
9293

93-
def attach_to_server(self, server: ThingServer, path: str, setting_storage_path: str):
94+
def attach_to_server(
95+
self, server: ThingServer, path: str, setting_storage_path: str
96+
):
9497
"""Add HTTP handlers to an app for all Interaction Affordances"""
9598
self.path = path
9699
self.action_manager: ActionManager = server.action_manager
@@ -119,15 +122,16 @@ def thing_description(request: Request) -> ThingDescription:
119122
async def websocket(ws: WebSocket):
120123
await websocket_endpoint(self, ws)
121124

122-
123125
_settings: Optional[list[str]] = None
124126

125127
@property
126128
def settings(self):
127129
if self._settings is None:
128130
self._settings = []
129131
for name, attribute in class_attributes(self):
130-
if hasattr(attribute, "property_affordance") and hasattr(attribute, "persistent"):
132+
if hasattr(attribute, "property_affordance") and hasattr(
133+
attribute, "persistent"
134+
):
131135
self._settings.append(name)
132136
return self._settings
133137

@@ -149,27 +153,31 @@ def load_settings(self, setting_storage_path):
149153
setting_dict = json.load(file_obj)
150154
setting_attributes = {}
151155
for name, attribute in class_attributes(self):
152-
if hasattr(attribute, "property_affordance") and hasattr(attribute, "persistent"):
156+
if hasattr(attribute, "property_affordance") and hasattr(
157+
attribute, "persistent"
158+
):
153159
setting_attributes[name] = attribute
154160
for key, value in setting_dict.items():
155161
if key in setting_attributes:
156-
setting_attributes[key].set_without_emit(self, value)
162+
setting_attributes[key].set_without_emit(self, value)
157163
else:
158164
_LOGGER.warning(
159165
"Cannot set %s from persistent storage as %s has no matching setting.",
160-
key, thing_name
166+
key,
167+
thing_name,
161168
)
162169
except (FileNotFoundError, JSONDecodeError, PermissionError):
163170
_LOGGER.warning("Error loading settings for %s", thing_name)
164171
self._setting_storage_path = setting_storage_path
165172

166-
167173
def save_settings(self):
168174
"""Save settings to JSON. This is called when a setting is updated with a setter"""
169175
if self.settings is not None:
170176
setting_dict = {}
171177
for name, attribute in class_attributes(self):
172-
if hasattr(attribute, "property_affordance") and hasattr(attribute, "persistent"):
178+
if hasattr(attribute, "property_affordance") and hasattr(
179+
attribute, "persistent"
180+
):
173181
setting_dict[name] = attribute.get_raw(self)
174182
# Dumpy to string before writing so if this fails the file isn't overwritten
175183
setting_json = json.dumps(setting_dict, indent=4)

0 commit comments

Comments
 (0)