Skip to content

Commit

Permalink
Reformat some of the *.py files with ruff (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Mundhahs <[email protected]>
  • Loading branch information
hannahbast and Qup42 authored Jan 30, 2025
1 parent db11bb1 commit 9b36926
Show file tree
Hide file tree
Showing 9 changed files with 746 additions and 441 deletions.
13 changes: 9 additions & 4 deletions src/qlever/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ def snake_to_camel(str):
# Each module in `qlever/commands` corresponds to a command. The name
# of the command is the base name of the module file.
package_path = Path(__file__).parent
command_names = [Path(p).stem for p in package_path.glob("commands/*.py")
if p.name != "__init__.py"]
command_names = [
Path(p).stem
for p in package_path.glob("commands/*.py")
if p.name != "__init__.py"
]

# Dynamically load all the command classes and create an object for each.
command_objects = {}
Expand All @@ -24,8 +27,10 @@ def snake_to_camel(str):
try:
module = __import__(module_path, fromlist=[class_name])
except ImportError as e:
raise Exception(f"Could not import class {class_name} from module "
f"{module_path} for command {command_name}: {e}")
raise Exception(
f"Could not import class {class_name} from module "
f"{module_path} for command {command_name}: {e}"
)
# Create an object of the class and store it in the dictionary. For the
# commands, take - instead of _.
command_class = getattr(module, class_name)
Expand Down
11 changes: 6 additions & 5 deletions src/qlever/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def description(self) -> str:

@abstractmethod
def should_have_qleverfile(self) -> bool:

"""
Return `True` if the command should have a Qleverfile, `False`
otherwise. If a command should have a Qleverfile, but none is
Expand All @@ -43,7 +42,7 @@ def should_have_qleverfile(self) -> bool:
pass

@abstractmethod
def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
"""
Retun the arguments relevant for this command. This must be a subset of
the names of `all_arguments` defined in `QleverConfig`. Only these
Expand Down Expand Up @@ -81,6 +80,8 @@ def show(command_description: str, only_show: bool = False):
log.info(colored(command_description, "blue"))
log.info("")
if only_show:
log.info("You called \"qlever ... --show\", therefore the command "
"is only shown, but not executed (omit the \"--show\" to "
"execute it)")
log.info(
'You called "qlever ... --show", therefore the command '
'is only shown, but not executed (omit the "--show" to '
"execute it)"
)
75 changes: 47 additions & 28 deletions src/qlever/commands/add_text_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ def __init__(self):
pass

def description(self) -> str:
return ("Add text index to an index built with `qlever index`")
return "Add text index to an index built with `qlever index`"

def should_have_qleverfile(self) -> bool:
return True

def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
return {"data": ["name"],
"index": ["index_binary", "text_index",
"text_words_file", "text_docs_file"],
"runtime": ["system", "image", "index_container"]}
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {
"data": ["name"],
"index": [
"index_binary",
"text_index",
"text_words_file",
"text_docs_file",
],
"runtime": ["system", "image", "index_container"],
}

def additional_arguments(self, subparser) -> None:
subparser.add_argument(
"--overwrite-existing",
action="store_true",
help="Overwrite existing text index files")
"--overwrite-existing",
action="store_true",
help="Overwrite existing text index files",
)

def execute(self, args) -> bool:
# Check that there is actually something to add.
Expand All @@ -42,24 +49,31 @@ def execute(self, args) -> bool:

# Construct the command line.
add_text_index_cmd = f"{args.index_binary} -A -i {args.name}"
if args.text_index in \
["from_text_records", "from_text_records_and_literals"]:
add_text_index_cmd += (f" -w {args.text_words_file}"
f" -d {args.text_docs_file}")
if args.text_index in \
["from_literals", "from_text_records_and_literals"]:
if args.text_index in [
"from_text_records",
"from_text_records_and_literals",
]:
add_text_index_cmd += (
f" -w {args.text_words_file}" f" -d {args.text_docs_file}"
)
if args.text_index in [
"from_literals",
"from_text_records_and_literals",
]:
add_text_index_cmd += " --text-words-from-literals"
add_text_index_cmd += f" | tee {args.name}.text-index-log.txt"

# Run the command in a container (if so desired).
if args.system in Containerize.supported_systems():
add_text_index_cmd = Containerize().containerize_command(
add_text_index_cmd,
args.system, "run --rm",
args.image,
args.index_container,
volumes=[("$(pwd)", "/index")],
working_directory="/index")
add_text_index_cmd,
args.system,
"run --rm",
args.image,
args.index_container,
volumes=[("$(pwd)", "/index")],
working_directory="/index",
)

