Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init(self):
config = self.config
options = self.options
mounter = udiskie.mount.Mounter(
self.udisks,
config=config.device_config)
if options['']:
devices = [self.udisks.find(path) for path in options['']]
else:
devices = mounter.get_all_handleable()
DeviceFilter = udiskie.config.DeviceFilter
output = options['output']
# old behaviour: single attribute
if output in DeviceFilter.VALID_PARAMETERS:
def format_output(device):
return getattr(device, output)
# new behaviour: format string
else:
def format_output(device):
view = ObjDictView(device, DeviceFilter.VALID_PARAMETERS)
return output.format_map(view)
filters = [_parse_filter(spec) for spec in options['filter']]
matcher = DeviceFilter(dict(filters))
for device in devices:
if matcher.match(device):
"""Create the launcher object from the command line template."""
if isinstance(argv, str):
self.argv = shlex.split(argv)
else:
self.argv = argv
self.extra = extra.copy()
# obtain a list of used fields names
formatter = string.Formatter()
self.used_attrs = set()
for arg in self.argv:
for text, kwd, spec, conv in formatter.parse(arg):
if kwd is None:
continue
if kwd in DeviceFilter.VALID_PARAMETERS:
self.used_attrs.add(kwd)
if kwd not in DeviceFilter.VALID_PARAMETERS and \
kwd not in self.extra:
self.extra[kwd] = None
logging.getLogger(__name__).error(_(
'Unknown device attribute {!r} in format string: {!r}',
kwd, arg))
def __init__(self, argv, **extra):
"""Create the launcher object from the command line template."""
if isinstance(argv, str):
self.argv = shlex.split(argv)
else:
self.argv = argv
self.extra = extra.copy()
# obtain a list of used fields names
formatter = string.Formatter()
self.used_attrs = set()
for arg in self.argv:
for text, kwd, spec, conv in formatter.parse(arg):
if kwd is None:
continue
if kwd in DeviceFilter.VALID_PARAMETERS:
self.used_attrs.add(kwd)
if kwd not in DeviceFilter.VALID_PARAMETERS and \
kwd not in self.extra:
self.extra[kwd] = None
logging.getLogger(__name__).error(_(
'Unknown device attribute {!r} in format string: {!r}',
kwd, arg))
def device_config(self):
device_config = map(DeviceFilter, self._data.get('device_config', []))
mount_options = map(MountOptions, self._data.get('mount_options', []))
ignore_device = map(IgnoreDevice, self._data.get('ignore_device', []))
return list(device_config) + list(mount_options) + list(ignore_device)
"""
self._log.debug(_('{0} matched {1}',
device.device_file or device.object_path, self))
return self._values[kind]
class MountOptions(DeviceFilter):
"""Associate a list of mount options to matched devices."""
def __init__(self, config_item):
config_item.setdefault('options', None)
super().__init__(config_item)
class IgnoreDevice(DeviceFilter):
"""Associate a boolean ignore flag to matched devices."""
def __init__(self, config_item):
config_item.setdefault('ignore', True)
super().__init__(config_item)
def match_config(filters, device, kind, default):
"""
Matches devices against multiple :class:`DeviceFilter`s.
:param list filters: device filters
:param Device device: device to be mounted
:param str kind: value kind
:param default: default value
def has_value(self, kind):
return kind in self._values
def value(self, kind, device):
"""
Get the value for the device object associated with this filter.
If :meth:`match` is False for the device, the return value of this
method is undefined.
"""
self._log.debug(_('{0} matched {1}',
device.device_file or device.object_path, self))
return self._values[kind]
class MountOptions(DeviceFilter):
"""Associate a list of mount options to matched devices."""
def __init__(self, config_item):
config_item.setdefault('options', None)
super().__init__(config_item)
class IgnoreDevice(DeviceFilter):
"""Associate a boolean ignore flag to matched devices."""
def __init__(self, config_item):
config_item.setdefault('ignore', True)
super().__init__(config_item)