From 35dbdf4777dd690ce2e0f21d0d638a309979557a Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 1 Jul 2018 22:26:12 +0530 Subject: [PATCH] udiskie: Use glib library for dbus interaction Reviewed-by: James Valleroy --- plinth/modules/udiskie/__init__.py | 91 ++++++++---------- plinth/modules/udiskie/templates/udiskie.html | 94 +++++-------------- plinth/modules/udiskie/views.py | 1 + 3 files changed, 67 insertions(+), 119 deletions(-) diff --git a/plinth/modules/udiskie/__init__.py b/plinth/modules/udiskie/__init__.py index 70e41ddf5..51e78c113 100644 --- a/plinth/modules/udiskie/__init__.py +++ b/plinth/modules/udiskie/__init__.py @@ -18,11 +18,11 @@ FreedomBox app for udiskie. """ -import dbus from django.utils.translation import ugettext_lazy as _ -from plinth import service as service_module 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 @@ -30,7 +30,7 @@ version = 1 managed_services = ['freedombox-udiskie'] -managed_packages = ['udiskie'] +managed_packages = ['udiskie', 'gir1.2-udisks-2.0'] name = _('udiskie') @@ -93,55 +93,46 @@ def disable(): def list_devices(): - UDISKS2 = 'org.freedesktop.UDisks2' - UDISKS2_PATH = '/org/freedesktop/UDisks2' - BLOCK = UDISKS2 + '.Block' - PROPERTIES = 'org.freedesktop.DBus.Properties' + """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 = [] - bus = dbus.SystemBus() - udisks_obj = bus.get_object(UDISKS2, UDISKS2_PATH) - manager = dbus.Interface(udisks_obj, 'org.freedesktop.DBus.ObjectManager') - for k, v in manager.GetManagedObjects().items(): - drive_info = v.get(BLOCK, {}) - if drive_info.get('IdUsage') == "filesystem" \ - and not drive_info.get('HintSystem') \ - and not drive_info.get('ReadOnly'): - device_name = drive_info.get('Device') - if device_name: - device_name = bytearray(device_name).replace( - b'\x00', b'').decode('utf-8') - short_name = device_name.replace('/dev', '', 1) - bd = bus.get_object( - UDISKS2, UDISKS2_PATH + '/block_devices%s' % short_name) - drive_name = bd.Get(BLOCK, 'Drive', dbus_interface=PROPERTIES) - drive = bus.get_object(UDISKS2, drive_name) - ejectable = drive.Get(UDISKS2 + '.Drive', 'Ejectable', - dbus_interface=PROPERTIES) - if ejectable: - label = bd.Get(BLOCK, 'IdLabel', dbus_interface=PROPERTIES) - size = bd.Get(BLOCK, 'Size', dbus_interface=PROPERTIES) - file_system = bd.Get(BLOCK, 'IdType', - dbus_interface=PROPERTIES) - try: - mount_points = bd.Get(UDISKS2 + '.Filesystem', - 'MountPoints', - dbus_interface=PROPERTIES) - mount_point = mount_points[0] - except: - mount_point = None + for obj in object_manager.get_objects(): + if not obj.get_block(): + continue - devices.append({ - 'device': - device_name, - 'label': - str(label), - 'size': - format_bytes(size), - 'file_system': - str(file_system), - 'mount_point': - ''.join([chr(ch) for ch in mount_point]), - }) + 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 diff --git a/plinth/modules/udiskie/templates/udiskie.html b/plinth/modules/udiskie/templates/udiskie.html index fd3270bc8..ba2f6efdb 100644 --- a/plinth/modules/udiskie/templates/udiskie.html +++ b/plinth/modules/udiskie/templates/udiskie.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "service.html" %} {% comment %} # # This file is part of FreedomBox. @@ -22,77 +22,33 @@ {% load i18n %} {% load static %} -{% block content %} +{% block status %} -

{{ service.name }}

- - {% for paragraph in description %} -

{{ paragraph|safe }}

- {% endfor %} - - {% if manual_page %} -

- - {% trans 'Learn more...' %} - -

- {% endif %} + {{ block.super }}

{% trans "Devices" %}

-
-
- - - - - - - - - - - - {% for device in devices %} - - - - - - - - {% endfor %} - -
{% trans "Device" %}{% trans "Label" %}{% trans "Size" %}{% trans "File System" %}{% trans "Mount Point" %}
{{ device.device }}{{ device.label }}{{ device.size }}{{ device.file_system }}{{ device.mount_point }}
-
-
- {% if show_status_block %} -

{% trans "Status" %}

-

- {% with service_name=service.name %} - {% if service.is_running %} - - {% blocktrans trimmed %} - Service {{ service_name }} is running. - {% endblocktrans %} - {% else %} - - {% blocktrans trimmed %} - Service {{ service_name }} is not running. - {% endblocktrans %} - {% endif %} - {% endwith %} -

- {% endif %} + + + + + + + + + + + + {% for device in devices %} + + + + + + + + {% endfor %} + +
{% trans "Device" %}{% trans "Label" %}{% trans "Size" %}{% trans "Filesystem" %}{% trans "Mount Point" %}
{{ device.device }}{{ device.label }}{{ device.size }}{{ device.filesystem_type }}{{ device.mount_points|join:', ' }}
-

{% trans "Configuration" %}

- -
- {% csrf_token %} - - {{ form|bootstrap }} - - -
{% endblock %} diff --git a/plinth/modules/udiskie/views.py b/plinth/modules/udiskie/views.py index 5d146a6e3..f493498ad 100644 --- a/plinth/modules/udiskie/views.py +++ b/plinth/modules/udiskie/views.py @@ -23,6 +23,7 @@ from plinth.views import ServiceView class UdiskieView(ServiceView): + """View to show devices.""" template_name = 'udiskie.html' def get_context_data(self, **kwargs):