Skip to content

Commit

Permalink
Use the walrus := operator!
Browse files Browse the repository at this point in the history
Fixed by running
```
ack --type=python -f | grep -v '^lib/galaxy/schema/bco/\|^lib/galaxy/schema/drs/\|^tools/\|^.venv/' | xargs auto-walrus
make format
```
  • Loading branch information
mr-c committed Nov 1, 2023
1 parent 1089a79 commit 0d50ae2
Show file tree
Hide file tree
Showing 187 changed files with 328 additions and 659 deletions.
3 changes: 1 addition & 2 deletions doc/parse_gx_xsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def _build_tag(tag, hide_attributes):
assertions_buffer.write(f"#### ``{element.attrib['name']}``:\n\n{doc}\n\n")
text = text.replace(line, assertions_buffer.getvalue())
tag_help.write(text)
best_practices = _get_bp_link(annotation_el)
if best_practices:
if best_practices := _get_bp_link(annotation_el):
tag_help.write("\n\n### Best Practices\n")
tag_help.write(
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/actions/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def _get_path_paste_uploaded_datasets(self, trans, params, library_bunch, respon
return uploaded_datasets, 200, None

def _get_path_files_and_folders(self, params, preserve_dirs):
problem_response = self._check_path_paste_params(params)
if problem_response:
if problem_response := self._check_path_paste_params(params):
return problem_response
files_and_folders = []
for line, path in self._paths_list(params):
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,7 @@ def to_str(self, **kwd):

class ExecutionTimerFactory:
def __init__(self, config):
statsd_host = getattr(config, "statsd_host", None)
if statsd_host:
if statsd_host := getattr(config, "statsd_host", None):
from galaxy.web.statsd_client import GalaxyStatsdClient

self.galaxy_statsd_client: Optional[GalaxyStatsdClient] = GalaxyStatsdClient(
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def active_authenticators(self, email, username, password):

def _get_allow_register(d):
s = d.get("allow-register", True)
lower_s = str(s).lower()
if lower_s == "challenge":
if (lower_s := str(s).lower()) == "challenge":
return lower_s
else:
return string_as_bool(s)
3 changes: 1 addition & 2 deletions lib/galaxy/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def get_galaxy_app():

@lru_cache(maxsize=1)
def build_app():
kwargs = get_app_properties()
if kwargs:
if kwargs := get_app_properties():
kwargs["check_migrate_databases"] = False
kwargs["use_display_applications"] = False
kwargs["use_converters"] = False
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,6 @@ def _process_config(self, kwargs: Dict[str, Any]) -> None:
log_destination = kwargs.get("log_destination")
log_rotate_size = size_to_bytes(unicodify(kwargs.get("log_rotate_size", 0)))
log_rotate_count = int(kwargs.get("log_rotate_count", 0))
galaxy_daemon_log_destination = os.environ.get("GALAXY_DAEMON_LOG")
if log_destination == "stdout":
LOGGING_CONFIG_DEFAULT["handlers"]["console"] = {
"class": "logging.StreamHandler",
Expand All @@ -1189,7 +1188,7 @@ def _process_config(self, kwargs: Dict[str, Any]) -> None:
"maxBytes": log_rotate_size,
"backupCount": log_rotate_count,
}
if galaxy_daemon_log_destination:
if galaxy_daemon_log_destination := os.environ.get("GALAXY_DAEMON_LOG"):
LOGGING_CONFIG_DEFAULT["handlers"]["files"] = {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "stack",
Expand Down
6 changes: 2 additions & 4 deletions lib/galaxy/config/config_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ def _replace_file(args: Namespace, f: StringIO, app_desc: App, from_path: str, t
def _build_sample_yaml(args: Namespace, app_desc: App) -> None:
schema = app_desc.schema
f = StringIO()
description = getattr(schema, "description", None)
if description:
if description := getattr(schema, "description", None):
description = description.lstrip()
as_comment = "\n".join(f"# {line}" for line in description.split("\n")) + "\n"
f.write(as_comment)
Expand Down Expand Up @@ -517,8 +516,7 @@ def _warn(message: str) -> None:

def _get_option_desc(option: Dict[str, Any]) -> str:
desc = option["desc"]
parent_dir = option.get("path_resolves_to")
if parent_dir:
if parent_dir := option.get("path_resolves_to"):
path_resolves = f"The value of this option will be resolved with respect to <{parent_dir}>."
return f"{desc}\n{path_resolves}" if desc else path_resolves
return desc
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/datatypes/dataproviders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def __new__(cls, name, base_classes, attributes):
if base_settings:
settings.update(base_settings)
# get settings defined in this class
new_settings = attributes.pop("settings", None)
if new_settings:
if new_settings := attributes.pop("settings", None):
settings.update(new_settings)
attributes["settings"] = settings
return type.__new__(cls, name, base_classes, attributes)
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/datatypes/dataproviders/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ def __iter__(self):
parent_gen = super().__iter__()
yield from parent_gen

last_block = self.handle_last_block()
if last_block is not None:
if (last_block := self.handle_last_block()) is not None:
self.num_data_returned += 1
yield last_block

Expand Down
6 changes: 2 additions & 4 deletions lib/galaxy/datatypes/display_applications/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ def _get_dataset_like_object(self, other_values):
return data

def get_value(self, other_values, dataset_hash, user_hash, trans):
data = self._get_dataset_like_object(other_values)
if data:
if data := self._get_dataset_like_object(other_values):
return DisplayDataValueWrapper(data, self, other_values, dataset_hash, user_hash, trans)
return None

Expand Down Expand Up @@ -165,8 +164,7 @@ def is_preparing(self, other_values):
return False

def ready(self, other_values):
value = self._get_dataset_like_object(other_values)
if value:
if value := self._get_dataset_like_object(other_values):
if value.state == value.states.OK:
return True
elif value.state == value.states.ERROR:
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/datatypes/isa.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def _get_investigation(self, dataset: HasExtraFilesPath) -> Optional["Investigat
We will use it to parse and access information from the archive."""

investigation = None
main_file = self._get_main_file(dataset)
if main_file is not None:
if (main_file := self._get_main_file(dataset)) is not None:
investigation = self._make_investigation_instance(main_file)

return investigation
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/datatypes/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,7 @@ def load_datatype_sniffers(self, root, override=False, compressed_sniffers=None)
distributed config) or contained within an installed Tool Shed repository.
"""
sniffer_elem_classes = [e.attrib["type"] for e in self.sniffer_elems]
sniffers = root.find("sniffers")
if sniffers is not None:
if (sniffers := root.find("sniffers")) is not None:
for elem in sniffers.findall("sniffer"):
# Keep a status of the process steps to enable stopping the process of handling the sniffer if necessary.
ok = True
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/files/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ def _get_error_msg_for(rule_name: str) -> str:

def uri_join(*args):
# url_join doesn't work with non-standard scheme
arg0 = args[0]
if "://" in arg0:
if "://" in (arg0 := args[0]):
scheme, path = arg0.split("://", 1)
rval = f"{scheme}://{slash_join(path, *args[1:]) if path else slash_join(*args[1:])}"
else:
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/files/sources/drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def _write_from(self, target_path, native_path, user_context=None, opts: Optiona
raise NotImplementedError()

def score_url_match(self, url: str):
match = self._url_regex.match(url)
if match:
if match := self._url_regex.match(url):
return match.span()[1]
else:
return 0
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/files/sources/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def _serialization_props(self, user_context=None) -> HTTPFilesSourceProperties:
return cast(HTTPFilesSourceProperties, effective_props)

def score_url_match(self, url: str):
match = self._url_regex.match(url)
if match:
if match := self._url_regex.match(url):
return match.span()[1]
else:
return 0
Expand Down
6 changes: 2 additions & 4 deletions lib/galaxy/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ def from_elem(self, elem, form_definition_current=None):
form_type = elem.get("type", None)
# load layout
layout = []
layouts_elem = elem.find("layout")
if layouts_elem:
if layouts_elem := elem.find("layout"):
for layout_elem in layouts_elem.findall("grid"):
layout_name = layout_elem.get("name", None)
assert layout_name and layout_name not in layout, "Layout grid element requires a unique name."
layout.append(layout_name)
# load fields
fields = []
fields_elem = elem.find("fields")
if fields_elem is not None:
if (fields_elem := elem.find("fields")) is not None:
for field_elem in fields_elem.findall("field"):
field_type = field_elem.get("type")
assert field_type in self.field_type_factories, f"Invalid form field type ( {field_type} )."
Expand Down
6 changes: 2 additions & 4 deletions lib/galaxy/job_execution/actions/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ def execute_on_mapped_over(
if step_input and hasattr(step_input, "name"):
input_names[input_key] = step_input.name

new_name = cls._gen_new_name(action, input_names, replacement_dict)
if new_name:
if new_name := cls._gen_new_name(action, input_names, replacement_dict):
for name, step_output in step_outputs.items():
if action.output_name == "" or name == action.output_name:
step_output.name = new_name
Expand Down Expand Up @@ -248,8 +247,7 @@ def execute(cls, app, sa_session, action, job, replacement_dict, final_job_state
if has_collection and hasattr(has_collection, "name"):
input_names[input_assoc.name] = has_collection.name

new_name = cls._gen_new_name(action, input_names, replacement_dict)
if new_name:
if new_name := cls._gen_new_name(action, input_names, replacement_dict):
for dataset_assoc in job.output_datasets:
if action.output_name == "" or dataset_assoc.name == action.output_name:
dataset_assoc.dataset.name = new_name
Expand Down
6 changes: 2 additions & 4 deletions lib/galaxy/job_execution/output_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def permissions(self):
return self._permissions

def set_default_hda_permissions(self, primary_data):
permissions = self.permissions
if permissions is not UNSET:
if (permissions := self.permissions) is not UNSET:
self._security_agent.set_all_dataset_permissions(primary_data.dataset, permissions, new=True, flush=False)

def copy_dataset_permissions(self, init_from, primary_data):
Expand Down Expand Up @@ -672,9 +671,8 @@ def match(self, dataset_instance, filename, path=None, parent_paths=None):
pattern = self._pattern_for_dataset(dataset_instance)
if self.match_relative_path and parent_paths:
filename = os.path.join(*parent_paths, filename)
re_match = re.match(pattern, filename)
match_object = None
if re_match:
if re_match := re.match(pattern, filename):
match_object = RegexCollectedDatasetMatch(re_match, self, filename, path=path)
return match_object

Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/job_execution/ports/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def register_container_information(self, job_id, **kwd):

# Copy/paste from JobFilesView - TODO: de-duplicate.
def __authorize_job_access(self, encoded_job_id, **kwargs):
key = "job_key"
if key not in kwargs:
if (key := "job_key") not in kwargs:
error_message = f"Job files action requires a valid '{key}'."
raise ObjectAttributeMissingException(error_message)

Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/job_metrics/instrumenters/cgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class CgroupPlugin(InstrumentPlugin):
def __init__(self, **kwargs):
self.verbose = asbool(kwargs.get("verbose", False))
self.cgroup_mount = kwargs.get("cgroup_mount", "/sys/fs/cgroup")
params_str = kwargs.get("params", None)
if params_str:
if params_str := kwargs.get("params", None):
params = [v.strip() for v in params_str.split(",")]
else:
params = list(TITLES.keys())
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/job_metrics/instrumenters/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class EnvPlugin(InstrumentPlugin):
default_safety = Safety.UNSAFE

def __init__(self, **kwargs):
variables_str = kwargs.get("variables", None)
if variables_str:
if variables_str := kwargs.get("variables", None):
self.variables = [v.strip() for v in variables_str.split(",")]
else:
self.variables = None
Expand Down
21 changes: 7 additions & 14 deletions lib/galaxy/jobs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ def job_config_xml_to_dict(config, root):
config_dict["runners"] = runners

# Parser plugins section populate 'runners' and 'dynamic' in config_dict.
plugins = root.find("plugins")
if plugins is not None:
if (plugins := root.find("plugins")) is not None:
for plugin in ConfiguresHandlers._findall_with_required(plugins, "plugin", ("id", "type", "load")):
if plugin.get("type") == "runner":
workers = plugin.get("workers", plugins.get("workers", JobConfiguration.DEFAULT_NWORKERS))
Expand Down Expand Up @@ -243,8 +242,7 @@ def job_config_xml_to_dict(config, root):
resource_groups = {}

# Parse resources...
resources = root.find("resources")
if resources is not None:
if (resources := root.find("resources")) is not None:
default_resource_group = resources.get("default", None)
if default_resource_group:
resources_config_dict["default"] = default_resource_group
Expand All @@ -259,9 +257,8 @@ def job_config_xml_to_dict(config, root):
config_dict["resources"] = resources_config_dict

# Parse tool mappings
tools = root.find("tools")
config_dict["tools"] = []
if tools is not None:
if (tools := root.find("tools")) is not None:
for tool in tools.findall("tool"):
# There can be multiple definitions with identical ids, but different params
tool_mapping_conf = {}
Expand All @@ -275,8 +272,7 @@ def job_config_xml_to_dict(config, root):
config_dict["tools"].append(tool_mapping_conf)

limits_config = []
limits = root.find("limits")
if limits is not None:
if (limits := root.find("limits")) is not None:
for limit in JobConfiguration._findall_with_required(limits, "limit", ("type",)):
limit_dict = {}
for key in ["type", "tag", "id", "window"]:
Expand Down Expand Up @@ -1237,8 +1233,7 @@ def prepare(self, compute_environment=None):

def get_special():
stmt = select(model.JobExportHistoryArchive).filter_by(job=job).limit(1)
jeha = self.sa_session.scalars(stmt).first()
if jeha:
if jeha := self.sa_session.scalars(stmt).first():
return jeha.fda
stmt = select(model.GenomeIndexToolData).filter_by(job=job).limit(1)
return self.sa_session.scalars(stmt).first()
Expand Down Expand Up @@ -1510,8 +1505,7 @@ def is_ready_for_resubmission(self, job=None):
if job is None:
job = self.get_job()

destination_params = job.destination_params
if "__resubmit_delay_seconds" in destination_params:
if "__resubmit_delay_seconds" in (destination_params := job.destination_params):
delay = float(destination_params["__resubmit_delay_seconds"])
if job.seconds_since_updated < delay:
return False
Expand Down Expand Up @@ -2408,8 +2402,7 @@ def container_monitor_command(self, container, **kwds):
@property
def user(self):
job = self.get_job()
user_email = job.get_user_email()
if user_email:
if user_email := job.get_user_email():
return user_email
elif job.galaxy_session is not None:
return f"anonymous@{job.galaxy_session.remote_addr.split()[-1]}"
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/jobs/command_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def build_command(

__handle_remote_command_line_building(commands_builder, job_wrapper, for_pulsar=for_pulsar)

container_monitor_command = job_wrapper.container_monitor_command(container)
if container_monitor_command:
if container_monitor_command := job_wrapper.container_monitor_command(container):
commands_builder.prepend_command(container_monitor_command)

working_directory = remote_job_directory or job_wrapper.working_directory
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/jobs/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,7 @@ def stop(self, job, job_wrapper):
from galaxy.celery import celery_app

celery_app.control.revoke(job.job_runner_external_id)
job_runner_name = job.get_job_runner_name()
if job_runner_name is not None:
if (job_runner_name := job.get_job_runner_name()) is not None:
runner_name = job_runner_name.split(":", 1)[0]
log.debug(f"Stopping job {job_wrapper.get_id_tag()} in {runner_name} runner")
try:
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/jobs/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ def __get_expand_function(self, destination):
rules_module_name = destination.params.get("rules_module")
rule_modules = self.__get_rule_modules_or_defaults(rules_module_name)
expand_function = None
expand_function_name = destination.params.get("function")
if expand_function_name:
if expand_function_name := destination.params.get("function"):
expand_function = self.__last_matching_function_in_modules(rule_modules, expand_function_name)
if not expand_function:
message = ERROR_MESSAGE_RULE_FUNCTION_NOT_FOUND % expand_function_name
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/jobs/runners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ def shutdown(self):
for _ in range(len(self.work_threads)):
self.work_queue.put((STOP_SIGNAL, None))

join_timeout = self.app.config.monitor_thread_join_timeout
if join_timeout > 0:
if (join_timeout := self.app.config.monitor_thread_join_timeout) > 0:
log.info("Waiting up to %d seconds for job worker threads to shutdown...", join_timeout)
start = time.time()
# NOTE: threads that have already joined by now are not going to be logged
Expand Down
Loading

0 comments on commit 0d50ae2

Please sign in to comment.