Skip to content
Merged
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
32 changes: 0 additions & 32 deletions .flake8

This file was deleted.

37 changes: 10 additions & 27 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,12 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace

# Sorts Python imports alphabetically and by section with `isort`.
- repo: https://github.com/pycqa/isort
rev: 6.0.1
hooks:
- id: isort
args: [--profile, black, --settings-path, pyproject.toml]

# Automatically upgrade syntax for newer versions of the language.
- repo: https://github.com/asottile/pyupgrade
rev: v3.20.0
hooks:
- id: pyupgrade
args: [--py38-plus]

# Aplly black formatting to python code
# https://github.com/psf/black
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
hooks:
- id: black
args: [--config, pyproject.toml]
args: [--py310-plus]

# Checks for spelling errors
- repo: https://github.com/codespell-project/codespell
Expand Down Expand Up @@ -84,15 +69,13 @@ repos:
- id: prettier
types_or: [css, html, json]

# Check that Python code complies with PEP8 guidelines
# flake8 uses pydocstyle to check docstrings: https://flake8.pycqa.org/en/latest/
# flake8-docstrings: https://pypi.org/project/flake8-docstrings/
# flake8-use-fstring forces to use fstrings: https://pypi.org/project/flake8-use-fstring/
# flake8-functions checks functions quality: https://pypi.org/project/flake8-functions/
# flake8-bugbear detects some common bugs: https://github.com/PyCQA/flake8-bugbear
- repo: https://github.com/pyCQA/flake8
rev: 7.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.7
hooks:
- id: flake8
args: [--config, .flake8, --verbose, ecobidas, tests, macros]
additional_dependencies: [flake8-docstrings, flake8-use-fstring, flake8-functions, flake8-bugbear]
# Run the linter.
- id: ruff-check
# args: [--statistics]
args: [--fix, --show-fixes]
# Run the formatter.
- id: ruff-format
# args: [--diff]
11 changes: 5 additions & 6 deletions ecobidas/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def cli(argv: Sequence[str] = sys.argv) -> None:
args, unknowns = parser.parse_known_args(argv[1:])
if unknowns:
logger.error(f"The following arguments are unknown: {unknowns}")
exit(1)
sys.exit(1)

verbosity = args.verbosity
set_verbosity(verbosity)
Expand All @@ -46,21 +46,20 @@ def cli(argv: Sequence[str] = sys.argv) -> None:
if isinstance(output_dir, list):
output_dir = output_dir[0]
convert(schema, output_dir, repo, branch)
exit(0)
sys.exit(0)

if args.command in ["update"]:
schema = args.schema[0]
download_spreadsheet(schema=schema)
exit(0)
sys.exit(0)

if args.command in ["serve"]:
folder = args.folder if args.folder is None else args.folder[0]
serve(folder=folder)
exit(0)
sys.exit(0)


def convert(schema: str, output_dir: str, repo: str, branch: str) -> None:

logger.debug(f"{schema=}, {output_dir=}, {repo=}, {branch=}")

spreadsheets_info = get_spreadsheets_info()
Expand All @@ -69,7 +68,7 @@ def convert(schema: str, output_dir: str, repo: str, branch: str) -> None:

if not schema_list:
logger.error(
f"No known schema for: {schema=}" f"Known schemas are: {list(spreadsheets_info.keys())}"
f"No known schema for: {schema=}Known schemas are: {list(spreadsheets_info.keys())}"
)

for this_schema in schema_list:
Expand Down
27 changes: 13 additions & 14 deletions ecobidas/create_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math
import os
from pathlib import Path

