Fix ruff warnings

This commit is contained in:
Adrian Moennich 2023-11-22 18:19:25 +01:00
parent b294c174b3
commit 34e8cd3148
20 changed files with 51 additions and 49 deletions

View File

@ -121,7 +121,10 @@ class LiveSyncCitadelUploader(Uploader):
# XXX this should no longer happen under any circumstances!
# if we already have a mapping entry, delete the newly created record and
# update the existing one in case something changed in the meantime
self.logger.error(f'{object_type.name.title()} %d already in citadel; deleting+updating', object_id)
self.logger.error(
f'{object_type.name.title()} %d already in citadel; deleting+updating', # noqa: G004
object_id
)
db.session.rollback()
self._citadel_delete(session, new_citadel_id, delete_mapping=False)
existing_citadel_id = CitadelIdMap.get_citadel_id(object_type, object_id)
@ -313,12 +316,14 @@ class LiveSyncCitadelBackend(LiveSyncBackendBase):
def process_queue(self, uploader, allowed_categories=()):
super().process_queue(uploader, allowed_categories)
uploader_name = type(uploader).__name__
self.plugin.logger.info(f'{uploader_name} starting file upload')
self.plugin.logger.info(f'{uploader_name} starting file upload') # noqa: G004
total, errors, aborted = self.run_export_files(verbose=False)
if aborted:
self.plugin.logger.info(f'{uploader_name} aborted after uploading %d files (%d failed)', total, errors)
self.plugin.logger.info(f'{uploader_name} aborted after uploading %d files (%d failed)', # noqa: G004
total, errors)
else:
self.plugin.logger.info(f'{uploader_name} finished uploading %d files (%d failed)', total, errors)
self.plugin.logger.info(f'{uploader_name} finished uploading %d files (%d failed)', # noqa: G004
total, errors)
def run_initial_export(self, batch_size, force=False, verbose=False):
if not super().run_initial_export(batch_size, force, verbose):
@ -363,7 +368,7 @@ class LiveSyncCitadelBackend(LiveSyncBackendBase):
lambda obj: re.sub(r'\s+', ' ', strip_control_chars(obj.attachment.title)),
print_total_time=True)
else:
self.plugin.logger.info(f'{total} files need to be uploaded')
self.plugin.logger.info('%d files need to be uploaded', total)
total, errors, aborted = uploader.upload_files(attachments, initial=initial)
return total, errors, aborted

View File

@ -57,7 +57,7 @@ def upload(batch, force, max_size, retry):
traceback.print_exc()
print('Restarting in 2 seconds\a')
time.sleep(2)
os.execl(sys.argv[0], *sys.argv)
os.execl(sys.argv[0], *sys.argv) # noqa: S606
return # exec doesn't return but just in case...
if not errors and not aborted:

View File

