Skip to content

Scaffold

pysdrlib.scaffold

Generate the skeleton of a new driver

python -m pysdrlib.scaffold my_radio                       # a driver on a compiled extension (C++ and Python)
python -m pysdrlib.scaffold my_radio --class MyRadio       # name the Device class (default: CamelCase of the name)
python -m pysdrlib.scaffold my_radio --python-only         # no extension: a Device that talks to its radio in Python
python -m pysdrlib.scaffold my_radio --out-of-tree         # a package of its own (not in pysdrlib) that pysdrlib finds once installed

It writes the files pysdrlib.contributing lists, filled in with the names, and fails if any already exists. The Python driver imports and passes pysdrlib.testing.check_driver as generated; what it can't know (the vendor's API, the radio's limits and gain stages) is marked TODO. A native driver's _ext/<name>.py returns no extension from get() until you have replaced the C++ placeholders, so an unfinished driver never breaks pip install.

Functions:

Name Description
files

{path relative to the pysdrlib package (or, out of tree, to the folder that holds the new project): text} of a

new_driver

Write the skeleton of the driver name under root (default: the pysdrlib package). Returns the paths written;

files

files(name, cls, native=True, out_of_tree=False)

{path relative to the pysdrlib package (or, out of tree, to the folder that holds the new project): text} of a new driver

Source code in src/pysdrlib/scaffold.py
def files(name, cls, native=True, out_of_tree=False):
    """{path relative to the pysdrlib package (or, out of tree, to the folder that holds the new project): text} of a
    new driver"""
    names = {"NAME": name, "CLASS": cls, "UPPER": name.upper(), "DIST": name.replace("_", "-")}
    if out_of_tree:
        project = f"pysdrlib-{names['DIST']}"
        out = {
            f"{project}/pyproject.toml": PYPROJECT_OUT,
            f"{project}/README.md": README_OUT,
            f"{project}/src/pysdrlib_{name}/__init__.py": INIT_OUT,
            f"{project}/src/pysdrlib_{name}/{name}.py": DEVICE_OUT,
            f"{project}/tests/test_{name}.py": TEST_OUT,
        }
        return {path: _fill(text, names) for path, text in out.items()}
    out = {
        f"{name}/__init__.py": DOC + (INIT if native else INIT_PY),
        f"{name}/{name}.py": DEVICE if native else DEVICE_PY,
    }
    if native:
        out.update({
            f"{name}/lib/__init__.py": LIB_INIT,
            f"_ext/{name}.py": EXT,
            f"_native/{name}/{name}_device.hpp": HPP,
            f"_native/{name}/{name}_device.cpp": CPP,
            f"_native/{name}/{name}_pybind.cpp": PYBIND,
        })
    return {path: _fill(text, names) for path, text in out.items()}

new_driver

new_driver(name, cls=None, native=True, root=None, out_of_tree=False)

Write the skeleton of the driver name under root (default: the pysdrlib package). Returns the paths written; raises FileExistsError, writing nothing, if any already exists. out_of_tree: a project of its own, pysdrlib-<name>/ under root (default: the current folder), which pysdrlib finds through an entry point once installed. It is always a Python-only driver (native is ignored)

Source code in src/pysdrlib/scaffold.py
def new_driver(name, cls=None, native=True, root=None, out_of_tree=False):
    """Write the skeleton of the driver `name` under `root` (default: the pysdrlib package). Returns the paths written;
    raises FileExistsError, writing nothing, if any already exists.
    out_of_tree: a project of its own, `pysdrlib-<name>/` under `root` (default: the current folder), which pysdrlib
    finds through an entry point once installed. It is always a Python-only driver (`native` is ignored)"""
    from . import devices
    if not re.fullmatch(r"[a-z][a-z0-9_]*", name) or name in ("base", "file", "remote", "testing", "scaffold") \
            or name in devices.ls() or name in devices._packages():
        raise ValueError(f"{name!r}: a driver name is lower case letters, digits and underscores, starting with a letter, "
                         "and not one pysdrlib already uses")
    cls = cls or camel(name)
    if not cls.isidentifier():
        raise ValueError(f"{cls!r} is not a valid class name")
    root = Path(root) if root else (Path.cwd() if out_of_tree else PACKAGE)
    planned = {root / path: text for path, text in files(name, cls, native, out_of_tree).items()}
    existing = [str(p) for p in planned if p.exists()]
    if existing:
        raise FileExistsError("already exists: " + ", ".join(existing))
    for path, text in planned.items():
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(text, encoding="utf-8")
    return list(planned)