Skip to content

Commit 872a7e3

Browse files
committed
Move to f-strings
These have been around long enough now. Fixes pylint C0209
1 parent 64079a5 commit 872a7e3

17 files changed

+102
-122
lines changed

dbusmock/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def parse_args():
106106
try:
107107
parameters = json.loads(args.parameters)
108108
except ValueError as detail:
109-
sys.stderr.write('Malformed JSON given for parameters: %s\n' % detail)
109+
sys.stderr.write(f'Malformed JSON given for parameters: {detail}\n')
110110
sys.exit(2)
111111

112112
if not isinstance(parameters, dict):

dbusmock/mockobject.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def _wrap_in_dbus_variant(value):
115115
return type(value)(value.conjugate(), variant_level=1)
116116
if isinstance(value, str):
117117
return dbus.String(value, variant_level=1)
118-
raise dbus.exceptions.DBusException(
119-
'could not wrap type {}'.format(type(value)))
118+
raise dbus.exceptions.DBusException(f'could not wrap type {type(value)}')
120119

121120

122121
class DBusMockObject(dbus.service.Object): # pylint: disable=too-many-instance-attributes
@@ -178,7 +177,7 @@ def _set_up_object_manager(self) -> None:
178177
if self.path == '/':
179178
cond = 'k != \'/\''
180179
else:
181-
cond = 'k.startswith(\'%s/\')' % self.path
180+
cond = f'k.startswith(\'{self.path}/\')'
182181

