(list_only=False, target_model=None)
return chosen_adapter["adapter"], chosen_adapter["frontend"]
Find the DVB adapter

    Returns:
        Tuple with (adapter index, frontend index)
Find the DVB adapter
Find the DVB adapter
def _find_adapter(list_only=False, target_model=None):
    """Find the DVB adapter

    Returns:
        Tuple with (adapter index, frontend index)

    """
    if (target_model is None):
        util.print_header("Find DVB Adapter")

    # Search a range of adapters. There is no command to list all adapters, so
    # we try to list each one individually using `dvbnet -a adapter_no -l`.
    adapters = list()
    for a in range(0, 10):
        cmd = ["dvbnet", "-a", str(a), "-l"]
        logger.debug("> " + " ".join(cmd))

        with open(os.devnull, 'w') as devnull:
            res = subprocess.call(cmd, stdout=devnull, stderr=devnull)
            if (res == 0):
                # Try a few frontends too
                for f in range(0, 2):
                    try:
                        output = subprocess.check_output(
                            ["dvb-fe-tool", "-a",
                             str(a), "-f",
                             str(f)])
                        line = output.splitlines()[0].decode().split()
                        adapter = {
                            "adapter":
                            str(a),
                            "frontend":
                            line[5].replace(")", "").split("frontend")[-1],
                            "vendor":
                            line[1],
                            "model":
                            " ".join(line[2:4]),
                            "support":
                            line[4]
                        }
                        adapters.append(adapter)
                        logger.debug(pformat(adapter))
                    except subprocess.CalledProcessError:
                        pass

    # If nothing was obtained using dvbnet, try to inspect dmesg logs
    if (len(adapters) == 0):
        ps = subprocess.Popen("dmesg", stdout=subprocess.PIPE)
        try:
            output = subprocess.check_output(["grep", "frontend"],
                                             stdin=ps.stdout,
                                             stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as grepexc:
            if (grepexc.returncode == 1):
                output = ""
                pass
        ps.wait()

        lines = output.splitlines()
        adapter_set = set()  # use set to filter unique values
        adapters = list()
        for line in lines:
            linesplit = line.decode().split()
            if ("adapter" not in linesplit):
                continue
            i_adapter = linesplit.index('adapter')
            i_frontend = linesplit.index('frontend')
            device = linesplit[i_frontend + 2:]
            adapter = {
                "adapter": linesplit[i_adapter + 1],
                "frontend": linesplit[i_frontend + 1],
                "vendor": device[0][1:],
                "model": " ".join(device[1:-1]),
                "support": device[-1][:-4].replace('(', '').replace(')', '')
            }

            # Maybe the device on dmesg does not exist anymore. Check!
            try:
                cmd = ["dvbnet", "-a", adapter['adapter'], "-l"]
                logger.debug("> " + " ".join(cmd))
                res = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
            except subprocess.CalledProcessError:
                continue

            # If it exists, add to set of candidate adapters
            adapter_set.add(json.dumps(adapter))

        # Process unique adapter logs
        for adapter in adapter_set:
            adapters.append(json.loads(adapter))
            logger.debug(pformat(json.loads(adapter)))

    dvb_s2_adapters = [a for a in adapters if ("DVB-S/S2" in a["support"])]
    logger.debug(dvb_s2_adapters)

    assert (len(dvb_s2_adapters) > 0), "No DVB-S2 adapters found"

    chosen_adapter = None
    if (target_model is not None):
        # Search without prompting
        for adapter in dvb_s2_adapters:
            if (adapter["model"] == target_model):
                chosen_adapter = adapter
                break
    else:
        # Prompt the user
        for adapter in dvb_s2_adapters:
            print("Found DVB-S2 adapter: %s %s" %
                  (adapter["vendor"], adapter["model"]))

            if (not list_only):
                if (len(dvb_s2_adapters) == 1
                        or util.ask_yes_or_no("Choose adapter?")):
                    chosen_adapter = adapter
                    logger.debug("Chosen adapter:")
                    logger.debug(pformat(adapter))
                    break

    if (list_only):
        return

    if (chosen_adapter is None):
        if (target_model is None):
            err_msg = "Please choose a DVB-S2 adapter"
        else:
            err_msg = "Please choose a valid DVB-S2 adapter"
        raise ValueError(err_msg)

    return chosen_adapter["adapter"], chosen_adapter["frontend"]
def _find_adapter ( list_only = False , target_model = None ) : if ( target_model is None ) : util . print_header ( "Find DVB Adapter" ) # Search a range of adapters. There is no command to list all adapters, so # we try to list each one individually using `dvbnet -a adapter_no -l`. adapters = list ( ) for a in range ( 0 , 10 ) : cmd = [ "dvbnet" , "-a" , str ( a ) , "-l" ] logger . debug ( "> " + " " . join ( cmd ) ) with open ( os . devnull , 'w' ) as devnull : res = subprocess . call ( cmd , stdout = devnull , stderr = devnull ) if ( res == 0 ) : # Try a few frontends too for f in range ( 0 , 2 ) : try : output = subprocess . check_output ( [ "dvb-fe-tool" , "-a" , str ( a ) , "-f" , str ( f ) ] ) line = output . splitlines ( ) [ 0 ] . decode ( ) . split ( ) adapter = { "adapter" : str ( a ) , "frontend" : line [ 5 ] . replace ( ")" , "" ) . split ( "frontend" ) [ - 1 ] , "vendor" : line [ 1 ] , "model" : " " . join ( line [ 2 : 4 ] ) , "support" : line [ 4 ] } adapters . append ( adapter ) logger . debug ( pformat ( adapter ) ) except subprocess . CalledProcessError : pass # If nothing was obtained using dvbnet, try to inspect dmesg logs if ( len ( adapters ) == 0 ) : ps = subprocess . Popen ( "dmesg" , stdout = subprocess . PIPE ) try : output = subprocess . check_output ( [ "grep" , "frontend" ] , stdin = ps . stdout , stderr = subprocess . STDOUT ) except subprocess . CalledProcessError as grepexc : if ( grepexc . returncode == 1 ) : output = "" pass ps . wait ( ) lines = output . splitlines ( ) adapter_set = set ( ) # use set to filter unique values adapters = list ( ) for line in lines : linesplit = line . decode ( ) . split ( ) if ( "adapter" not in linesplit ) : continue i_adapter = linesplit . index ( 'adapter' ) i_frontend = linesplit . index ( 'frontend' ) device = linesplit [ i_frontend + 2 : ] adapter = { "adapter" : linesplit [ i_adapter + 1 ] , "frontend" : linesplit [ i_frontend + 1 ] , "vendor" : device [ 0 ] [ 1 : ] , "model" : " " . join ( device [ 1 : - 1 ] ) , "support" : device [ - 1 ] [ : - 4 ] . replace ( '(' , '' ) . replace ( ')' , '' ) } # Maybe the device on dmesg does not exist anymore. Check! try : cmd = [ "dvbnet" , "-a" , adapter [ 'adapter' ] , "-l" ] logger . debug ( "> " + " " . join ( cmd ) ) res = subprocess . check_output ( cmd , stderr = subprocess . DEVNULL ) except subprocess . CalledProcessError : continue # If it exists, add to set of candidate adapters adapter_set . add ( json . dumps ( adapter ) ) # Process unique adapter logs for adapter in adapter_set : adapters . append ( json . loads ( adapter ) ) logger . debug ( pformat ( json . loads ( adapter ) ) ) dvb_s2_adapters = [ a for a in adapters if ( "DVB-S/S2" in a [ "support" ] ) ] logger . debug ( dvb_s2_adapters ) assert ( len ( dvb_s2_adapters ) > 0 ) , "No DVB-S2 adapters found" chosen_adapter = None if ( target_model is not None ) : # Search without prompting for adapter in dvb_s2_adapters : if ( adapter [ "model" ] == target_model ) : chosen_adapter = adapter break else : # Prompt the user for adapter in dvb_s2_adapters : print ( "Found DVB-S2 adapter: %s %s" % ( adapter [ "vendor" ] , adapter [ "model" ] ) ) if ( not list_only ) : if ( len ( dvb_s2_adapters ) == 1 or util . ask_yes_or_no ( "Choose adapter?" ) ) : chosen_adapter = adapter logger . debug ( "Chosen adapter:" ) logger . debug ( pformat ( adapter ) ) break if ( list_only ) : return if ( chosen_adapter is None ) : if ( target_model is None ) : err_msg = "Please choose a DVB-S2 adapter" else : err_msg = "Please choose a valid DVB-S2 adapter" raise ValueError ( err_msg ) return chosen_adapter [ "adapter" ] , chosen_adapter [ "frontend" ]