Skip to content

Commit

Permalink
Simplify drivers loading
Browse files Browse the repository at this point in the history
  • Loading branch information
egzumer committed Jan 2, 2024
1 parent f4e468c commit 1de00dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
32 changes: 7 additions & 25 deletions chirp/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import glob
import os
import logging
import sys
import chirp.drivers

from chirp import chirp_common, errors

Expand Down Expand Up @@ -172,27 +171,10 @@ class DynamicRadioAlias(rclass):


def import_drivers(limit=None):
frozen = getattr(sys, 'frozen', False)
if sys.platform == 'win32' and frozen:
# We are in a frozen win32 build, so we can not glob
# the driver files, but we do not need to anyway
import chirp.drivers
for module in chirp.drivers.__all__:
try:
__import__('chirp.drivers.%s' % module)
except Exception as e:
print('Failed to import %s: %s' % (module, e))
return

# Safe import of everything in chirp/drivers. We need to import them
# to get them to register, but should not abort if one import fails
chirp_module_base = os.path.dirname(os.path.abspath(__file__))
driver_files = glob.glob(os.path.join(chirp_module_base,
'drivers',
'*.py'))
for driver_file in driver_files:
module, ext = os.path.splitext(driver_file)
driver_module = os.path.basename(module)
if limit and driver_module not in limit:
for module in chirp.drivers.__all__:
if limit and module not in limit:
continue
__import__('chirp.drivers.%s' % driver_module)
try:
__import__('chirp.drivers.%s' % module)
except Exception as e:
print('Failed to import %s: %s' % (module, e))
18 changes: 5 additions & 13 deletions chirp/drivers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import os
import sys
from glob import glob
import warnings
import pkgutil

# This won't be here in the frozen build because we convert this file to
# a static list of driver modules to import.
warnings.filterwarnings('once', category=DeprecationWarning,
module=__name__)

module_dir = os.path.dirname(sys.modules["chirp.drivers"].__file__)
module_dir = sys.modules["chirp.drivers"].__path__
__all__ = []
for i in sorted(glob(os.path.join(module_dir, "*.py"))):
name = os.path.basename(i)[:-3]
if not name.startswith("__"):
__all__.append(name)
for i in pkgutil.iter_modules(module_dir):
__all__.append(i.name)
__all__.sort()

0 comments on commit 1de00dc

Please sign in to comment.