183182
self.AddMethod(OBJECT_MANAGER_IFACE,
184183
'GetManagedObjects', '', 'a{oa{sa{sv}}}',
@@ -201,7 +200,7 @@ def _reset(self, props: PropsType) -> None:
201200
def Get(self, interface_name: str, property_name: str) -> Any:
202201
'''Standard D-Bus API for getting a property value'''
203202

204-
self.log('Get %s.%s' % (interface_name, property_name))
203+
self.log(f'Get {interface_name}.{property_name}')
205204

206205
if not interface_name:
207206
interface_name = self.interface
@@ -233,9 +232,7 @@ def GetAll(self, interface_name: str, *_, **__) -> PropsType:
233232
def Set(self, interface_name: str, property_name: str, value: Any, *_, **__) -> None:
234233
'''Standard D-Bus API for setting a property value'''
235234

236-
self.log('Set %s.%s%s' % (interface_name,
237-
property_name,
238-
_format_args((value,))))
235+
self.log(f'Set {interface_name}.{property_name}{_format_args((value,))}')
239236

240237
try:
241238
iface_props = self.props[interface_name]
@@ -292,9 +289,7 @@ def AddObject(self, path: str, interface: str, properties: PropsType, methods: L
292289
])
293290
'''
294291
if path in objects:
295-
raise dbus.exceptions.DBusException(
296-
'object %s already exists' % path,
297-
name='org.freedesktop.DBus.Mock.NameError')
292+
raise dbus.exceptions.DBusException(f'object {path} already exists', name='org.freedesktop.DBus.Mock.NameError')
298293

299294
obj = DBusMockObject(self.bus_name,
300295
path,
@@ -322,7 +317,7 @@ def RemoveObject(self, path: str) -> None: # pylint: disable=no-self-use
322317
del objects[path]
323318
except KeyError as e:
324319
raise dbus.exceptions.DBusException(
325-
'object %s does not exist' % path,
320+
f'object {path} does not exist',
326321
name='org.freedesktop.DBus.Mock.NameError') from e
327322

328323
@dbus.service.method(MOCK_IFACE,
@@ -408,7 +403,7 @@ def AddMethod(self, interface, name: str, in_sig: str, out_sig: str, code: str)
408403
out_signature=out_sig)(method)
409404
dbus_method.__name__ = str(name)
410405
dbus_method._dbus_in_signature = in_sig
411-
dbus_method._dbus_args = ['arg%i' % i for i in range(1, n_args + 1)]
406+
dbus_method._dbus_args = [f'arg{i}' for i in range(1, n_args + 1)]
412407

413408
# for convenience, add mocked methods on the primary interface as
414409
# callable methods
@@ -458,9 +453,7 @@ def UpdateProperties(self, interface: str, properties: PropsType) -> None:
458453
if not interface:
459454
interface = self.interface
460455
if name not in self.props.get(interface, {}):
461-
raise dbus.exceptions.DBusException(
462-
'property %s not found' % name,
463-
name=interface + '.NoSuchProperty')
456+
raise dbus.exceptions.DBusException(f'property {name} not found', name=interface + '.NoSuchProperty')
464457

465458
self._set_property(interface, name, value)
466459
changed_props[name] = _wrap_in_dbus_variant(value)
@@ -483,9 +476,7 @@ def AddProperty(self, interface: str, name: str, value: Any) -> None:
483476
if not interface:
484477
interface = self.interface
485478
if name in self.props.get(interface, {}):
486-
raise dbus.exceptions.DBusException(
487-
'property %s already exists' % name,
488-
name=self.interface + '.PropertyExists')
479+
raise dbus.exceptions.DBusException(f'property {name} already exists', name=self.interface + '.PropertyExists')
489480

490481
self._set_property(interface, name, value)
491482

@@ -528,7 +519,7 @@ def AddTemplate(self, template: str, parameters: PropsType) -> None:
528519
try:
529520
module = load_module(template)
530521
except ImportError as e:
531-
raise dbus.exceptions.DBusException('Cannot add template %s: %s' % (template, str(e)),
522+
raise dbus.exceptions.DBusException(f'Cannot add template {template}: {str(e)}',
532523
name='org.freedesktop.DBus.Mock.TemplateError')
533524

534525
# If the template specifies this is an ObjectManager, set that up
@@ -585,11 +576,11 @@ def EmitSignal(self, interface: str, name: str, signature: str, args: List[Any])
585576
m.append(signature=signature, *args)
586577
args = m.get_args_list()
587578

588-
fn = lambda self, *args: self.log('emit %s.%s%s' % (interface, name, _format_args(args)))
579+
fn = lambda self, *args: self.log(f'emit {interface}.{name}{_format_args(args)}')
589580
fn.__name__ = str(name)
590581
dbus_fn = dbus.service.signal(interface)(fn)
591582
dbus_fn._dbus_signature = signature
592-
dbus_fn._dbus_args = ['arg%i' % i for i in range(1, len(args) + 1)]
583+
dbus_fn._dbus_args = [f'arg{i}' for i in range(1, len(args) + 1)]
593584

594585
dbus_fn(self, *args)
595586

@@ -692,7 +683,7 @@ def log(self, msg: str) -> None:
692683
else:
693684
fd = sys.stdout.fileno()
694685

695-
os.write(fd, ('%.3f %s\n' % (time.time(), msg)).encode('UTF-8'))
686+
os.write(fd, f'{time.time():.3f} {msg}\n'.encode('UTF-8'))
696687

697688
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
698689
in_signature='',
@@ -726,7 +717,7 @@ def Introspect(self, object_path: str, connection: dbus.connection.Connection) -
726717
# We might have properties for new interfaces we don't know about
727718
# yet. Try to find an existing <interface> node named after our
728719
# interface to append to, and create one if we can't.
729-
interface = tree.find(".//interface[@name='%s']" % name)
720+
interface = tree.find(f".//interface[@name='{name}']")
730721
if interface is None:
731722
interface = ElementTree.Element("interface", {"name": name})
732723
tree.append(interface)

dbusmock/templates/accounts_service.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
def get_user_path(uid):
30-
return '/org/freedesktop/Accounts/User{}'.format(uid)
30+
return f'/org/freedesktop/Accounts/User{uid}'
3131

3232

3333
def load(mock, parameters=None):
@@ -74,9 +74,9 @@ def AddUser(self, uid, username, password=DEFAULT_USER_PASSWORD,
7474
'AccountType': dbus.Int32(1),
7575
'AutomaticLogin': False,
7676
'BackgroundFile': '',
77-
'Email': '{}@python-dbusmock.org'.format(username),
77+
'Email': f'{username}@python-dbusmock.org',
7878
'FormatsLocale': 'C',
79-
'HomeDirectory': '/nonexisting/mock-home/{}'.format(username),
79+
'HomeDirectory': f'/nonexisting/mock-home/{username}',
8080
'IconFile': '',
8181
'InputSources': dbus.Array([], signature='a{ss}'),
8282
'Language': 'C',
@@ -164,9 +164,7 @@ def CreateUser(self, name, fullname, account_type):
164164
found = False
165165

166166
if found:
167-
raise dbus.exceptions.DBusException(
168-
'User {} already exists'.format(name),
169-
name='org.freedesktop.Accounts.Error.Failed')
167+
raise dbus.exceptions.DBusException(f'User {name} already exists', name='org.freedesktop.Accounts.Error.Failed')
170168

171169
self.users_auto_uids += 1
172170

@@ -231,9 +229,7 @@ def FindUserByName(self, username):
231229
[user_id] = [uid for uid, props in self.mock_users.items()
232230
if props['UserName'] == username]
233231
except ValueError as e:
234-
raise dbus.exceptions.DBusException(
235-
'No such user exists: {}'.format(e),
236-
name='org.freedesktop.Accounts.Error.Failed')
232+
raise dbus.exceptions.DBusException(f'No such user exists: {e}', name='org.freedesktop.Accounts.Error.Failed')
237233
return get_user_path(user_id)
238234

239235

dbusmock/templates/bluez5.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def AddDevice(self, adapter_device_name, device_address, alias):
205205

206206
if adapter_path not in mockobject.objects:
207207
raise dbus.exceptions.DBusException(
208-
'Adapter %s does not exist.' % adapter_device_name,
208+
f'Adapter {adapter_device_name} does not exist.',
209209
name=BLUEZ_MOCK_IFACE + '.NoSuchAdapter')
210210

211211
properties = {
@@ -270,12 +270,10 @@ def PairDevice(_self, adapter_device_name, device_address, class_=5898764):
270270

271271
if adapter_path not in mockobject.objects:
272272
raise dbus.exceptions.DBusException(
273-
'Adapter %s does not exist.' % adapter_device_name,
273+
f'Adapter {adapter_device_name} does not exist.',
274274
name=BLUEZ_MOCK_IFACE + '.NoSuchAdapter')
275275
if device_path not in mockobject.objects:
276-
raise dbus.exceptions.DBusException(
277-
'Device %s does not exist.' % device_name,
278-
name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
276+
raise dbus.exceptions.DBusException(f'Device {device_name} does not exist.', name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
279277

280278
device = mockobject.objects[device_path]
281279
device.paired = True
@@ -348,12 +346,10 @@ def BlockDevice(_self, adapter_device_name, device_address):
348346

349347
if adapter_path not in mockobject.objects:
350348
raise dbus.exceptions.DBusException(
351-
'Adapter %s does not exist.' % adapter_device_name,
349+
f'Adapter {adapter_device_name} does not exist.',
352350
name=BLUEZ_MOCK_IFACE + '.NoSuchAdapter')
353351
if device_path not in mockobject.objects:
354-
raise dbus.exceptions.DBusException(
355-
'Device %s does not exist.' % device_name,
356-
name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
352+
raise dbus.exceptions.DBusException(f'Device {device_name} does not exist.', name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
357353

358354
device = mockobject.objects[device_path]
359355

@@ -393,11 +389,11 @@ def ConnectDevice(_self, adapter_device_name, device_address):
393389

394390
if adapter_path not in mockobject.objects:
395391
raise dbus.exceptions.DBusException(
396-
'Adapter %s does not exist.' % adapter_device_name,
392+
f'Adapter {adapter_device_name} does not exist.',
397393
name=BLUEZ_MOCK_IFACE + '.NoSuchAdapter')
398394
if device_path not in mockobject.objects:
399395
raise dbus.exceptions.DBusException(
400-
'Device %s does not exist.' % device_name,
396+
f'Device {device_name} does not exist.',
401397
name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
402398

403399
device = mockobject.objects[device_path]
@@ -439,11 +435,11 @@ def DisconnectDevice(_self, adapter_device_name, device_address):
439435

440436
if adapter_path not in mockobject.objects:
441437
raise dbus.exceptions.DBusException(
442-
'Adapter %s does not exist.' % adapter_device_name,
438+
f'Adapter {adapter_device_name} does not exist.',
443439
name=BLUEZ_MOCK_IFACE + '.NoSuchAdapter')
444440
if device_path not in mockobject.objects:
445441
raise dbus.exceptions.DBusException(
446-
'Device %s does not exist.' % device_name,
442+
f'Device {device_name} does not exist.',
447443
name=BLUEZ_MOCK_IFACE + '.NoSuchDevice')
448444

449445
device = mockobject.objects[device_path]

dbusmock/templates/iio-sensors-proxy.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ def SetInternalProperty(self, interface, property_name, value):
177177
owners = None
178178
if is_valid_sensor_for_interface(sensor, interface):
179179

180-
if not getattr(self, 'has_{}'.format(sensor)):
181-
raise Exception('No {} sensor available'.format(sensor))
180+
if not getattr(self, f'has_{sensor}'):
181+
raise Exception(f'No {sensor} sensor available')
182182

183-
owners = getattr(self, '{}_owners'.format(sensor))
183+
owners = getattr(self, f'{sensor}_owners')
184184
# We allow setting a property from any client here, even if not claiming
185185
# but only owners, if any, will be notified about sensors changes
186186

dbusmock/templates/logind.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def load(mock, parameters):
3434
('Hibernate', 'b', '', ''),
3535
('HybridSleep', 'b', '', ''),
3636
('SuspendThenHibernate', 'b', '', ''),
37-
('CanPowerOff', '', 's', 'ret = "%s"' % parameters.get('CanPowerOff', 'yes')),
38-
('CanReboot', '', 's', 'ret = "%s"' % parameters.get('CanReboot', 'yes')),
39-
('CanSuspend', '', 's', 'ret = "%s"' % parameters.get('CanSuspend', 'yes')),
40-
('CanHibernate', '', 's', 'ret = "%s"' % parameters.get('CanHibernate', 'yes')),
41-
('CanHybridSleep', '', 's', 'ret = "%s"' % parameters.get('CanHybridSleep', 'yes')),
42-
('CanSuspendThenHibernate', '', 's', 'ret = "%s"' % parameters.get('CanSuspendThenHibernate', 'yes')),
37+
('CanPowerOff', '', 's', f'ret = "{parameters.get("CanPowerOff", "yes")}"'),
38+
('CanReboot', '', 's', f'ret = "{parameters.get("CanReboot", "yes")}"'),
39+
('CanSuspend', '', 's', f'ret = "{parameters.get("CanSuspend", "yes")}"'),
40+
('CanHibernate', '', 's', f'ret = "{parameters.get("CanHibernate", "yes")}"'),
41+
('CanHybridSleep', '', 's', f'ret = "{parameters.get("CanHybridSleep", "yes")}"'),
42+
('CanSuspendThenHibernate', '', 's', f'ret = "{parameters.get("CanSuspendThenHibernate", "yes")}"'),
4343

4444
('GetSession', 's', 'o', 'ret = "/org/freedesktop/login1/session/" + args[0]'),
4545
('ActivateSession', 's', '', ''),
@@ -56,7 +56,7 @@ def load(mock, parameters):
5656
('ListSeats', '', 'a(so)', 'ret = [(k.split("/")[-1], k) for k in objects.keys() if "/seat/" in k]'),
5757
('TerminateSeat', 's', '', ''),
5858

59-
('GetUser', 'u', 'o', 'ret = "/org/freedesktop/login1/user/%u" % args[0]'),
59+
('GetUser', 'u', 'o', 'ret = "/org/freedesktop/login1/user/" + args[0]'),
6060
('KillUser', 'us', '', ''),
6161
('TerminateUser', 'u', '', ''),
6262
])
@@ -152,8 +152,7 @@ def AddSeat(self, seat):
152152
'''
153153
seat_path = '/org/freedesktop/login1/seat/' + seat
154154
if seat_path in mockobject.objects:
155-
raise dbus.exceptions.DBusException('Seat %s already exists' % seat,
156-
name=MOCK_IFACE + '.SeatExists')
155+
raise dbus.exceptions.DBusException(f'Seat {seat} already exists', name=MOCK_IFACE + '.SeatExists')
157156

