udiskie: Use glib library for dbus interaction

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2018-07-01 22:26:12 +05:30 committed by James Valleroy
parent 08eeace30f
commit 35dbdf4777
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 67 additions and 119 deletions

View File

@ -18,11 +18,11 @@
FreedomBox app for udiskie. FreedomBox app for udiskie.
""" """
import dbus
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from plinth import service as service_module
from plinth import action_utils, actions 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.menu import main_menu
from plinth.modules.storage import format_bytes from plinth.modules.storage import format_bytes
@ -30,7 +30,7 @@ version = 1
managed_services = ['freedombox-udiskie'] managed_services = ['freedombox-udiskie']
managed_packages = ['udiskie'] managed_packages = ['udiskie', 'gir1.2-udisks-2.0']
name = _('udiskie') name = _('udiskie')
@ -93,55 +93,46 @@ def disable():
def list_devices(): def list_devices():
UDISKS2 = 'org.freedesktop.UDisks2' """List devices that can be ejected."""
UDISKS2_PATH = '/org/freedesktop/UDisks2' udisks = utils.import_from_gi('UDisks', '2.0')
BLOCK = UDISKS2 + '.Block'
PROPERTIES = 'org.freedesktop.DBus.Properties'
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
block = None
devices = [] devices = []
bus = dbus.SystemBus() for obj in object_manager.get_objects():
udisks_obj = bus.get_object(UDISKS2, UDISKS2_PATH) if not obj.get_block():
manager = dbus.Interface(udisks_obj, 'org.freedesktop.DBus.ObjectManager') continue
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
devices.append({ block = obj.get_block()
'device': if block.props.id_usage != 'filesystem' or \
device_name, block.props.hint_system or \
'label': block.props.read_only:
str(label), continue
'size':
format_bytes(size), device_name = block.props.device
'file_system': if not device_name:
str(file_system), continue
'mount_point':
''.join([chr(ch) for ch in mount_point]), 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 return devices

View File

@ -1,4 +1,4 @@
{% extends "base.html" %} {% extends "service.html" %}
{% comment %} {% comment %}
# #
# This file is part of FreedomBox. # This file is part of FreedomBox.
@ -22,77 +22,33 @@
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% block content %} {% block status %}
<h2>{{ service.name }}</h2> {{ block.super }}
{% for paragraph in description %}
<p>{{ paragraph|safe }}</p>
{% endfor %}
{% if manual_page %}
<p class="manual-page">
<a href="{% url 'help:manual-page' manual_page %}">
{% trans 'Learn more...' %}
</a>
</p>
{% endif %}
<h3>{% trans "Devices" %}</h3> <h3>{% trans "Devices" %}</h3>
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Device" %}</th>
<th>{% trans "Label" %}</th>
<th>{% trans "Size" %}</th>
<th>{% trans "File System" %}</th>
<th>{% trans "Mount Point" %}</th>
</tr>
</thead>
<tbody>
{% for device in devices %}
<tr>
<td>{{ device.device }}</td>
<td>{{ device.label }}</td>
<td>{{ device.size }}</td>
<td>{{ device.file_system }}</td>
<td>{{ device.mount_point }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% if show_status_block %} <table class="table table-bordered table-condensed table-striped">
<h3>{% trans "Status" %}</h3> <thead>
<p class="running-status-parent"> <tr>
{% with service_name=service.name %} <th>{% trans "Device" %}</th>
{% if service.is_running %} <th>{% trans "Label" %}</th>
<span class="running-status active"></span> <th>{% trans "Size" %}</th>
{% blocktrans trimmed %} <th>{% trans "Filesystem" %}</th>
Service <em>{{ service_name }}</em> is running. <th>{% trans "Mount Point" %}</th>
{% endblocktrans %} </tr>
{% else %} </thead>
<span class="running-status inactive"></span> <tbody>
{% blocktrans trimmed %} {% for device in devices %}
Service <em>{{ service_name }}</em> is not running. <tr>
{% endblocktrans %} <td>{{ device.device }}</td>
{% endif %} <td>{{ device.label }}</td>
{% endwith %} <td>{{ device.size }}</td>
</p> <td>{{ device.filesystem_type }}</td>
{% endif %} <td>{{ device.mount_points|join:', ' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h3>{% trans "Configuration" %}</h3>
<form class="form form-configuration" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary"
value="{% trans "Update setup" %}"/>
</form>
{% endblock %} {% endblock %}

View File

@ -23,6 +23,7 @@ from plinth.views import ServiceView
class UdiskieView(ServiceView): class UdiskieView(ServiceView):
"""View to show devices."""
template_name = 'udiskie.html' template_name = 'udiskie.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):