Convert formatting automatically to f-strings

Automatically convert formatting with the following call:
flynt -ll 2000 -v frappy*
Result: 303/381 auto-converted.
Failing conversions will be looked at manually in a follow-up commit.

Change-Id: Icd996b27221202faccc15af78e0380cf52ee37f2
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30900
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Georg Brandl <g.brandl@fz-juelich.de>
Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de>
This commit is contained in:
Alexander Zaft
2023-04-14 07:12:03 +02:00
parent c114dbab26
commit 34183453e0
56 changed files with 327 additions and 380 deletions

View File

@@ -53,10 +53,10 @@ from frappy.protocol.messages import COMMANDREPLY, DESCRIPTIONREPLY, \
def make_update(modulename, pobj):
if pobj.readerror:
return (ERRORPREFIX + EVENTREPLY, '%s:%s' % (modulename, pobj.export),
return (ERRORPREFIX + EVENTREPLY, f'{modulename}:{pobj.export}',
# error-report !
[pobj.readerror.name, str(pobj.readerror), {'t': pobj.timestamp}])
return (EVENTREPLY, '%s:%s' % (modulename, pobj.export),
return (EVENTREPLY, f'{modulename}:{pobj.export}',
[pobj.export_value(), {'t': pobj.timestamp}])
@@ -114,7 +114,7 @@ class Dispatcher:
if ':' not in eventname:
# also remove 'more specific' subscriptions
for k, v in self._subscriptions.items():
if k.startswith('%s:' % eventname):
if k.startswith(f'{eventname}:'):
v.discard(conn)
if eventname in self._subscriptions:
self._subscriptions[eventname].discard(conn)
@@ -151,7 +151,7 @@ class Dispatcher:
return self._modules[modulename]
if modulename in list(self._modules.values()):
return modulename
raise NoSuchModuleError('Module %r does not exist on this SEC-Node!' % modulename)
raise NoSuchModuleError(f'Module {modulename!r} does not exist on this SEC-Node!')
def remove_module(self, modulename_or_obj):
moduleobj = self.get_module(modulename_or_obj)
@@ -160,7 +160,7 @@ class Dispatcher:
self._export.remove(modulename)
self._modules.pop(modulename)
self._subscriptions.pop(modulename, None)
for k in [kk for kk in self._subscriptions if kk.startswith('%s:' % modulename)]:
for k in [kk for kk in self._subscriptions if kk.startswith(f'{modulename}:')]:
self._subscriptions.pop(k, None)
def list_module_names(self):
@@ -201,25 +201,25 @@ class Dispatcher:
# command is also accepted
result = result['accessibles'][pname]
elif pname:
raise NoSuchParameterError('Module %r has no parameter %r' % (modname, pname))
raise NoSuchParameterError(f'Module {modname!r} has no parameter {pname!r}')
elif not modname or modname == '.':
result['equipment_id'] = self.equipment_id
result['firmware'] = 'FRAPPY - The Python Framework for SECoP'
result['version'] = '2021.02'
result.update(self.nodeprops)
else:
raise NoSuchModuleError('Module %r does not exist' % modname)
raise NoSuchModuleError(f'Module {modname!r} does not exist')
return result
def _execute_command(self, modulename, exportedname, argument=None):
moduleobj = self.get_module(modulename)
if moduleobj is None:
raise NoSuchModuleError('Module %r does not exist' % modulename)
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
cname = moduleobj.accessiblename2attr.get(exportedname)
cobj = moduleobj.commands.get(cname)
if cobj is None:
raise NoSuchCommandError('Module %r has no command %r' % (modulename, cname or exportedname))
raise NoSuchCommandError(f'Module {modulename!r} has no command {cname or exportedname!r}')
if cobj.argument:
argument = cobj.argument.import_value(argument)
@@ -233,18 +233,16 @@ class Dispatcher:
def _setParameterValue(self, modulename, exportedname, value):
moduleobj = self.get_module(modulename)
if moduleobj is None:
raise NoSuchModuleError('Module %r does not exist' % modulename)
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
pname = moduleobj.accessiblename2attr.get(exportedname)
pobj = moduleobj.parameters.get(pname)
if pobj is None:
raise NoSuchParameterError('Module %r has no parameter %r' % (modulename, pname or exportedname))
raise NoSuchParameterError(f'Module {modulename!r} has no parameter {pname or exportedname!r}')
if pobj.constant is not None:
raise ReadOnlyError("Parameter %s:%s is constant and can not be changed remotely"
% (modulename, pname))
raise ReadOnlyError(f"Parameter {modulename}:{pname} is constant and can not be changed remotely")
if pobj.readonly:
raise ReadOnlyError("Parameter %s:%s can not be changed remotely"
% (modulename, pname))
raise ReadOnlyError(f"Parameter {modulename}:{pname} can not be changed remotely")
# validate!
value = pobj.datatype.validate(value, previous=pobj.value)
@@ -256,12 +254,12 @@ class Dispatcher:
def _getParameterValue(self, modulename, exportedname):
moduleobj = self.get_module(modulename)
if moduleobj is None:
raise NoSuchModuleError('Module %r does not exist' % modulename)
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
pname = moduleobj.accessiblename2attr.get(exportedname)
pobj = moduleobj.parameters.get(pname)
if pobj is None:
raise NoSuchParameterError('Module %r has no parameter %r' % (modulename, pname or exportedname))
raise NoSuchParameterError(f'Module {modulename!r} has no parameter {pname or exportedname!r}')
if pobj.constant is not None:
# really needed? we could just construct a readreply instead....
# raise ReadOnlyError('This parameter is constant and can not be accessed remotely.')
@@ -293,11 +291,11 @@ class Dispatcher:
action, specifier, data = '_ident', None, None
self.log.debug('Looking for handle_%s', action)
handler = getattr(self, 'handle_%s' % action, None)
handler = getattr(self, f'handle_{action}', None)
if handler:
return handler(conn, specifier, data)
raise ProtocolError('unhandled message: %s' % repr(msg))
raise ProtocolError(f'unhandled message: {repr(msg)}')
# now the (defined) handlers for the different requests
def handle_help(self, conn, specifier, data):
@@ -351,13 +349,13 @@ class Dispatcher:
if ':' in specifier:
modulename, exportedname = specifier.split(':', 1)
if modulename not in self._export:
raise NoSuchModuleError('Module %r does not exist' % modulename)
raise NoSuchModuleError(f'Module {modulename!r} does not exist')
moduleobj = self.get_module(modulename)
if exportedname is not None:
pname = moduleobj.accessiblename2attr.get(exportedname, True)
if pname and pname not in moduleobj.accessibles:
# what if we try to subscribe a command here ???
raise NoSuchParameterError('Module %r has no parameter %r' % (modulename, pname))
raise NoSuchParameterError(f'Module {modulename!r} has no parameter {pname!r}')
modules = [(modulename, pname)]
else:
modules = [(modulename, None)]
@@ -392,7 +390,7 @@ class Dispatcher:
def send_log_msg(self, conn, modname, level, msg):
"""send log message """
conn.send_reply((LOG_EVENT, '%s:%s' % (modname, level), msg))
conn.send_reply((LOG_EVENT, f'{modname}:{level}', msg))
def set_all_log_levels(self, conn, level):
for modobj in self._modules.values():

View File

@@ -88,17 +88,14 @@ REQUEST2REPLY = {
}
HelpMessage = """Try one of the following:
'%s' to query protocol version
'%s' to read the description
'%s <module>[:<parameter>]' to request reading a value
'%s <module>[:<parameter>] value' to request changing a value
'%s <module>[:<command>]' to execute a command
'%s <nonce>' to request a heartbeat response
'%s' to activate async updates
'%s' to deactivate updates
'%s [<module>] <loglevel>' to activate logging events
""" % (IDENTREQUEST, DESCRIPTIONREQUEST, READREQUEST,
WRITEREQUEST, COMMANDREQUEST, HEARTBEATREQUEST,
ENABLEEVENTSREQUEST, DISABLEEVENTSREQUEST,
LOGGING_REQUEST)
HelpMessage = f"""Try one of the following:
'{IDENTREQUEST}' to query protocol version
'{DESCRIPTIONREQUEST}' to read the description
'{READREQUEST} <module>[:<parameter>]' to request reading a value
'{WRITEREQUEST} <module>[:<parameter>] value' to request changing a value
'{COMMANDREQUEST} <module>[:<command>]' to execute a command
'{HEARTBEATREQUEST} <nonce>' to request a heartbeat response
'{ENABLEEVENTSREQUEST}' to activate async updates
'{DISABLEEVENTSREQUEST}' to deactivate updates
'{LOGGING_REQUEST} [<module>] <loglevel>' to activate logging events
"""

View File

@@ -55,11 +55,11 @@ class SecopClient(frappy.client.SecopClient):
return name
def updateEvent(self, module, parameter, value, timestamp, readerror):
specifier = '%s:%s' % (module, parameter)
specifier = f'{module}:{parameter}'
if readerror:
msg = ERRORPREFIX + EVENTREPLY, specifier, (readerror.name, str(readerror), dict(t=timestamp))
msg = ERRORPREFIX + EVENTREPLY, specifier, (readerror.name, str(readerror), {'t': timestamp})
else:
msg = EVENTREPLY, specifier, (value, dict(t=timestamp))
msg = EVENTREPLY, specifier, (value, {'t': timestamp})
self.dispatcher.broadcast_event(msg)
def nodeStateChange(self, online, state):
@@ -74,7 +74,7 @@ class SecopClient(frappy.client.SecopClient):
if module is None:
self.dispatcher.restart()
self._shutdown = True
raise frappy.errors.SECoPError('descriptive data for node %r has changed' % self.nodename)
raise frappy.errors.SECoPError(f'descriptive data for node {self.nodename!r} has changed')
class Router(frappy.protocol.dispatcher.Dispatcher):
@@ -139,7 +139,7 @@ class Router(frappy.protocol.dispatcher.Dispatcher):
data = node.descriptive_data.copy()
modules = data.pop('modules')
equipment_id = data.pop('equipment_id', 'unknown')
node_description.append('--- %s ---\n%s' % (equipment_id, data.pop('description', '')))
node_description.append(f"--- {equipment_id} ---\n{data.pop('description', '')}")
node_description.append('\n'.join('%s: %r' % kv for kv in data.items()))
for modname, moddesc in modules.items():
if modname in allmodules:
@@ -154,12 +154,12 @@ class Router(frappy.protocol.dispatcher.Dispatcher):
super().handle_activate(conn, specifier, data)
for node in self.nodes:
for (module, parameter), (value, t, readerror) in node.cache.items():
spec = '%s:%s' % (module, parameter)
spec = f'{module}:{parameter}'
if readerror:
reply = ERRORPREFIX + EVENTREPLY, spec, (readerror.name, str(readerror), dict(t=t))
reply = ERRORPREFIX + EVENTREPLY, spec, (readerror.name, str(readerror), {'t': t})
else:
datatype = node.modules[module]['parameters'][parameter]['datatype']
reply = EVENTREPLY, spec, [datatype.export_value(value), dict(t=t)]
reply = EVENTREPLY, spec, [datatype.export_value(value), {'t': t}]
self.broadcast_event(reply)
return ENABLEEVENTSREPLY, None, None
@@ -175,7 +175,7 @@ class Router(frappy.protocol.dispatcher.Dispatcher):
node = self.node_by_module[module]
if node.online:
return node.request(READREQUEST, specifier, data)
return ERRORPREFIX + READREQUEST, specifier, SecopClient.disconnectedError + (dict(t=node.disconnect_time),)
return ERRORPREFIX + READREQUEST, specifier, SecopClient.disconnectedError + ({'t': node.disconnect_time},)
def handle_change(self, conn, specifier, data):
module = specifier.split(':')[0]