158157
self.AddObject(seat_path,
159158
'org.freedesktop.login1.Seat',
@@ -183,10 +182,9 @@ def AddUser(self, uid, username, active):
183182
184183
Return the object path of the new user.
185184
'''
186-
user_path = '/org/freedesktop/login1/user/%i' % uid
185+
user_path = f'/org/freedesktop/login1/user/{uid}'
187186
if user_path in mockobject.objects:
188-
raise dbus.exceptions.DBusException('User %i already exists' % uid,
189-
name=MOCK_IFACE + '.UserExists')
187+
raise dbus.exceptions.DBusException(f'User {uid} already exists', name=MOCK_IFACE + '.UserExists')
190188

191189
self.AddObject(user_path,
192190
'org.freedesktop.login1.User',
@@ -195,7 +193,7 @@ def AddUser(self, uid, username, active):
195193
'IdleHint': False,
196194
'DefaultControlGroup': 'systemd:/user/' + username,
197195
'Name': username,
198-
'RuntimePath': '/run/user/%i' % uid,
196+
'RuntimePath': f'/run/user/{uid}',
199197
'Service': '',
200198
'State': (active and 'active' or 'online'),
201199
'Display': ('', dbus.ObjectPath('/')),
@@ -223,17 +221,17 @@ def AddSession(self, session_id, seat, uid, username, active):
223221
224222
Return the object path of the new session.
225223
'''
226-
seat_path = dbus.ObjectPath('/org/freedesktop/login1/seat/' + seat)
224+
seat_path = dbus.ObjectPath(f'/org/freedesktop/login1/seat/{seat}')
227225
if seat_path not in mockobject.objects:
228226
self.AddSeat(seat)
229227

230-
user_path = dbus.ObjectPath('/org/freedesktop/login1/user/%i' % uid)
228+
user_path = dbus.ObjectPath(f'/org/freedesktop/login1/user/{uid}')
231229
if user_path not in mockobject.objects:
232230
self.AddUser(uid, username, active)
233231

234-
session_path = dbus.ObjectPath('/org/freedesktop/login1/session/' + session_id)
232+
session_path = dbus.ObjectPath(f'/org/freedesktop/login1/session/{session_id}')
235233
if session_path in mockobject.objects:
236-
raise dbus.exceptions.DBusException('Session %s already exists' % session_id,
234+
raise dbus.exceptions.DBusException(f'Session {session_id} already exists',
237235
name=MOCK_IFACE + '.SessionExists')
238236

239237
self.AddObject(session_path,
@@ -246,7 +244,7 @@ def AddSession(self, session_id, seat, uid, username, active):
246244
'KillProcesses': False,
247245
'Remote': False,
248246
'Class': 'user',
249-
'DefaultControlGroup': 'systemd:/user/%s/%s' % (username, session_id),
247+
'DefaultControlGroup': f'systemd:/user/{username}/{session_id}',
250248
'Display': os.getenv('DISPLAY', ''),
251249
'Id': session_id,
252250
'Name': username,

0 commit comments

Comments
 (0)