Skip to content

Commit

Permalink
Merge pull request #272 from quartiq/py-path
Browse files Browse the repository at this point in the history
py: refactor topic/path
  • Loading branch information
jordens authored Dec 17, 2024
2 parents 1c5a54c + 6c1eeea commit 0958b29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
25 changes: 11 additions & 14 deletions py/miniconf-mqtt/miniconf/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _dispatch(self, message: Message):
fut.set_exception(MiniconfException(code, resp))
del self._inflight[cd]

async def _do(self, topic: str, *, response=1, **kwargs):
async def _do(self, path: str, *, response=1, **kwargs):
response = int(response)
props = Properties(PacketTypes.PUBLISH)
if response:
Expand All @@ -135,6 +135,7 @@ async def _do(self, topic: str, *, response=1, **kwargs):
assert cd not in self._inflight
self._inflight[cd] = fut, []

topic = f"{self.prefix}/settings{path}"
LOGGER.info(f"Publishing {topic}: {kwargs.get('payload')}, [{props}]")
await self.client.publish(
topic,
Expand All @@ -160,44 +161,40 @@ async def set(self, path: str, value, retain=False, response=True, **kwargs):
retain: Retain the the setting on the broker.
"""
return await self._do(
topic=f"{self.prefix}/settings{path}",
path,
payload=json.dumps(value, separators=(",", ":")),
response=response,
retain=retain,
**kwargs,
)

async def list(self, root: str = "", **kwargs):
async def list(self, path: str = "", **kwargs):
"""Get a list of all the paths below a given root.
Args:
root: Path to the root node to list.
path: Path to the root node to list.
"""
return await self._do(
topic=f"{self.prefix}/settings{root}", response=2, **kwargs
)
return await self._do(path, response=2, **kwargs)

async def dump(self, root: str = "", **kwargs):
async def dump(self, path: str = "", **kwargs):
"""Dump all the paths at or below a given root into the settings namespace.
Note that the target may be unable to respond to messages when a multipart
operation (list or dump) is in progress.
This method does not wait for completion.
Args:
root: Path to the root node to dump. Can be a leaf or an internal node.
path: Path to the root node to dump. Can be a leaf or an internal node.
"""
await self._do(topic=f"{self.prefix}/settings{root}", response=0, **kwargs)
await self._do(path, response=0, **kwargs)

async def get(self, path: str, **kwargs):
"""Get the specific value of a given path.
Args:
path: The path to get. Must be a leaf node.
"""
return json.loads(
await self._do(topic=f"{self.prefix}/settings{path}", **kwargs)
)
return json.loads(await self._do(path, **kwargs))

async def clear(self, path: str, response=True, **kwargs):
"""Clear retained value from a path.
Expand All @@ -209,7 +206,7 @@ async def clear(self, path: str, response=True, **kwargs):
"""
return json.loads(
await self._do(
f"{self.prefix}/settings{path}",
path,
retain=True,
response=response,
**kwargs,
Expand Down
21 changes: 11 additions & 10 deletions py/miniconf-mqtt/miniconf/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def close(self):
self.client.on_unsubscribe = None
LOGGER.info(f"Unsubscribed from {self.response_topic}")

def _do(self, topic: str, *, response=1, timeout=None, **kwargs):
def _do(self, path: str, *, response=1, timeout=None, **kwargs):
response = int(response)

props = Properties(PacketTypes.PUBLISH)
Expand Down Expand Up @@ -114,6 +114,7 @@ def on_message(_client, _userdata, message):

self.client.on_message = on_message

topic = f"{self.prefix}/settings{path}"
LOGGER.info(f"Publishing {topic}: {kwargs.get('payload')}, [{props}]")
_pub = self.client.publish(topic, properties=props, **kwargs)

Expand All @@ -140,40 +141,40 @@ def set(self, path: str, value, retain=False, response=True, **kwargs):
retain: Retain the the setting on the broker.
"""
return self._do(
topic=f"{self.prefix}/settings{path}",
path,
payload=json.dumps(value, separators=(",", ":")),
response=response,
retain=retain,
**kwargs,
)

def list(self, root: str = "", **kwargs):
def list(self, path: str = "", **kwargs):
"""Get a list of all the paths below a given root.
Args:
root: Path to the root node to list.
path: Path to the root node to list.
"""
return self._do(topic=f"{self.prefix}/settings{root}", response=2, **kwargs)
return self._do(path, response=2, **kwargs)

def dump(self, root: str = "", **kwargs):
def dump(self, path: str = "", **kwargs):
"""Dump all the paths at or below a given root into the settings namespace.
Note that the target may be unable to respond to messages when a multipart
operation (list or dump) is in progress.
This method does not wait for completion.
Args:
root: Path to the root node to dump. Can be a leaf or an internal node.
path: Path to the root node to dump. Can be a leaf or an internal node.
"""
return self._do(topic=f"{self.prefix}/settings{root}", response=0, **kwargs)
return self._do(path, response=0, **kwargs)

def get(self, path: str, **kwargs):
"""Get the specific value of a given path.
Args:
path: The path to get. Must be a leaf node.
"""
return json.loads(self._do(topic=f"{self.prefix}/settings{path}", **kwargs))
return json.loads(self._do(path, **kwargs))

def clear(self, path: str, response=True, **kwargs):
"""Clear retained value from a path.
Expand All @@ -185,7 +186,7 @@ def clear(self, path: str, response=True, **kwargs):
"""
return json.loads(
self._do(
f"{self.prefix}/settings{path}",
path,
retain=True,
response=response,
**kwargs,
Expand Down

0 comments on commit 0958b29

Please sign in to comment.