GRC: GR conf&persistent paths instead of ~/.gnuradio || ~/.grc_gnuradio

Signed-off-by: Marcus Müller <mmueller@gnuradio.org>
This commit is contained in:
Marcus Müller 2024-05-17 12:24:07 +02:00 committed by Jeff Long
parent 213f3e30c0
commit 38435f7fe9
11 changed files with 95 additions and 14 deletions

View File

@ -10,6 +10,8 @@ import json
import logging
import os
from ..main import get_state_directory
from ..core import Constants
from . import block_tree, block
path = os.path
@ -18,6 +20,7 @@ logger = logging.getLogger(__name__)
excludes = [
'qtgui_',
'.grc_gnuradio/',
os.path.join(get_state_directory(), Constants.GRC_SUBDIR),
'blks2',
'wxgui',
'epy_block.xml',
@ -31,7 +34,7 @@ excludes = [
class Converter(object):
def __init__(self, search_path, output_dir='~/.cache/grc_gnuradio'):
def __init__(self, search_path: str, output_dir: str = os.path.join(get_state_directory(), Constants.GRC_SUBDIR)):
self.search_path = search_path
self.output_dir = os.path.expanduser(output_dir)
logger.info("Saving converted files to {}".format(self.output_dir))

View File

@ -9,6 +9,7 @@ import os
from os.path import expanduser, normpath, expandvars, exists
from collections import OrderedDict
from ..main import get_state_directory, get_config_file_path
from . import Constants
@ -17,8 +18,7 @@ class Config(object):
license = __doc__.strip()
website = 'https://www.gnuradio.org/'
hier_block_lib_dir = os.environ.get(
'GRC_HIER_PATH', Constants.DEFAULT_HIER_BLOCK_LIB_DIR)
hier_block_lib_dir = os.environ.get('GRC_HIER_PATH', get_state_directory())
def __init__(self, version, version_parts=None, name=None, prefs=None):
self._gr_prefs = prefs if prefs else DummyPrefs()

View File

@ -19,11 +19,14 @@ import numpy
DATA_DIR = os.path.dirname(__file__)
BLOCK_DTD = os.path.join(DATA_DIR, 'block.dtd')
DEFAULT_FLOW_GRAPH = os.path.join(DATA_DIR, 'default_flow_graph.grc')
DEFAULT_HIER_BLOCK_LIB_DIR = os.path.expanduser('~/.grc_gnuradio')
DEFAULT_FLOW_GRAPH_ID = 'default'
CACHE_FILE = os.path.expanduser('~/.cache/grc_gnuradio/cache_v2.json')
EXAMPLE_CACHE_FILE = os.path.expanduser('~/.cache/grc_gnuradio/example_cache.json')
PROJECT_DEFAULT_DIR = 'gnuradio'
GRC_SUBDIR = 'grc'
CACHE_FILE_NAME = 'cache_v2.json'
FALLBACK_CACHE_FILE = os.path.expanduser(f'~/.cache/{PROJECT_DEFAULT_DIR}/{GRC_SUBDIR}/{CACHE_FILE_NAME}')
EXAMPLE_CACHE_FILE_NAME = 'example_cache.json'
FALLBACK_EXAMPLE_CACHE_FILE = os.path.expanduser(f'~/.cache/{PROJECT_DEFAULT_DIR}/{GRC_SUBDIR}/{EXAMPLE_CACHE_FILE_NAME}')
BLOCK_DESCRIPTION_FILE_FORMAT_VERSION = 1
# File format versions:

View File

@ -95,7 +95,7 @@ class ${class_name}(gr.top_block, Qt.QWidget):
self.top_grid_layout = Qt.QGridLayout()
self.top_layout.addLayout(self.top_grid_layout)
self.settings = Qt.QSettings("GNU Radio", "${class_name}")
self.settings = Qt.QSettings("gnuradio/flowgraphs", "${class_name}")
try:
geometry = self.settings.value("geometry")

View File

@ -133,7 +133,12 @@ class Platform(Element):
self.cpp_connection_templates.clear()
self._block_categories.clear()
with Cache(Constants.CACHE_FILE, version=self.config.version) as cache:
try:
from gnuradio.gr import paths
cache_file = os.path.join(paths.cache(), Constants.GRC_SUBDIR, Constants.CACHE_FILE_NAME)
except ImportError:
cache_file = Constants.FALLBACK_CACHE_FILE
with Cache(cache_file, version=self.config.version) as cache:
for file_path in self._iter_files_in_block_path(path):
if file_path.endswith('.block.yml'):

View File

@ -1,6 +1,6 @@
# This file contains system wide configuration data for GNU Radio.
# You may override any setting on a per-user basis by editing
# ~/.gnuradio/config.conf
# ~/.config/gnuradio/grc.conf
[grc]
global_blocks_path = @blocksdir@

View File

@ -11,6 +11,7 @@ import sys
import os
import configparser
from ..main import get_config_file_path
from ..core.Config import Config as CoreConfig
from . import Constants
@ -28,7 +29,7 @@ class Config(CoreConfig):
name = 'GNU Radio Companion'
gui_prefs_file = os.environ.get(
'GRC_PREFS_PATH', os.path.expanduser('~/.gnuradio/grc.conf'))
'GRC_PREFS_PATH', os.path.join(get_config_file_path(), '/grc.conf'))
def __init__(self, install_prefix, *args, **kwargs):
CoreConfig.__init__(self, *args, **kwargs)

View File

@ -3,6 +3,7 @@ from os.path import expanduser, normpath, expandvars, exists
from collections import OrderedDict
from ..core.Config import Config as CoreConfig
from ..main import get_config_file_path
from qtpy import QtCore
@ -11,7 +12,7 @@ class Config(CoreConfig):
name = 'GNU Radio Companion'
gui_prefs_file = os.environ.get(
'GRC_QT_PREFS_PATH', os.path.expanduser('~/.gnuradio/grc_qt.conf'))
'GRC_QT_PREFS_PATH', os.path.join(get_config_file_path(), 'grc_qt.conf'))
def __init__(self, install_prefix, *args, **kwargs):
CoreConfig.__init__(self, *args, **kwargs)

