From cc858bbd6a31f9271ebff914ab1bb7970e311058 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 16 Nov 2015 08:53:39 +0530 Subject: [PATCH] Use pgi only when gi is not available - Write a convenience utility to handle the import process. --- plinth/modules/networks/forms.py | 5 ++--- plinth/network.py | 8 +++---- plinth/package.py | 8 +++---- plinth/utils.py | 36 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 plinth/utils.py diff --git a/plinth/modules/networks/forms.py b/plinth/modules/networks/forms.py index 177d02512..10101e717 100644 --- a/plinth/modules/networks/forms.py +++ b/plinth/modules/networks/forms.py @@ -20,9 +20,8 @@ from django.core import validators from django.utils.translation import ugettext_lazy as _ from plinth import network -import pgi -pgi.require_version('NM', '1.0') -from pgi.repository import NM as nm +from plinth.utils import import_from_gi +nm = import_from_gi('NM', '1.0') def _get_interface_choices(device_type): diff --git a/plinth/network.py b/plinth/network.py index 4fb7adcad..a8d8fde3c 100644 --- a/plinth/network.py +++ b/plinth/network.py @@ -21,17 +21,15 @@ Helper functions for working with network manager. import collections from django.utils.translation import ugettext_lazy as _ -import pgi -pgi.require_version('GLib', '2.0') -from pgi.repository import GLib as glib -pgi.require_version('NM', '1.0') -from pgi.repository import NM as nm import logging import socket import struct import subprocess import uuid +from plinth.utils import import_from_gi +glib = import_from_gi('GLib', '2.0') +nm = import_from_gi('NM', '1.0') logger = logging.getLogger(__name__) diff --git a/plinth/package.py b/plinth/package.py index 234a422fc..821df66cb 100644 --- a/plinth/package.py +++ b/plinth/package.py @@ -22,15 +22,13 @@ Framework for installing and updating distribution packages from django.contrib import messages from django.utils.translation import ugettext as _ import functools -import pgi -pgi.require_version('GLib', '2.0') -from pgi.repository import GLib as glib -pgi.require_version('PackageKitGlib', '1.0') -from pgi.repository import PackageKitGlib as packagekit import logging import threading import plinth +from plinth.utils import import_from_gi +glib = import_from_gi('GLib', '2.0') +packagekit = import_from_gi('PackageKitGlib', '1.0') logger = logging.getLogger(__name__) diff --git a/plinth/utils.py b/plinth/utils.py new file mode 100644 index 000000000..edb76adc1 --- /dev/null +++ b/plinth/utils.py @@ -0,0 +1,36 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Miscelleneous utility method. +""" + +import importlib + + +def import_from_gi(library, version): + """Import and return a GObject introspection library.""" + try: + import gi as package + package_name = 'gi' + except ImportError: + import pgi as package + package_name = 'pgi' + + package.require_version(library, version) + + return importlib.import_module(package_name + '.repository.' + library)