Use pgi only when gi is not available

- Write a convenience utility to handle the import process.
This commit is contained in:
Sunil Mohan Adapa 2015-11-16 08:53:39 +05:30
parent 608897b9ec
commit cc858bbd6a
4 changed files with 44 additions and 13 deletions

View File

@ -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):

View File

@ -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__)

View File

@ -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__)

36
plinth/utils.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
"""
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)