View File

@ -250,7 +250,12 @@ class ExampleBrowser(QWidget, base.Component):
def find_examples(self, progress_callback, ext="grc"):
"""Iterate through the example flowgraph directories and parse them."""
examples_dict = {}
with Cache(Constants.EXAMPLE_CACHE_FILE, log=False) as cache:
try:
from gnuradio.gr import paths
cache_file = os.path.join(paths.cache(), Constants.GRC_SUBDIR, Constants.EXAMPLE_CACHE_FILE_NAME)
except ImportError:
cache_file = Constants.FALLBACK_EXAMPLE_CACHE_FILE
with Cache(cache_file, log=False) as cache:
for entry in self.platform.config.example_paths:
if entry == '':
log.info("Empty example path!")

View File

@ -53,6 +53,7 @@ class Paths(object):
TOP_BLOCK_FILE_MODE = HIER_BLOCK_FILE_MODE | stat.S_IXUSR | stat.S_IXGRP
# Setup paths
# FIXME: this should use ..main.get_config_directory() and get_state_directory(), probably.
'''
HIER_BLOCKS_LIB_DIR = os.environ.get('GRC_HIER_PATH',
os.path.expanduser('~/.grc_gnuradio'))

66
main.py
View File

@ -187,9 +187,71 @@ def run_qt(args, log):
sys.exit(app.run())
def get_config_file_path(config_file: str = "grc.conf") -> str:
oldpath = os.path.join(os.path.expanduser("~/.gnuradio"), config_file)
try:
from gnuradio.gr import paths
newpath = os.path.join(paths.userconf(), config_file)
if os.path.exists(newpath):
return newpath
if os.path.exists(oldpath):
log.warn(f"Found specification for config path '{newpath}', but file does not exist. " +
f"Old default config file path '{oldpath}' exists; using that. " +
"Please consider moving configuration to new location.")
return oldpath
# Default to the correct path if both are configured.
# neither old, nor new path exist: create new path, return that
os.makedirs(newpath, exist_ok=True)
return newpath
except ImportError:
log.warn("Could not retrieve GNU Radio configuration directory from GNU Radio. Trying defaults.")
xdgconf = os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
xdgcand = os.path.join(xdgconf, config_file)
if os.path.exists(xdgcand):
return xdgcand
if os.path.exists(oldpath):
log.warn(f"Using legacy config path '{oldpath}'. Please consider moving configuration " +
f"files to '{newpath}'.")
return oldpath
# neither old, nor new path exist: create new path, return that
os.makedirs(xdgcand, exist_ok=True)
return xdgcand
def get_state_directory() -> str:
oldpath = os.path.expanduser("~/.gnuradio")
try:
from gnuradio.gr import paths
newpath = paths.persistent()
if os.path.exists(newpath):
return newpath
if os.path.exists(oldpath):
log.warn(f"Found specification for persistent state path '{newpath}', but file does not exist. " +
f"Old default persistent state path '{oldpath}' exists; using that. " +
"Please consider moving state to new location.")
return oldpath
# Default to the correct path if both are configured.
# neither old, nor new path exist: create new path, return that
os.makedirs(newpath, exist_ok=True)
return newpath
except (ImportError, NameError):
log.warn("Could not retrieve GNU Radio persistent state directory from GNU Radio. Trying defaults.")
xdgstate = os.getenv("XDG_STATE_HOME", os.path.expanduser("~/.local/state"))
xdgcand = os.path.join(xdgstate, "gnuradio")
if os.path.exists(xdgcand):
return xdgcand
if os.path.exists(oldpath):
log.warn(f"Using legacy state path '{oldpath}'. Please consider moving state " +
f"files to '{newpath}'.")
return oldpath
# neither old, nor new path exist: create new path, return that
os.makedirs(xdgcand, exist_ok=True)
return xdgcand
def main():
grc_version_from_config = ""
grc_qt_config_file = os.path.expanduser('~/.gnuradio/grc_qt.conf')
grc_qt_config_file = get_config_file_path('grc_qt.conf')
if os.path.isfile(grc_qt_config_file):
try:
from qtpy.QtCore import QSettings
@ -227,7 +289,7 @@ def main():
log.info("Starting GNU Radio Companion {} (Python {})".format(gr.version(), py_version))
# File logging
log_file = os.path.expanduser('~') + "/.gnuradio/grc.log"
log_file = os.path.join(get_state_directory(), "grc.log")
try:
fileHandler = logging.FileHandler(log_file)
file_msg_format = '%(asctime)s [%(levelname)s] %(message)s'