import pandas as pd
Expand Down Expand Up @@ -59,8 +58,8 @@ def create_schema(
if debug:
activities = [1]

for i, activity_idx in enumerate(activities):
this_activity = df["activity_order"] == activities[i]
for activity_idx in activities:
this_activity = df["activity_order"] == activity_idx
items = df[this_activity]
included_items = items["include"] == 1
items = items[included_items]
Expand All @@ -85,14 +84,14 @@ def create_schema(

item_info = get_item_info(this_item)

if "message" in item_info and item_info["message"]:
if item_info.get("message"):
activity.messages.append(item_info["message"])

print_item_info(activity_idx, item_idx, item_info)
print_item_info(str(activity_idx), item_idx, item_info)

item = define_new_item(item_info)

item.write(os.path.join(activity_path, "items"))
item.write(Path(activity_path) / "items")

activity.append_item(item)

Expand Down Expand Up @@ -150,7 +149,7 @@ def initialize_activity(items: pd.DataFrame, output_dir: str | Path) -> tuple[Ac


def get_activity_preamble(items: pd.DataFrame) -> str:
if "preamble" not in items.keys():
if "preamble" not in items:
return ""

not_nan = items["preamble"].notna()
Expand Down Expand Up @@ -182,7 +181,7 @@ def create_response_options(
print_info(
"response options",
schema_info["basename"],
os.path.join(output_dir, response_options.at_id),
str(Path(output_dir) / response_options.at_id),
)


Expand All @@ -197,12 +196,12 @@ def make_preamble(schema_info: dict[str, str], items: pd.DataFrame) -> str:
if not preamble:
return preamble

info = dict(
preamble=preamble,
xls=schema_info["link"],
repo="",
citation="",
)
info = {
"preamble": preamble,
"xls": schema_info["link"],
"repo": "",
"citation": "",
}

if schema_info["repo"]:
info["repo"] = schema_info["repo"]
Expand Down
16 changes: 6 additions & 10 deletions ecobidas/download_tsv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Download the content of the different google spreadsheet in the inputs folder."""

import ast
import json
from pathlib import Path
Expand All @@ -12,8 +13,7 @@


# Function to download spreadsheet
def download_spreadsheet(schema: str, output_dir: Path = None) -> None:

def download_spreadsheet(schema: str, output_dir: Path | None = None) -> None:
if output_dir is None:
output_dir = get_input_dir()

Expand All @@ -33,7 +33,6 @@ def download_spreadsheet(schema: str, output_dir: Path = None) -> None:

# Iterate through entries and download spreadsheets
for google_id, subfolder, output_filename in zip(google_ids, subfolders, output_filenames):

output_folder = output_dir / subfolder
output_folder.mkdir(exist_ok=True, parents=True)
output_file = output_folder / f"{output_filename}.tsv"
Expand All @@ -48,7 +47,7 @@ def download_spreadsheet(schema: str, output_dir: Path = None) -> None:
f"https://docs.google.com/spreadsheets/d/{google_id}/export?format=tsv"
)
if response.status_code == 200:
with open(output_file, "wb") as tsv_file:
with output_file.open("wb") as tsv_file:
tsv_file.write(response.content)
else:
logger.error("Error downloading the spreadsheet.")
Expand All @@ -69,7 +68,7 @@ def validate_downloaded_file(file: str | Path) -> None:
df = pd.read_csv(file, sep="\t")

data_dictionary_file = get_input_dir() / "data-dictionary.json"
with open(data_dictionary_file) as f:
with data_dictionary_file.open() as f:
data_dictionary = json.load(f)

columns = {x for x in df.columns if "Unnamed:" not in x}
Expand All @@ -96,11 +95,8 @@ def validate_downloaded_file(file: str | Path) -> None:
f"\nThe following columns are missing from the data dictionary: {sorted(extra_columns)}"
)

invalid_vis = []
visibility = df.visibility.values
for vis in visibility:
if not is_valid_python(vis):
invalid_vis.append(vis)
visibility = df.visibility.to_numpy()
invalid_vis = [vis for vis in visibility if not is_valid_python(vis)]
if invalid_vis:
logger.warning(f"\nThe following visibility are not valid python:\n{invalid_vis}")

Expand Down
10 changes: 5 additions & 5 deletions ecobidas/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


def set_item_name(this_item: dict) -> str:
if "item" not in this_item.keys():
item_name = convert_to_str(this_item["item_pref_label"])
elif isinstance(convert_to_str(this_item["item"]), float):
item_name = convert_to_str(this_item["item_pref_label"])
elif convert_to_str(this_item["item"]) == "":
if (
"item" not in this_item
or isinstance(convert_to_str(this_item["item"]), float)
or convert_to_str(this_item["item"]) == ""
):
item_name = convert_to_str(this_item["item_pref_label"])
else:
item_name = convert_to_str(this_item["item"])
Expand Down
18 changes: 9 additions & 9 deletions ecobidas/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ def main(output_dir: Path | None = None) -> None:
output_dir = Path() / "output"
output_dir.mkdir(exist_ok=True, parents=True)

with open(output_dir / "preset_responses_table.md", "w") as out:
with (output_dir / "preset_responses_table.md").open("w") as out:
out.write(f"{table_preset_responses()}")

with open(output_dir / "apps_table.md", "w") as out:
with (output_dir / "apps_table.md").open("w") as out:
out.write(f"{table_apps()}")

with open(output_dir / "spreadsheets_table.md", "w") as out:
with (output_dir / "spreadsheets_table.md").open("w") as out:
out.write(f"{table_spreadsheets()}")

with open(output_dir / "data_dictionary_table.md", "w") as out:
with (output_dir / "data_dictionary_table.md").open("w") as out:
out.write(f"{table_data_dictionary()}")


def table_data_dictionary() -> str:
"""Create markdown table for list of apps."""
data_dictionary_file = get_input_dir() / "data-dictionary.json"
with open(data_dictionary_file) as f:
with data_dictionary_file.open() as f:
data_dictionary = list(json.load(f).values())
data_dictionary = sorted(data_dictionary, key=itemgetter("VariableName"))
for i, _ in enumerate(data_dictionary):
Expand Down Expand Up @@ -74,9 +74,9 @@ def table_spreadsheets() -> str:
for i, value in enumerate(sheets):
if "meeg" in value["dir"]:
continue
sheets[i][
"preview"
] = f"{PREVIEW_BASE}{BASE_RAW_URL}/{value['dir']}/activities/{value['basename']}/{value['basename']}_schema.jsonld"
sheets[i]["preview"] = (
f"{PREVIEW_BASE}{BASE_RAW_URL}/{value['dir']}/activities/{value['basename']}/{value['basename']}_schema.jsonld"
)

TemplateManager.initialize()
template = TemplateManager.env.get_template("spreadsheet_table.j2")
Expand All @@ -86,7 +86,7 @@ def table_spreadsheets() -> str:
def table_apps() -> str:
"""Create markdown table for spreadsheets that are not apps or preset responses."""
spreadsheets_info = get_spreadsheets_info()
apps = [value for key, value in spreadsheets_info.items() if spreadsheets_info[key]["app_link"]]
apps = [value for key, value in spreadsheets_info.items() if value["app_link"]]
apps = sorted(apps, key=itemgetter("basename"))

TemplateManager.initialize()
Expand Down
8 changes: 5 additions & 3 deletions ecobidas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_output_dir(this_schema: str | Path, output_dir: str | Path) -> Path:
@lru_cache
def get_spreadsheets_info() -> dict[str, dict[str, str]]:
"""Load info about all the spreadsheets."""
with open(get_input_dir() / "spreadsheet_google_id.yml") as f:
with (get_input_dir() / "spreadsheet_google_id.yml").open() as f:
spreadsheets_info = yaml.load(f)

for key in spreadsheets_info:
Expand Down Expand Up @@ -87,7 +87,7 @@ def load_data(this_schema: str) -> pd.DataFrame:
schema_info = get_schema_info(this_schema)
input_file = get_input_file(schema_info)

logger.info(f"\nLoading: {str(input_file)}\n")
logger.info(f"\nLoading: {input_file!s}\n")

return pd.read_csv(input_file, sep="\t")

Expand All @@ -112,7 +112,9 @@ def print_info(type: str, pref_label: str, file: str) -> None:

def print_item_info(activity_idx: str, item_idx: str, item_info: dict[str, str]) -> None:
logger.debug(
f"Activity: {int(activity_idx)} Item: {int(item_idx)}\t{item_info['name']}\t{item_info['field_type']}\t{item_info['visibility']}"
f"Activity: {int(activity_idx)} "
f"Item: {int(item_idx)}\t{item_info['name']}"
f"\t{item_info['field_type']}\t{item_info['visibility']}"
)


Expand Down
2 changes: 1 addition & 1 deletion macros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
__all__ = [
"define_env",
"table_apps",
"table_data_dictionary",
"table_preset_responses",
"table_spreadsheets",
"table_data_dictionary",
]
Loading
Loading