<commit_before>import avahi
import dbus
import os
import uuid


class ZeroconfService:
    """A simple class to publish a network service with zeroconf using
    avahi.
    """

    def __init__(self, name, port, stype="_http._tcp", subtype=None,
                 domain="", host="", text="", loopback=False):
        self.name = name
        self.stype = stype
        self.domain = domain
        self.host = host
        self.port = port
        self.text = text
        self.subtype = subtype
        self.loopback = loopback
        self.group = None

    def publish(self):
        bus = dbus.SystemBus()
        server = dbus.Interface(
            bus.get_object(
                avahi.DBUS_NAME,
                avahi.DBUS_PATH_SERVER),
            avahi.DBUS_INTERFACE_SERVER)

        g = dbus.Interface(
            bus.get_object(avahi.DBUS_NAME,
                           server.EntryGroupNew()),
            avahi.DBUS_INTERFACE_ENTRY_GROUP)

        iface = avahi.IF_UNSPEC
        if self.loopback:
            iface = 0

        g.AddService(iface, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                     self.name, self.stype, self.domain, self.host,
                     dbus.UInt16(self.port), self.text)

        if self.subtype:
            g.AddServiceSubtype(avahi.IF_UNSPEC,
                                avahi.PROTO_UNSPEC,
                                dbus.UInt32(0),
                                self.name, self.stype, self.domain,
                                self.subtype)

        g.Commit()
        self.group = g

    def unpublish(self):
        self.group.Reset()


class Service:
    """A simple class to publish a Machinekit network service using zeroconf.
    """

    def __init__(self, type, svcUuid, dsn, port, name=None, host=None,
                loopback=False, debug=False):
        self.dsn = dsn
        self.svcUuid = svcUuid
        self.type = type
        self.port = port
        self.name = name
        self.host = host
        self.loopback = loopback
        self.debug = debug

        self.stype = '_machinekit._tcp'
        self.subtype = '_%s._sub.%s' % (self.type, self.stype)

        if name is None:
            pid = os.getpid()
            self.name = '%s on %s pid %i' % \
                        (self.type.title(), self.host, pid)

        me = uuid.uuid1()
        self.statusTxtrec = [str('dsn=' + self.dsn),
                             str('uuid=' + self.svcUuid),
                             str('service=' + self.type),
                             str('instance=' + str(me))]

        if self.debug:
            print(('service: ' + 'dsname = ' + self.dsn +
                                 ' port = ' + str(self.port) +
                                 ' txtrec = ' + str(self.statusTxtrec) +
                                 ' name = ' + self.name))

        self.statusService = ZeroconfService(self.name, self.port,
                                            stype=self.stype,
                                            subtype=self.subtype,
                                            text=self.statusTxtrec,
                                            loopback=self.loopback)

    def publish(self):
        self.statusService.publish()

    def unpublish(self):
        self.statusService.unpublish()<commit_msg>python/machinekit/service: consistent naming of Python an C announced services<commit_after>import avahi
import dbus
import os
import uuid


class ZeroconfService:
    """A simple class to publish a network service with zeroconf using
    avahi.
    """

    def __init__(self, name, port, stype="_http._tcp", subtype=None,
                 domain="", host="", text="", loopback=False):
        self.name = name
        self.stype = stype
        self.domain = domain
        self.host = host
        self.port = port
        self.text = text
        self.subtype = subtype
        self.loopback = loopback
        self.group = None

    def publish(self):
        bus = dbus.SystemBus()
        server = dbus.Interface(
            bus.get_object(
                avahi.DBUS_NAME,
                avahi.DBUS_PATH_SERVER),
            avahi.DBUS_INTERFACE_SERVER)

        g = dbus.Interface(
            bus.get_object(avahi.DBUS_NAME,
                           server.EntryGroupNew()),
            avahi.DBUS_INTERFACE_ENTRY_GROUP)

        iface = avahi.IF_UNSPEC
        if self.loopback:
            iface = 0

        g.AddService(iface, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                     self.name, self.stype, self.domain, self.host,
                     dbus.UInt16(self.port), self.text)

        if self.subtype:
            g.AddServiceSubtype(avahi.IF_UNSPEC,
                                avahi.PROTO_UNSPEC,
                                dbus.UInt32(0),
                                self.name, self.stype, self.domain,
                                self.subtype)

        g.Commit()
        self.group = g

    def unpublish(self):
        self.group.Reset()


class Service:
    """A simple class to publish a Machinekit network service using zeroconf.
    """

    def __init__(self, type, svcUuid, dsn, port, name=None, host=None,
                loopback=False, debug=False):
        self.dsn = dsn
        self.svcUuid = svcUuid
        self.type = type
        self.port = port
        self.name = name
        self.host = host
        self.loopback = loopback
        self.debug = debug

        self.stype = '_machinekit._tcp'
        self.subtype = '_%s._sub.%s' % (self.type, self.stype)

        if name is None:
            pid = os.getpid()
            self.name = '%s service on %s pid %i' % \
                        (self.type.title(), self.host, pid)

        me = uuid.uuid1()
        self.statusTxtrec = [str('dsn=' + self.dsn),
                             str('uuid=' + self.svcUuid),
                             str('instance=' + str(me)),
                             str('service=' + self.type)]

        if self.debug:
            print(('service: ' + 'dsname = ' + self.dsn +
                                 ' port = ' + str(self.port) +
                                 ' txtrec = ' + str(self.statusTxtrec) +
                                 ' name = ' + self.name))

        self.statusService = ZeroconfService(self.name, self.port,
                                            stype=self.stype,
                                            subtype=self.subtype,
                                            text=self.statusTxtrec,
                                            loopback=self.loopback)

    def publish(self):
        self.statusService.publish()

    def unpublish(self):
        self.statusService.unpublish()
{'fraction_non_alphanumeric': 0.1019859638644169, 'fraction_numerical': 0.002986411826190832, 'mean_word_length': 1.306473829201102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3929972', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1860, 'n_words': 388}