@ -60,7 +60,7 @@ def _make_checks():
class CitadelIdMap(db.Model):
__tablename__ = 'id_map'
__table_args__ = tuple(_make_checks()) + ({'schema': 'plugin_citadel'},)
__table_args__ = (*_make_checks(), {'schema': 'plugin_citadel'})
id = db.Column(
db.Integer,

View File

@ -62,9 +62,9 @@ class ACLSchema:
manager_list = set(linked_object.get_manager_list(recursive=True))
if attachment.is_self_protected:
return _get_identifiers({e for e in attachment.acl} | manager_list)
return _get_identifiers(set(attachment.acl) | manager_list)
elif attachment.is_inheriting and attachment.folder.is_self_protected:
return _get_identifiers({e for e in attachment.folder.acl} | manager_list)
return _get_identifiers(set(attachment.folder.acl) | manager_list)
else:
return self._get_acl(linked_object)
@ -84,7 +84,7 @@ class ACLSchema:
elif isinstance(object, EventNote):
obj_acl = self._get_acl(object.object)
else:
raise ValueError(f'unknown object {object}')
raise TypeError(f'unknown object {object}')
acl = {
'owner': [default_acl],
@ -92,7 +92,7 @@ class ACLSchema:
'delete': [default_acl]
}
if obj_acl is not None:
acl['read'] = [default_acl] + obj_acl
acl['read'] = [default_acl, *obj_acl]
return acl

View File

@ -10,7 +10,7 @@ import pytest
from indico_citadel.util import _flatten, format_aggregations, format_query, remove_none_entries
@pytest.mark.parametrize(('query', 'expected'), [
@pytest.mark.parametrize(('query', 'expected'), (
('title:"my event" ola person:john ola some:yes ola',
'title:"my event" ola person:john ola some\\:yes ola'),
('title:"my title:something"', 'title:"my title\\:something"'),
@ -24,29 +24,29 @@ from indico_citadel.util import _flatten, format_aggregations, format_query, rem
('"section meeting" OR "group meeting"', '"section meeting" OR "group meeting"'),
('title:meeting AND "indico"', 'title:meeting AND "indico"'),
('title:valid stringtitle:valid foo:bar', 'title:valid stringtitle\\:valid foo\\:bar')
])
))
def test_query_placeholders(query, expected):
placeholders = {'title': 'title', 'person': 'person', 'file': 'file'}
assert format_query(query, placeholders) == expected
@pytest.mark.parametrize(('val', 'expected'), [
@pytest.mark.parametrize(('val', 'expected'), (
({'a': 0, 'b': None, 'c': {'c1': None, 'c2': 0, 'c3': {'c3a': None}}},
{'a': 0, 'c': {'c2': 0, 'c3': {}}}),
({'a': 0, 'b': [None, {'b1': None, 'b2': 'test'}]},
{'a': 0, 'b': [None, {'b2': 'test'}]}),
(None, None),
])
))
def test_remove_none_entries(val, expected):
assert remove_none_entries(val) == expected
@pytest.mark.parametrize(('val', 'expected'), [
@pytest.mark.parametrize(('val', 'expected'), (
({'person': {'name': {'buckets': []}, 'affiliation': {'buckets': []}}, 'other': {'other': {'buckets': []}}},
{'person_name', 'person_affiliation', 'other_other'}),
({'other': {'other': {'other': {'other': {'buckets': []}}}}},
{'other_other_other_other'})
])
))
def test_flatten(val, expected):
assert {key for key, _ in _flatten(val)} == expected

View File

@ -107,7 +107,7 @@ def initial_export(agent_id, batch, force, verbose, retry):
traceback.print_exc()
print('Restarting in 2 seconds')
time.sleep(2)
os.execl(sys.argv[0], *sys.argv)
os.execl(sys.argv[0], *sys.argv) # noqa: S606
return # exec doesn't return but just in case...
agent.initial_data_exported = True
@ -197,7 +197,7 @@ def reset(agent_id):
for i in range(5):
print(cformat('\rResetting in %{white!}{}%{reset}s (CTRL+C to abort)').format(5 - i), end='')
time.sleep(1)
print('')
print()
backend.reset()
db.session.commit()

View File

@ -64,7 +64,7 @@ def _make_checks():
class LiveSyncQueueEntry(db.Model):
__tablename__ = 'queues'
__table_args__ = tuple(_make_checks()) + ({'schema': 'plugin_livesync'},)
__table_args__ = (*_make_checks(), {'schema': 'plugin_livesync'})
#: Entry ID
id = db.Column(
@ -267,7 +267,7 @@ class LiveSyncQueueEntry(db.Model):
note_id=None, attachment_id=None)
@classmethod
def create(cls, changes, ref, excluded_categories=set()):
def create(cls, changes, ref, excluded_categories=None):
"""Create a new change in all queues.
:param changes: the change types, an iterable containing
@ -277,6 +277,9 @@ class LiveSyncQueueEntry(db.Model):
:param excluded_categories: set of categories (IDs) whose items
will not be tracked
"""
if excluded_categories is None:
excluded_categories = set()
ref = dict(ref)
obj = obj_deref(ref)

View File

@ -35,17 +35,17 @@ class Uploader:
if self.from_cli:
simplified = self._make_verbose(simplified, total)
try:
self.logger.info(f'{self_name} uploading %d changes from %d records', total, len(records))
self.logger.info(f'{self_name} uploading %d changes from %d records', total, len(records)) # noqa: G004
if not self.upload_records(simplified):
self.logger.warning('uploader indicated a failure')
return
except Exception:
self.logger.exception(f'{self_name} failed')
self.logger.exception(f'{self_name} failed') # noqa: G004
if self.from_cli:
raise
return
self.processed_records(records)
self.logger.info(f'{self_name} finished (%d total changes from %d records)', total, len(records))
self.logger.info(f'{self_name} finished (%d total changes from %d records)', total, len(records)) # noqa: G004
def run_initial(self, records, total):
"""Runs the initial batch upload

View File

@ -39,7 +39,7 @@ def obj_ref(obj):
elif isinstance(obj, Attachment):
ref = {'type': EntryType.attachment, 'attachment_id': obj.id}
else:
raise ValueError(f'Unexpected object: {obj.__class__.__name__}')
raise TypeError(f'Unexpected object: {obj.__class__.__name__}')
return ImmutableDict(ref)

View File

@ -39,7 +39,6 @@ def test_ipn_verify_business(formdata, expected, dummy_event):
('13.37', 'EUR', True),
('13.37', 'CHF', False),
('10.00', 'CHF', False),
('10.00', 'CHF', False),
))
def test_ipn_verify_amount(mocker, amount, currency, expected):
nai = mocker.patch('indico_payment_paypal.controllers.notify_amount_inconsistency')

View File

@ -70,7 +70,7 @@ class ReportBase:
if self.end_date is None:
today = now_utc().date()
end_date = self.event.end_dt.date()
self.end_date = end_date if end_date < today else today
self.end_date = min(today, end_date)
if self.start_date is None:
self.start_date = self.end_date - timedelta(days=ReportBase.default_report_interval)

View File

@ -59,7 +59,7 @@ class CppHighlighter(Preprocessor):
try:
if nb.metadata.kernelspec.language == 'c++':
self.preprocess_cell = self._preprocess_cell_cpp
except Exception:
except Exception: # noqa: S110 - no idea what exception's being caught here :(
# if no language metadata, keep python as default
pass
return super().preprocess(nb, resources)

View File

@ -46,7 +46,7 @@ def test_endpoint_works(get_metrics):
@pytest.mark.usefixtures('db', 'enable_plugin')
def test_endpoint_empty(get_metrics):
metrics, _ = get_metrics()
metrics = get_metrics()[0]
assert metrics['indico_num_users'] == 1.0
assert metrics['indico_num_active_users'] == 0.0
@ -77,7 +77,7 @@ def test_endpoint_returning_data(get_metrics, create_event):
# create an event
create_event(title='Test event #1')
metrics, _ = get_metrics()
metrics = get_metrics()[0]
assert metrics['indico_num_users'] == 2.0
assert metrics['indico_num_active_users'] == 0.0
assert metrics['indico_num_events'] == 1.0

View File

@ -135,7 +135,7 @@ class S3Importer:
for model, total in models.items()}
max_length = max(len(x) for x in labels.values())
labels = {model: label.ljust(max_length) for model, label in labels.items()}
for model, total in sorted(list(models.items()), key=itemgetter(1)):
for model, total in sorted(models.items(), key=itemgetter(1)):
with click.progressbar(self.query_chunked(model, 1000), length=total, label=labels[model],
show_percent=True, show_pos=True) as objects:
for obj in self.flush_rclone_iterator(objects, 1000):
@ -342,7 +342,7 @@ def apply_changes(data_file, revert=False):
for model, total in models.items()}
max_length = max(len(x) for x in labels.values())
labels = {model: label.ljust(max_length) for model, label in labels.items()}
for model, total in sorted(list(models.items()), key=itemgetter(1)):
for model, total in sorted(models.items(), key=itemgetter(1)):
pks = inspect(model).primary_key
with click.progressbar(data[model.__name__], length=total, label=labels[model],
show_percent=True, show_pos=True) as entries:
@ -469,7 +469,7 @@ def copy(source_backend_names, bucket_names, static_bucket_name, s3_endpoint, s3
code.append(' backend = backend.replace("<week>", dt.strftime("%W"))')
code.append(' return bucket, backend')
d = {}
exec('\n'.join(code), d)
exec('\n'.join(code), d) # noqa: S102
if not source_backend_names:
source_backend_names = [x for x in config.STORAGE_BACKENDS if not isinstance(get_storage(x), S3StorageBase)]
if rclone:

View File

@ -13,6 +13,7 @@ from contextlib import contextmanager
from datetime import date
from enum import Enum
from io import BytesIO
from pathlib import Path
from tempfile import NamedTemporaryFile
from urllib.parse import quote
@ -172,8 +173,7 @@ class S3StorageBase(Storage):
if self.bucket_versioning:
self.client.put_bucket_versioning(Bucket=name, VersioningConfiguration={'Status': 'Enabled'})
if self.bucket_policy_file:
with open(self.bucket_policy_file) as f:
policy = f.read()
policy = Path(self.bucket_policy_file).read_text()
self.client.put_bucket_policy(Bucket=name, Policy=policy)
def _bucket_exists(self, name):
@ -248,8 +248,7 @@ class DynamicS3Storage(S3StorageBase):
def _replace_bucket_placeholders(self, name, date):
name = name.replace('<year>', date.strftime('%Y'))
name = name.replace('<month>', date.strftime('%m'))
name = name.replace('<week>', date.strftime('%W'))
return name
return name.replace('<week>', date.strftime('%W'))
def save(self, name, content_type, filename, fileobj):
try:

View File

@ -32,7 +32,7 @@ def create_bucket():
bucket_date = today + relativedelta(weeks=1)
bucket = storage._get_bucket_name(bucket_date)
storage._create_bucket(bucket)
elif placeholders == {'<year>', '<month>'} or placeholders == {'<year>'}:
elif placeholders in ({'<year>', '<month>'}, {'<year>'}):
if '<month>' in placeholders or today.month == 12:
bucket_date = today + relativedelta(months=1)
bucket = storage._get_bucket_name(bucket_date)

View File

@ -40,7 +40,7 @@ def _find_plugins():
def _get_config():
rv = {'extras': {}, 'skip': []}
try:
f = open('_meta/meta.yaml')
f = open('_meta/meta.yaml') # noqa: SIM115
except OSError as exc:
if exc.errno != errno.ENOENT:
raise
@ -84,17 +84,14 @@ def cli(nextver):
else:
plugins_require.append(pkgspec)
output = []
for entry in plugins_require:
output.append(f' {entry}')
output = [f' {entry}' for entry in plugins_require]
if extras_require:
if output:
output.append('')
output.append('[options.extras_require]')
for extra, pkgspecs in sorted(extras_require.items()):
output.append(f'{extra} =')
for pkg in sorted(pkgspecs):
output.append(f' {pkg}')
output.extend(f' {pkg}' for pkg in sorted(pkgspecs))
if _update_meta('\n'.join(output)):
click.secho('Updated meta package', fg='green')

View File

@ -28,8 +28,7 @@ def rooms(status=None):
if status:
room_query = room_query.filter(VCRoom.status == VCRoomStatus.get(status))
for room in room_query:
table_data.append([str(room.id), room.name, room.status.name, str(room.data['zoom_id'])])
table_data.extend([str(room.id), room.name, room.status.name, str(room.data['zoom_id'])] for room in room_query)
table = AsciiTable(table_data)
for col in (0, 3, 4):

View File

@ -95,7 +95,7 @@ class VCRoomForm(VCRoomFormBase):
allow_webinars = current_plugin.settings.get('allow_webinars')
if allow_webinars:
for field_name in {'mute_audio', 'mute_participant_video', 'waiting_room'}:
for field_name in ('mute_audio', 'mute_participant_video', 'waiting_room'):
inject_validators(self, field_name, [HiddenUnless('meeting_type', 'regular')])
super().__init__(*args, **kwargs)

View File

@ -256,7 +256,7 @@ class ZoomPlugin(VCPluginMixin, IndicoPlugin):
if room_assoc.link_object.start_dt
else {})
meeting = fetch_zoom_meeting(vc_room)
current_schedule_args = {k: meeting[k] for k in {'start_time', 'duration'} if k in meeting}
current_schedule_args = {k: meeting[k] for k in ('start_time', 'duration') if k in meeting}
# check whether the start time / duration of the scheduled meeting differs
if new_schedule_args != current_schedule_args:
@ -521,7 +521,7 @@ class ZoomPlugin(VCPluginMixin, IndicoPlugin):
def get_vc_room_attach_form_defaults(self, event):
defaults = super().get_vc_room_attach_form_defaults(event)
defaults['password_visibility'] = 'logged_in'
defaults['password_visibility'] = 'logged_in' # noqa: S105
return defaults
def can_manage_vc_room(self, user, room):