def main(argv=sys.argv, object_classes=None, default_bus_name=None):
    # Set default mainloop
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    if not default_bus_name:
        default_bus_name = rhsmlib.dbus.constants.BUS_NAME

    options, args = parse_argv(argv, default_bus_name)

    if options.verbose:
        logger = logging.getLogger("")
        logger.setLevel(logging.DEBUG)

    if not object_classes:
        # Read the object classes from the command-line if we don't
        # get anything as a parameter
        object_classes = []
        for clazz in args:
            object_classes.append(rhsmlib.import_class(clazz))

    try:
        log.debug("Starting DBus service with name %s" % options.bus_name)
        server.Server(bus_class=options.bus,
                      bus_name=options.bus_name,
                      object_classes=object_classes).run()
    except dbus.exceptions.DBusException as e:
        if e._dbus_error_name == "org.freedesktop.DBus.Error.AccessDenied":
            print(
                "Access to DBus denied.  You need to edit /etc/dbus-1/system.conf to allow %s or run with "
                "dbus-daemon and a custom config file." % options.bus_name)
    return 0
def parse_argv(argv, default_dbus_name):
    parser = argparse.ArgumentParser(
        usage="usage: %(prog)s [options] [class name]")
    parser.add_argument(
        "-b",
        "--bus",
        action="store",
        dest="bus",
        help="Bus to use (defaults to dbus.SystemBus)",
    )
    parser.add_argument("-n", "--bus-name", default=default_dbus_name)
    parser.add_argument("-v", "--verbose", action="store_true")
    (opts, args) = parser.parse_known_args(argv[1:])

    try:
        if hasattr(opts, "bus") and opts.bus:
            opts.bus = rhsmlib.import_class(opts.bus)
        else:
            opts.bus = dbus.SystemBus
    except (AttributeError, ValueError):
        system_exit(
            os.EX_USAGE,
            _("Error: Unable to load bus '{name}'").format(name=opts.bus))

    return opts, args
def main(argv=sys.argv, object_classes=None, default_bus_name=None):
    # Set default mainloop
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    if not default_bus_name:
        default_bus_name = rhsmlib.dbus.constants.BUS_NAME

    options, args = parse_argv(argv, default_bus_name)

    if options.verbose:
        logger = logging.getLogger('')
        logger.setLevel(logging.DEBUG)

    if not object_classes:
        # Read the object classes from the command-line if we don't
        # get anything as a parameter
        object_classes = []
        for clazz in args:
            object_classes.append(rhsmlib.import_class(clazz))

    try:
        log.debug('Starting DBus service with name %s' % options.bus_name)
        server.Server(
            bus_class=options.bus,
            bus_name=options.bus_name,
            object_classes=object_classes).run()
    except dbus.exceptions.DBusException as e:
        if e._dbus_error_name == "org.freedesktop.DBus.Error.AccessDenied":
            print("Access to DBus denied.  You need to edit /etc/dbus-1/system.conf to allow %s or run with "
                "dbus-daemon and a custom config file." % options.bus_name)
    return 0
Пример #4
0
 def test_import_class(self):
     """
     Simple test of importing class. It seems that we cannot use mock,
     but it is necessary to use some real package with module containing class.
     This unit test was created due to rewriting import_class to use
     importlib and not deprecated imp module.
     """
     clazz = import_class("rhsmlib.file_monitor.FilesystemWatcher")
     self.assertTrue(inspect.isclass(clazz))
Пример #5
0
def load_bus_class(option, opt_str, value, parser):
    """OptionParser callback method to load a class from a string"""
    clazz = rhsmlib.import_class(value)
    parser.values.bus = clazz
def load_bus_class(option, opt_str, value, parser):
    """OptionParser callback method to load a class from a string"""
    clazz = rhsmlib.import_class(value)
    parser.values.bus = clazz