# Show the command line.
self.show(add_text_index_cmd, only_show=args.show)
Expand All @@ -71,17 +85,22 @@ def execute(self, args) -> bool:
try:
run_command(f"{args.index_binary} --help")
except Exception as e:
log.error(f"Running \"{args.index_binary}\" failed ({e}), "
f"set `--index-binary` to a different binary or "
f"use `--container_system`")
log.error(
f'Running "{args.index_binary}" failed ({e}), '
f"set `--index-binary` to a different binary or "
f"use `--container_system`"
)
return False

# Check if text index files already exist.
existing_text_index_files = get_existing_index_files(
f"{args.name}.text.*")
f"{args.name}.text.*"
)
if len(existing_text_index_files) > 0 and not args.overwrite_existing:
log.error("Text index files found, if you want to overwrite them, "
"use --overwrite-existing")
log.error(
"Text index files found, if you want to overwrite them, "
"use --overwrite-existing"
)
log.info("")
log.info(f"Index files found: {existing_text_index_files}")
return False
Expand All @@ -90,7 +109,7 @@ def execute(self, args) -> bool:
try:
subprocess.run(add_text_index_cmd, shell=True, check=True)
except Exception as e:
log.error(f"Running \"{add_text_index_cmd}\" failed ({e})")
log.error(f'Running "{add_text_index_cmd}" failed ({e})')
return False

return True
10 changes: 7 additions & 3 deletions src/qlever/commands/system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def additional_arguments(self, subparser) -> None:

def execute(self, args) -> bool:
# Say what the command is doing.
self.show("Show system information and Qleverfile", only_show=args.show)
self.show(
"Show system information and Qleverfile", only_show=args.show
)
if args.show:
return True

Expand All @@ -80,13 +82,15 @@ def execute(self, args) -> bool:
memory_total = psutil.virtual_memory().total / (1024.0**3)
memory_available = psutil.virtual_memory().available / (1024.0**3)
log.info(
f"RAM: {memory_total:.1f} GB total, " f"{memory_available:.1f} GB available"
f"RAM: {memory_total:.1f} GB total, "
f"{memory_available:.1f} GB available"
)
num_cores = psutil.cpu_count(logical=False)
num_threads = psutil.cpu_count(logical=True)
cpu_freq = psutil.cpu_freq().max / 1000
log.info(
f"CPU: {num_cores} Cores, " f"{num_threads} Threads @ {cpu_freq:.2f} GHz"
f"CPU: {num_cores} Cores, "
f"{num_threads} Threads @ {cpu_freq:.2f} GHz"
)

cwd = Path.cwd()
Expand Down
20 changes: 16 additions & 4 deletions src/qlever/commands/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {
"data": ["name"],
"server": ["host_name", "port"],
"ui": ["ui_port", "ui_config", "ui_system", "ui_image", "ui_container"],
"ui": [
"ui_port",
"ui_config",
"ui_system",
"ui_image",
"ui_container",
],
}

def additional_arguments(self, subparser) -> None:
pass

def execute(self, args) -> bool:
# If QLEVER_OVERRIDE_DISABLE_UI is set, this command is disabled.
qlever_is_running_in_container = environ.get("QLEVER_IS_RUNNING_IN_CONTAINER")
qlever_is_running_in_container = environ.get(
"QLEVER_IS_RUNNING_IN_CONTAINER"
)
if qlever_is_running_in_container:
log.error(
"The environment variable `QLEVER_OVERRIDE_DISABLE_UI` is set, "
Expand Down Expand Up @@ -67,7 +75,9 @@ def execute(self, args) -> bool:
f'{args.ui_config} {server_url}"'
)
self.show(
"\n".join(["Stop running containers", pull_cmd, run_cmd, exec_cmd]),
"\n".join(
["Stop running containers", pull_cmd, run_cmd, exec_cmd]
),
only_show=args.show,
)
if qlever_is_running_in_container:
Expand All @@ -77,7 +87,9 @@ def execute(self, args) -> bool:

# Stop running containers.
for container_system in Containerize.supported_systems():
Containerize.stop_and_remove_container(container_system, args.ui_container)
Containerize.stop_and_remove_container(
container_system, args.ui_container
)

# Check if the UI port is already being used.
if is_port_used(args.ui_port):
Expand Down
3 changes: 2 additions & 1 deletion src/qlever/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class QleverLogFormatter(logging.Formatter):
"""
Custom formatter for logging.
"""

def format(self, record):
message = record.getMessage()
if record.levelno == logging.DEBUG:
Expand All @@ -34,7 +35,7 @@ def format(self, record):
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
"NO_LOG": logging.CRITICAL + 1
"NO_LOG": logging.CRITICAL + 1,
}


Expand Down
Loading

0 comments on commit 9b36926

Please sign in to comment.