/usr/lib/python3/dist-packages/udiskie/common.py is in udiskie 1.7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | """
Common DBus utilities.
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import os.path
import traceback
from .compat import fix_str_conversions
__all__ = [
'wraps',
'check_call',
'Emitter',
'samefile',
'sameuuid',
'setdefault',
'extend',
'cachedproperty',
'decode_ay',
# dealing with py2 strings:
'str2unicode',
'exc_message',
'format_exc',
]
try:
from black_magic.decorator import wraps
except ImportError:
from functools import wraps
def check_call(exc_type, func, *args):
try:
func(*args)
return True
except exc_type:
return False
class Emitter(object):
"""
Event emitter class.
Provides a simple event engine featuring a known finite set of events.
"""
def __init__(self, event_names=(), *args, **kwargs):
"""
Initialize with empty lists of event handlers.
:param iterable event_names: names of known events.
"""
super(Emitter, self).__init__(*args, **kwargs)
self._event_handlers = {}
for evt in event_names:
self._event_handlers[evt] = []
def trigger(self, event, *args):
"""
Trigger event handlers.
:param str event: event name
:param *args: event parameters
"""
for handler in self._event_handlers[event]:
handler(*args)
def connect(self, event, handler):
"""
Connect an event handler.
:param str event: event name
:param callable handler: event handler
"""
self._event_handlers[event].append(handler)
def disconnect(self, event, handler):
"""
Disconnect an event handler.
:param str event: event name
:param callable handler: event handler
"""
self._event_handlers[event].remove(handler)
def samefile(a, b):
"""Check if two pathes represent the same file."""
try:
return os.path.samefile(a, b)
except OSError:
return os.path.normpath(a) == os.path.normpath(b)
def sameuuid(a, b):
"""Compare two UUIDs."""
return a and b and a.lower() == b.lower()
def setdefault(self, other):
"""
Merge two dictionaries like .update() but don't overwrite values.
:param dict self: updated dict
:param dict other: default values to be inserted
"""
for k, v in other.items():
self.setdefault(k, v)
def extend(a, b):
"""Merge two dicts and return a new dict. Much like subclassing works."""
res = a.copy()
res.update(b)
return res
def cachedproperty(func):
"""A memoize decorator for class properties."""
key = '_' + func.__name__
@wraps(func)
def get(self):
try:
return getattr(self, key)
except AttributeError:
val = func(self)
setattr(self, key, val)
return val
return property(get)
# ----------------------------------------
# udisks.Device helper classes
# ----------------------------------------
class AttrDictView(object):
"""Provide attribute access view to a dictionary."""
def __init__(self, data):
self.__data = data
def __getattr__(self, key):
try:
return self.__data[key]
except KeyError:
raise AttributeError
class ObjDictView(object):
"""Provide dict-like access view to the attributes of an object."""
def __init__(self, object, valid=None):
self._object = object
self._valid = valid
def __getitem__(self, key):
if self._valid is None or key in self._valid:
try:
return getattr(self._object, key)
except AttributeError:
raise KeyError(key)
raise KeyError("Unknown key: {}".format(key))
@fix_str_conversions
class BaseDevice(object):
def __str__(self):
"""Show as object_path."""
return self.object_path
def __eq__(self, other):
"""Comparison by object_path."""
return self.object_path == str(other)
def __ne__(self, other):
"""Comparison by object_path."""
return not (self == other)
def is_file(self, path):
"""Comparison by mount and device file path."""
return (samefile(path, self.device_file) or
samefile(path, self.loop_file) or
any(samefile(path, mp) for mp in self.mount_paths) or
sameuuid(path, self.id_uuid) or
sameuuid(path, self.partition_uuid))
# ----------------------------------------
# derived properties
# ----------------------------------------
@property
def mount_path(self):
"""Return any mount path."""
try:
return self.mount_paths[0]
except IndexError:
return ''
@property
def in_use(self):
"""Check whether this device is in use, i.e. mounted or unlocked."""
if self.is_mounted or self.is_unlocked:
return True
if self.is_partition_table:
for device in self._daemon:
if device.partition_slave == self and device.in_use:
return True
return False
@property
def ui_id_label(self):
"""Label of the unlocked partition or the device itself."""
return (self.luks_cleartext_holder or self).id_label
@property
def ui_id_uuid(self):
"""UUID of the unlocked partition or the device itself."""
return (self.luks_cleartext_holder or self).id_uuid
@property
def ui_device_presentation(self):
"""Path of the crypto backing device or the device itself."""
return (self.luks_cleartext_slave or self).device_presentation
@property
def ui_label(self):
"""UI string identifying the partition if possible."""
return ': '.join(filter(None, [
self.ui_device_presentation,
self.ui_id_label or self.ui_id_uuid or self.drive_label
]))
@property
def ui_device_label(self):
"""UI string identifying the device (drive) if toplevel."""
return ': '.join(filter(None, [
self.ui_device_presentation,
self.loop_file or
self.drive_label or self.ui_id_label or self.ui_id_uuid
]))
@property
def drive_label(self):
"""Return drive label."""
return ' '.join(filter(None, [
self.drive_vendor,
self.drive_model,
]))
@fix_str_conversions
class NullDevice(object):
"""
Invalid object.
Evaluates to False in boolean context, but allows arbitrary attribute
access by returning another Null.
"""
object_path = '/'
def __init__(self, **properties):
"""Initialize an instance with the given DBus proxy object."""
self.__dict__.update(properties)
def __bool__(self):
return False
__nonzero__ = __bool__
def __str__(self):
"""Display as object path."""
return self.object_path
def __eq__(self, other):
"""Comparison by object path."""
return self.object_path == str(other)
def __ne__(self, other):
"""Comparison by object path."""
return not (self == other)
def is_file(self, path):
"""Comparison by mount and device file path."""
return False
# availability of interfaces
is_drive = False
is_block = False
is_partition_table = False
is_partition = False
is_filesystem = False
is_luks = False
is_loop = False
# Drive
is_toplevel = is_drive
is_detachable = False
is_ejectable = False
has_media = False
def eject(self, unmount=False):
raise RuntimeError("Cannot call methods on invalid device!")
def detach(self):
raise RuntimeError("Cannot call methods on invalid device!")
# Block
device_file = ''
device_presentation = ''
device_size = 0
id_usage = ''
is_crypto = False
is_ignored = None
device_id = ''
id_type = ''
id_label = ''
id_uuid = ''
@property
def luks_cleartext_slave(self):
raise AttributeError('Invalid device has no cleartext slave.')
is_luks_cleartext = False
is_external = None
is_systeminternal = None
@property
def drive(self):
raise AttributeError('Invalid device has no drive.')
root = drive
should_automount = False
icon_name = ''
symbolic_icon_name = icon_name
symlinks = []
# Partition
@property
def partition_slave(self):
raise AttributeError('Invalid device has no partition slave.')
# Filesystem
is_mounted = False
mount_paths = ()
def mount(self,
fstype=None,
options=None,
auth_no_user_interaction=False):
raise RuntimeError("Cannot call methods on invalid device!")
def unmount(self, force=False):
raise RuntimeError("Cannot call methods on invalid device!")
# Encrypted
@property
def luks_cleartext_holder(self):
raise AttributeError('Invalid device has no cleartext holder.')
is_unlocked = None
def unlock(self, password):
raise RuntimeError("Cannot call methods on invalid device!")
def lock(self):
raise RuntimeError("Cannot call methods on invalid device!")
# Loop
loop_file = ''
setup_by_uid = -1
autoclear = None
def delete(self, auth_no_user_interaction=None):
raise RuntimeError("Cannot call methods on invalid device!")
def set_autoclear(self, value, auth_no_user_interaction=None):
raise RuntimeError("Cannot call methods on invalid device!")
loop_support = False
# derived properties
mount_path = ''
in_use = False
ui_id_label = ''
ui_id_uuid = ''
ui_device_presentation = ''
ui_label = '(invalid device)'
ui_device_label = ''
drive_label = ''
parent_object_path = '/'
can_add = False
can_remove = False
class DaemonBase(object):
active = False
def activate(self):
udisks = self._mounter.udisks
for event, handler in self.events.items():
udisks.connect(event, handler)
self.active = True
def deactivate(self):
udisks = self._mounter.udisks
for event, handler in self.events.items():
udisks.disconnect(event, handler)
self.active = False
# ----------------------------------------
# byte array to string conversion
# ----------------------------------------
try:
unicode
except NameError:
unicode = str
def decode_ay(ay):
"""Convert binary blob from DBus queries to strings."""
if ay is None:
return ''
elif isinstance(ay, unicode):
return ay
elif isinstance(ay, bytes):
return ay.decode('utf-8')
else:
# dbus.Array([dbus.Byte]) or any similar sequence type:
return bytearray(ay).rstrip(bytearray((0,))).decode('utf-8')
def str2unicode(arg):
"""Decode python2 strings (bytes) to unicode."""
if isinstance(arg, list):
return [str2unicode(s) for s in arg]
if isinstance(arg, bytes):
return arg.decode('utf-8')
return arg
def exc_message(exc):
"""Get an exception message."""
message = getattr(exc, 'message', None)
return str2unicode(message or str(exc))
def format_exc():
return str2unicode(traceback.format_exc())
|