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

Simplify drivers loading #862

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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()
Loading