From 1aeade52fbdb100042ec2bbb9bd29ff19f9e5b19 Mon Sep 17 00:00:00 2001 From: Blai Peidro Date: Sat, 12 Sep 2026 02:50:11 +0200 Subject: [PATCH] refactor: convert printf-style formats to f-strings UP031 to reach .format(), then f-strings at those sites only, so this does not overlap the str.format conversion in #41. Four are left as % because their argument is a float that %d truncates and {:d} rejects. Formatted with black, which is what CI runs today. --- ascenderkit/api/pages/credentials.py | 4 ++-- ascenderkit/api/pages/inventory.py | 19 +++++++++---------- ascenderkit/api/pages/job_templates.py | 9 ++++----- .../api/pages/notification_templates.py | 7 +++---- ascenderkit/api/pages/page.py | 2 +- ascenderkit/api/pages/projects.py | 11 +++++------ ascenderkit/api/pages/system_job_templates.py | 5 +---- ascenderkit/ascender/inventory.py | 14 +++++++------- ascenderkit/cli/client.py | 2 +- ascenderkit/cli/utils.py | 2 +- ascenderkit/utils/__init__.py | 4 ++-- ascenderkit/yaml_file.py | 2 +- 12 files changed, 37 insertions(+), 44 deletions(-) diff --git a/ascenderkit/api/pages/credentials.py b/ascenderkit/api/pages/credentials.py index 648ad4c..45b032a 100644 --- a/ascenderkit/api/pages/credentials.py +++ b/ascenderkit/api/pages/credentials.py @@ -169,7 +169,7 @@ def test(self, data): """Test the credential type endpoint.""" response = self.connection.post(urljoin(str(self.url), 'test/'), data) exception = exception_from_status_code(response.status_code) - exc_str = "%s (%s) received" % (http.responses[response.status_code], response.status_code) + exc_str = f"{http.responses[response.status_code]} ({response.status_code}) received" if exception: raise exception(exc_str, response.json()) elif response.status_code == http.FORBIDDEN: @@ -266,7 +266,7 @@ def test(self, data): """Test the credential endpoint.""" response = self.connection.post(urljoin(str(self.url), 'test/'), data) exception = exception_from_status_code(response.status_code) - exc_str = "%s (%s) received" % (http.responses[response.status_code], response.status_code) + exc_str = f"{http.responses[response.status_code]} ({response.status_code}) received" if exception: raise exception(exc_str, response.json()) elif response.status_code == http.FORBIDDEN: diff --git a/ascenderkit/api/pages/inventory.py b/ascenderkit/api/pages/inventory.py index 683b618..54758ed 100644 --- a/ascenderkit/api/pages/inventory.py +++ b/ascenderkit/api/pages/inventory.py @@ -28,7 +28,7 @@ def print_ini(self): continue # output host groups - output.append('[%s]' % group) + output.append(f'[{group}]') for host in inv_dict[group].get('hosts', []): # FIXME ... include hostvars output.append(host) @@ -36,16 +36,16 @@ def print_ini(self): # output child groups if inv_dict[group].get('children', []): - output.append('[%s:children]' % group) + output.append(f'[{group}:children]') for child in inv_dict[group].get('children', []): output.append(child) output.append('') # newline # output group vars if inv_dict[group].get('vars', {}).items(): - output.append('[%s:vars]' % group) + output.append(f'[{group}:vars]') for k, v in inv_dict[group].get('vars', {}).items(): - output.append('%s=%s' % (k, v)) + output.append(f'{k}={v}') output.append('') # newline print('\n'.join(output)) @@ -371,20 +371,19 @@ def update(self): update_pg = self.get_related('update') # assert can_update == True - assert update_pg.can_update, "The specified inventory_source (id:%s) is not able to update (can_update:%s)" % (self.id, update_pg.can_update) + assert update_pg.can_update, f"The specified inventory_source (id:{self.id}) is not able to update (can_update:{update_pg.can_update})" # start the inventory_update result = update_pg.post() # assert JSON response - assert 'inventory_update' in result.json, "Unexpected JSON response when starting an inventory_update.\n%s" % json.dumps(result.json, indent=2) + assert 'inventory_update' in result.json, f"Unexpected JSON response when starting an inventory_update.\n{json.dumps(result.json, indent=2)}" # locate and return the inventory_update jobs_pg = self.related.inventory_updates.get(id=result.json['inventory_update']) - assert jobs_pg.count == 1, "An inventory_update started (id:%s) but job not found in response at %s/inventory_updates/" % ( - result.json['inventory_update'], - self.url, - ) + assert ( + jobs_pg.count == 1 + ), f"An inventory_update started (id:{result.json['inventory_update']}) but job not found in response at {self.url}/inventory_updates/" return jobs_pg.results[0] @property diff --git a/ascenderkit/api/pages/job_templates.py b/ascenderkit/api/pages/job_templates.py index 00e53d2..b546fdc 100644 --- a/ascenderkit/api/pages/job_templates.py +++ b/ascenderkit/api/pages/job_templates.py @@ -26,14 +26,13 @@ def launch(self, payload=None): # return job if result.json['type'] == 'job': jobs_pg = self.get_related('jobs', id=result.json['job']) - assert jobs_pg.count == 1, "job_template launched (id:%s) but job not found in response at %s/jobs/" % (result.json['job'], self.url) + assert jobs_pg.count == 1, f"job_template launched (id:{result.json['job']}) but job not found in response at {self.url}/jobs/" return jobs_pg.results[0] elif result.json['type'] == 'workflow_job': slice_workflow_jobs = self.get_related('slice_workflow_jobs', id=result.json['id']) - assert slice_workflow_jobs.count == 1, "job_template launched sliced job (id:%s) but not found in related %s/slice_workflow_jobs/" % ( - result.json['id'], - self.url, - ) + assert ( + slice_workflow_jobs.count == 1 + ), f"job_template launched sliced job (id:{result.json['id']}) but not found in related {self.url}/slice_workflow_jobs/" return slice_workflow_jobs.results[0] else: raise RuntimeError('Unexpected type of job template spawned job.') diff --git a/ascenderkit/api/pages/notification_templates.py b/ascenderkit/api/pages/notification_templates.py index c650742..06f7a49 100644 --- a/ascenderkit/api/pages/notification_templates.py +++ b/ascenderkit/api/pages/notification_templates.py @@ -26,10 +26,9 @@ def test(self): # return notification page notifications_pg = self.get_related('notifications', id=notification_id).wait_until_count(1) - assert notifications_pg.count == 1, "test notification triggered (id:%s) but notification not found in response at %s/notifications/" % ( - notification_id, - self.url, - ) + assert ( + notifications_pg.count == 1 + ), f"test notification triggered (id:{notification_id}) but notification not found in response at {self.url}/notifications/" return notifications_pg.results[0] def silent_delete(self): diff --git a/ascenderkit/api/pages/page.py b/ascenderkit/api/pages/page.py index cae4620..6e5c209 100644 --- a/ascenderkit/api/pages/page.py +++ b/ascenderkit/api/pages/page.py @@ -194,7 +194,7 @@ def page_identity(self, response, request_json=None): ds = None data = self.extract_data(response) - exc_str = "%s (%s) received" % (http.responses[response.status_code], response.status_code) + exc_str = f"{http.responses[response.status_code]} ({response.status_code}) received" exception = exception_from_status_code(response.status_code) if exception: diff --git a/ascenderkit/api/pages/projects.py b/ascenderkit/api/pages/projects.py index 90029e7..cf9f0f5 100644 --- a/ascenderkit/api/pages/projects.py +++ b/ascenderkit/api/pages/projects.py @@ -101,20 +101,19 @@ def update(self): update_pg = self.get_related('update') # assert can_update == True - assert update_pg.can_update, "The specified project (id:%s) is not able to update (can_update:%s)" % (self.id, update_pg.can_update) + assert update_pg.can_update, f"The specified project (id:{self.id}) is not able to update (can_update:{update_pg.can_update})" # start the update result = update_pg.post() # assert JSON response - assert 'project_update' in result.json, "Unexpected JSON response when starting an project_update.\n%s" % json.dumps(result.json, indent=2) + assert 'project_update' in result.json, f"Unexpected JSON response when starting an project_update.\n{json.dumps(result.json, indent=2)}" # locate and return the specific update jobs_pg = self.get_related('project_updates', id=result.json['project_update']) - assert jobs_pg.count == 1, "An project_update started (id:%s) but job not found in response at %s/inventory_updates/" % ( - result.json['project_update'], - self.url, - ) + assert ( + jobs_pg.count == 1 + ), f"An project_update started (id:{result.json['project_update']}) but job not found in response at {self.url}/inventory_updates/" return jobs_pg.results[0] @property diff --git a/ascenderkit/api/pages/system_job_templates.py b/ascenderkit/api/pages/system_job_templates.py index f06951d..b74fe65 100644 --- a/ascenderkit/api/pages/system_job_templates.py +++ b/ascenderkit/api/pages/system_job_templates.py @@ -14,10 +14,7 @@ def launch(self, payload=None): # return job jobs_pg = self.get_related('jobs', id=result.json['system_job']) - assert jobs_pg.count == 1, "system_job_template launched (id:%s) but unable to find matching job at %s/jobs/" % ( - result.json['system_job'], - self.url, - ) + assert jobs_pg.count == 1, f"system_job_template launched (id:{result.json['system_job']}) but unable to find matching job at {self.url}/jobs/" return jobs_pg.results[0] diff --git a/ascenderkit/ascender/inventory.py b/ascenderkit/ascender/inventory.py index 01dc61e..8260d67 100644 --- a/ascenderkit/ascender/inventory.py +++ b/ascenderkit/ascender/inventory.py @@ -16,13 +16,13 @@ def upload_inventory(ansible_runner, nhosts=10, ini=False): copy_dest = '/tmp/inventory{}.sh'.format(random_title(non_ascii=False)) copy_content = '''#!/bin/bash cat <