Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoformat code using black #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ jobs:

- name: Install dependencies needed for checking
run: |
sudo apt update && sudo apt install flake8
sudo apt update
sudo apt install flake8
pip install black
pip install isort

- name: Check format, compilation, and imports
working-directory: ${{github.workspace}}/qlever-control
run: |
for PY in $(find src test -name "*.py"); do printf "$PY ... "; flake8 $PY && python3 -m py_compile $PY && isort -c $PY && echo "OK"; done
for PY in $(find src test -name "*.py"); do printf "$PY ... "; black --check $PY && flake8 $PY && python3 -m py_compile $PY && isort -c $PY && echo "OK"; done
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[lint]
select = ["E2", "E3", "E4", "E5", "E7", "E9", "F", "I"]
11 changes: 7 additions & 4 deletions src/qlever/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ 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 +25,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)"
)
69 changes: 40 additions & 29 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,25 @@ 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,16 +79,19 @@ 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`"
)

# Check if text index files already exist.
existing_text_index_files = get_existing_index_files(
f"{args.name}.text.*")
existing_text_index_files = get_existing_index_files(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 @@ -89,7 +100,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
69 changes: 39 additions & 30 deletions src/qlever/commands/cache_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ def __init__(self):
pass

def description(self) -> str:
return ("Show how much of the cache is currently being used")
return "Show how much of the cache is currently being used"

def should_have_qleverfile(self) -> bool:
return False

def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {"server": ["host_name", "port"]}

def additional_arguments(self, subparser) -> None:
subparser.add_argument("--server-url",
help="URL of the QLever server, default is "
"localhost:{port}")
subparser.add_argument("--detailed",
action="store_true",
default=False,
help="Show detailed statistics and settings")
subparser.add_argument(
"--server-url",
help="URL of the QLever server, default is " "localhost:{port}",
)
subparser.add_argument(
"--detailed",
action="store_true",
default=False,
help="Show detailed statistics and settings",
)

def execute(self, args) -> bool:
# Construct the two curl commands.
server_url = (args.server_url if args.server_url
else f"localhost:{args.port}")
cache_stats_cmd = (f"curl -s {server_url} "
f"--data-urlencode \"cmd=cache-stats\"")
cache_settings_cmd = (f"curl -s {server_url} "
f"--data-urlencode \"cmd=get-settings\"")
server_url = args.server_url if args.server_url else f"localhost:{args.port}"
cache_stats_cmd = f"curl -s {server_url} " f'--data-urlencode "cmd=cache-stats"'
cache_settings_cmd = (
f"curl -s {server_url} " f'--data-urlencode "cmd=get-settings"'
)

# Show them.
self.show("\n".join([cache_stats_cmd, cache_settings_cmd]),
only_show=args.show)
self.show("\n".join([cache_stats_cmd, cache_settings_cmd]), only_show=args.show)
if args.show:
return False

# Execute them.
try:
cache_stats = subprocess.check_output(cache_stats_cmd, shell=True)
cache_settings = subprocess.check_output(cache_settings_cmd,
shell=True)
cache_settings = subprocess.check_output(cache_settings_cmd, shell=True)
cache_stats_dict = json.loads(cache_stats)
cache_settings_dict = json.loads(cache_settings)
except Exception as e:
Expand All @@ -64,8 +64,10 @@ def execute(self, args) -> bool:
if not args.detailed:
cache_size = cache_settings_dict["cache-max-size"]
if not cache_size.endswith(" GB"):
log.error(f"Cache size {cache_size} is not in GB, "
f"QLever should return bytes instead")
log.error(
f"Cache size {cache_size} is not in GB, "
f"QLever should return bytes instead"
)
return False
else:
cache_size = float(cache_size[:-3])
Expand All @@ -76,15 +78,21 @@ def execute(self, args) -> bool:
if cached_size == 0:
log.info(f"Cache is empty, all {cache_size:.1f} GB available")
else:
log.info(f"Pinned queries : "
f"{pinned_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{pinned_size / cache_size:5.1%}]")
log.info(f"Non-pinned queries : "
f"{non_pinned_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{non_pinned_size / cache_size:5.1%}]")
log.info(f"FREE : "
f"{free_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{1 - cached_size / cache_size:5.1%}]")
log.info(
f"Pinned queries : "
f"{pinned_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{pinned_size / cache_size:5.1%}]"
)
log.info(
f"Non-pinned queries : "
f"{non_pinned_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{non_pinned_size / cache_size:5.1%}]"
)
log.info(
f"FREE : "
f"{free_size:5.1f} GB of {cache_size:5.1f} GB"
f" [{1 - cached_size / cache_size:5.1%}]"
)
return True

# Complete version.
Expand All @@ -96,6 +104,7 @@ def show_dict_as_table(key_value_pairs):
if re.match(r"^\d+\.\d+$", value):
value = "{:.2f}".format(float(value))
log.info(f"{key.ljust(max_key_len)} : {value}")

show_dict_as_table(cache_stats_dict.items())
log.info("")
show_dict_as_table(cache_settings_dict.items())
Expand Down
36 changes: 20 additions & 16 deletions src/qlever/commands/clear_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ def __init__(self):
pass

def description(self) -> str:
return ("Clear the query processing cache")
return "Clear the query processing cache"

def should_have_qleverfile(self) -> bool:
return True

def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {"server": ["port", "access_token"]}

def additional_arguments(self, subparser) -> None:
subparser.add_argument("--server-url",
help="URL of the QLever server, default is "
"localhost:{port}")
subparser.add_argument("--complete", action="store_true",
default=False,
help="Clear the cache completely, including "
"the pinned queries")
subparser.add_argument(
"--server-url",
help="URL of the QLever server, default is " "localhost:{port}",
)
subparser.add_argument(
"--complete",
action="store_true",
default=False,
help="Clear the cache completely, including " "the pinned queries",
)

def execute(self, args) -> bool:
# Construct command line and show it.
Expand All @@ -42,20 +45,21 @@ def execute(self, args) -> bool:
else:
clear_cache_cmd += f" localhost:{args.port}"
cmd_val = "clear-cache-complete" if args.complete else "clear-cache"
clear_cache_cmd += f" --data-urlencode \"cmd={cmd_val}\""
clear_cache_cmd += f' --data-urlencode "cmd={cmd_val}"'
if args.complete:
clear_cache_cmd += (f" --data-urlencode access-token="
f"\"{args.access_token}\"")
clear_cache_cmd += (
f" --data-urlencode access-token=" f'"{args.access_token}"'
)
self.show(clear_cache_cmd, only_show=args.show)
if args.show:
return False

# Execute the command.
try:
clear_cache_cmd += " -w \" %{http_code}\""
result = subprocess.run(clear_cache_cmd, shell=True,
capture_output=True, text=True,
check=True).stdout
clear_cache_cmd += ' -w " %{http_code}"'
result = subprocess.run(
clear_cache_cmd, shell=True, capture_output=True, text=True, check=True
).stdout
match = re.match(r"^(.*) (\d+)$", result, re.DOTALL)
if not match:
raise Exception(f"Unexpected output:\n{result}")
Expand Down
7 changes: 3 additions & 4 deletions src/qlever/commands/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def description(self) -> str:
def should_have_qleverfile(self) -> bool:
return True

def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {"data": ["name", "get_data_cmd"], "index": ["input_files"]}

def additional_arguments(self, subparser) -> None:
Expand All @@ -37,12 +37,11 @@ def execute(self, args) -> bool:
try:
run_command(args.get_data_cmd, show_output=True)
except Exception as e:
log.error(f"Problem executing \"{args.get_data_cmd}\": {e}")
log.error(f'Problem executing "{args.get_data_cmd}": {e}')
return False

# Show the total file size in GB and return.
patterns = shlex.split(args.input_files)
total_file_size = get_total_file_size(patterns)
print(f"Download successful, total file size: "
f"{total_file_size:,} bytes")
print(f"Download successful, total file size: " f"{total_file_size:,} bytes")
return True
Loading