Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grgsm_scanner -l #646

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions python/misc_utils/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@

import osmosdr
import os
from urllib.parse import parse_qsl
from ast import literal_eval

def get_devices(hint=""):
return osmosdr.device_find(osmosdr.device_t(hint))

def device_to_dict(dev):
dev_dict = {}
for k, v in parse_qsl(dev.to_string(), separator=","):
Copy link
Author

@0xxstone 0xxstone Oct 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to avoid the import of parse_qsl, we can also do it this way:

def device_to_dict(dev):
    dev_dict = {}
    for pair in dev.split(","):
        k, v = pair.split("=")
        try:
            dev_dict[k] = literal_eval(v)
        except (ValueError, SyntaxError):
            dev_dict[k] = literal_eval(f"'{v}'")
    return dev_dict

try:
dev_dict[k] = literal_eval(v)
except (ValueError, SyntaxError):
dev_dict[k] = literal_eval(f"'{v}'")
Comment on lines +35 to +38
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/except is required to handle the cases where the values returned by to_string are strings without quotes or strings without quotes starting with / like /dev/ttyUSB0 for example.

return dev_dict

def match(dev, filters):
dev = device_to_dict(dev)
for f in filters:
for k, v in f.items():
if (k not in dev or dev[k] != v):
Expand Down