avahi: Style and code cleanups in zeroconf module.
- Prepare for handling missing dbus directly in module. - Constants should always be all caps. - Extracted helpers from class to convey intent via their naming. - Moved imports out of class, imports should always be on the top. - Made sure calling publish mutiple times does not re-convert text. - Made sure calling unpublish without publish does not break.
This commit is contained in:
parent
697bff81cd
commit
cf5589b4eb
@ -1,17 +1,30 @@
|
|||||||
import dbus
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
__all__ = ["Zeroconf"]
|
try:
|
||||||
|
import dbus
|
||||||
|
except ImportError:
|
||||||
|
dbus = None
|
||||||
|
|
||||||
avahi_IF_UNSPEC = -1
|
import re
|
||||||
avahi_PROTO_UNSPEC = -1
|
|
||||||
avahi_PublishFlags_None = 0
|
_AVAHI_IF_UNSPEC = -1
|
||||||
|
_AVAHI_PROTO_UNSPEC = -1
|
||||||
|
_AVAHI_PUBLISHFLAGS_NONE = 0
|
||||||
|
|
||||||
|
|
||||||
|
def _filter_loopback_and_meta_addresses(host):
|
||||||
|
# TODO: see if we can find a cleaner way of handling this.
|
||||||
|
if re.search(r'(?<![.\d])(127|0)[.]', host):
|
||||||
|
return ''
|
||||||
|
return host
|
||||||
|
|
||||||
|
|
||||||
|
def _convert_text_to_dbus_bytes(text):
|
||||||
|
return [[dbus.Byte(ord(c)) for c in s] for s in text]
|
||||||
|
|
||||||
|
|
||||||
class Zeroconf:
|
class Zeroconf:
|
||||||
"""A simple class to publish a network service with zeroconf using
|
"""Publish a network service with zeroconf using avahi."""
|
||||||
avahi.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, name, port, stype="_http._tcp",
|
def __init__(self, name, port, stype="_http._tcp",
|
||||||
domain="", host="", text=[]):
|
domain="", host="", text=[]):
|
||||||
@ -20,35 +33,27 @@ class Zeroconf:
|
|||||||
self.domain = domain
|
self.domain = domain
|
||||||
self.port = port
|
self.port = port
|
||||||
self.text = text
|
self.text = text
|
||||||
# Let avahi choose how to advertise services
|
self.host = _filter_loopback_and_meta_addresses(host)
|
||||||
# listening on lo and meta addresses
|
self.group = None
|
||||||
import re
|
|
||||||
lo = re.search('(?<![.\d])(127|0)[.]', host)
|
|
||||||
self.host = "" if lo else host
|
|
||||||
|
|
||||||
def publish(self):
|
def publish(self):
|
||||||
bus = dbus.SystemBus()
|
bus = dbus.SystemBus()
|
||||||
server = dbus.Interface(
|
server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", "/"),
|
||||||
bus.get_object(
|
"org.freedesktop.Avahi.Server")
|
||||||
"org.freedesktop.Avahi",
|
|
||||||
"/"),
|
|
||||||
"org.freedesktop.Avahi.Server")
|
|
||||||
|
|
||||||
g = dbus.Interface(
|
self.group = dbus.Interface(
|
||||||
bus.get_object("org.freedesktop.Avahi",
|
bus.get_object("org.freedesktop.Avahi", server.EntryGroupNew()),
|
||||||
server.EntryGroupNew()),
|
|
||||||
"org.freedesktop.Avahi.EntryGroup")
|
"org.freedesktop.Avahi.EntryGroup")
|
||||||
|
|
||||||
if self.text:
|
text = _convert_text_to_dbus_bytes(self.text)
|
||||||
self.text = [[dbus.Byte(ord(c)) for c in s] for s in self.text]
|
self.group.AddService(_AVAHI_IF_UNSPEC, _AVAHI_PROTO_UNSPEC,
|
||||||
|
dbus.UInt32(_AVAHI_PUBLISHFLAGS_NONE),
|
||||||
|
self.name, self.stype, self.domain, self.host,
|
||||||
|
dbus.UInt16(self.port), text)
|
||||||
|
|
||||||
g.AddService(avahi_IF_UNSPEC, avahi_PROTO_UNSPEC,
|
self.group.Commit()
|
||||||
dbus.UInt32(avahi_PublishFlags_None),
|
|
||||||
self.name, self.stype, self.domain, self.host,
|
|
||||||
dbus.UInt16(self.port), self.text)
|
|
||||||
|
|
||||||
g.Commit()
|
|
||||||
self.group = g
|
|
||||||
|
|
||||||
def unpublish(self):
|
def unpublish(self):
|
||||||
self.group.Reset()
|
if self.group:
|
||||||
|
self.group.Reset()
|
||||||
|
self.group = None
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user