udiskie: Move udisks2 methods to separate module

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2018-07-19 16:03:36 -07:00 committed by James Valleroy
parent f8e3da055f
commit 5dc2ad1fa4
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 71 additions and 50 deletions

View File

@ -22,9 +22,7 @@ from django.utils.translation import ugettext_lazy as _
from plinth import action_utils, actions
from plinth import service as service_module
from plinth import utils
from plinth.menu import main_menu
from plinth.modules.storage import format_bytes
version = 1
@ -90,49 +88,3 @@ def enable():
def disable():
"""Disable the module."""
actions.superuser_run('udiskie', ['disable'])
def list_devices():
"""List devices that can be ejected."""
udisks = utils.import_from_gi('UDisks', '2.0')
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
block = None
devices = []
for obj in object_manager.get_objects():
if not obj.get_block():
continue
block = obj.get_block()
if block.props.id_usage != 'filesystem' or \
block.props.hint_system or \
block.props.read_only:
continue
device_name = block.props.device
if not device_name:
continue
device = {
'device': block.props.device,
'label': block.props.id_label,
'size': format_bytes(block.props.size),
'filesystem_type': block.props.id_type
}
try:
drive = client.get_drive_for_block(block)
device['ejectable'] = drive.props.id_type
except Exception:
pass
try:
device['mount_points'] = obj.get_filesystem().props.mount_points
except Exception:
pass
devices.append(device)
return devices

View File

@ -0,0 +1,68 @@
#
# This file is part of FreedomBox.
#
# 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/>.
#
"""
Library for interacting with udisks2 D-Bus service.
"""
from plinth import utils
from plinth.modules.storage import format_bytes
def list_devices():
"""List devices that can be ejected."""
udisks = utils.import_from_gi('UDisks', '2.0')
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
block = None
devices = []
for obj in object_manager.get_objects():
if not obj.get_block():
continue
block = obj.get_block()
if block.props.id_usage != 'filesystem' or \
block.props.hint_system or \
block.props.read_only:
continue
device_name = block.props.device
if not device_name:
continue
device = {
'device': block.props.device,
'label': block.props.id_label,
'size': format_bytes(block.props.size),
'filesystem_type': block.props.id_type
}
try:
drive = client.get_drive_for_block(block)
device['ejectable'] = drive.props.id_type
except Exception:
pass
try:
device['mount_points'] = obj.get_filesystem().props.mount_points
except Exception:
pass
devices.append(device)
return devices

View File

@ -18,9 +18,10 @@
Views for udiskie module.
"""
from plinth.modules import udiskie
from plinth.views import ServiceView
from . import udisks2
class UdiskieView(ServiceView):
"""View to show devices."""
@ -29,5 +30,5 @@ class UdiskieView(ServiceView):
def get_context_data(self, **kwargs):
"""Return the context data rendering the template."""
context = super().get_context_data(**kwargs)
context['devices'] = udiskie.list_devices()
context['devices'] = udisks2.list_devices()
return context