Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 21, 2024
1 parent 51d30b7 commit ab7c051
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
28 changes: 16 additions & 12 deletions reproschema/redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

matrix_group_count = {}


def clean_header(header):
cleaned_header = {}
for k, v in header.items():
# Strip BOM, whitespace, and enclosing quotation marks if present
cleaned_key = k.lstrip('\ufeff').strip().strip('"')
cleaned_key = k.lstrip("\ufeff").strip().strip('"')
cleaned_header[cleaned_key] = v
return cleaned_header

Expand All @@ -24,24 +25,28 @@ def normalize_condition(condition_str):
re_brackets = re.compile(r"\[([^\]]*)\]")
re_extra_spaces = re.compile(r"\s+")
re_double_quotes = re.compile(r'"')
re_or = re.compile(r'\bor\b') # Match 'or' as whole word
re_or = re.compile(r"\bor\b") # Match 'or' as whole word

# Apply regex replacements
condition_str = re_parentheses.sub(r"___\1", condition_str)
condition_str = re_non_gt_lt_equal.sub(r"\1 ==", condition_str)
condition_str = re_brackets.sub(r" \1 ", condition_str)

# Replace 'or' with '||', ensuring not to replace '||'
condition_str = re_or.sub('||', condition_str)
condition_str = re_or.sub("||", condition_str)

# Replace 'and' with '&&'
condition_str = condition_str.replace(" and ", " && ")

# Trim extra spaces and replace double quotes with single quotes
condition_str = re_extra_spaces.sub(' ', condition_str).strip() # Reduce multiple spaces to a single space
condition_str = re_double_quotes.sub("'", condition_str) # Replace double quotes with single quotes
condition_str = re_extra_spaces.sub(
" ", condition_str
).strip() # Reduce multiple spaces to a single space
condition_str = re_double_quotes.sub(
"'", condition_str
) # Replace double quotes with single quotes

return condition_str.strip()
return condition_str.strip()


def process_visibility(data):
Expand Down Expand Up @@ -116,7 +121,7 @@ def process_choices(field_type, choices_str):
value = parts[0]

choice_obj = {"name": " ".join(parts[1:]), "value": value}
# remove image for now
# remove image for now
# if len(parts) == 3:
# # Handle image url
# choice_obj["image"] = f"{parts[2]}.png"
Expand Down Expand Up @@ -204,10 +209,7 @@ def process_row(
}

for key, value in field.items():
if (
schema_map.get(key) in ["question", "description", "preamble"]
and value
):
if schema_map.get(key) in ["question", "description", "preamble"] and value:
rowData.update({schema_map[key]: parse_html(value)})

elif schema_map.get(key) == "allow" and value:
Expand Down Expand Up @@ -349,7 +351,9 @@ def create_protocol_schema(
"variableName": f"{activity}_schema",
# Assuming activity name as prefLabel, update as needed
"prefLabel": activity.replace("_", " ").title(),
"isVis": protocol_visibility_obj.get(activity, True), # Default to True if not specified
"isVis": protocol_visibility_obj.get(
activity, True
), # Default to True if not specified
}
protocol_schema["ui"]["addProperties"].append(add_property)
# Add the full path to the order list
Expand Down
15 changes: 11 additions & 4 deletions reproschema/reproschema2redcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ def get_csv_data(dir_path):
activity_order = parsed_protocol_json.get("ui", {}).get("order", [])
for relative_activity_path in activity_order:
# Normalize the relative path and construct the absolute path
normalized_relative_path = Path(relative_activity_path.lstrip("../"))
normalized_relative_path = Path(
relative_activity_path.lstrip("../")
)

activity_path = (
dir_path
/ "activities"
/ normalized_relative_path
/ (normalized_relative_path.name + "_schema")
)

activity_path = dir_path / "activities" / normalized_relative_path / (normalized_relative_path.name + "_schema")

parsed_activity_json = read_json_file(activity_path)

if parsed_activity_json:
Expand Down Expand Up @@ -239,4 +246,4 @@ def main(input_dir_path, output_csv_filename):
sys.exit(1)
input_dir_path = Path(sys.argv[1])
output_csv_filename = sys.argv[2]
main(input_dir_path, output_csv_filename)
main(input_dir_path, output_csv_filename)
20 changes: 15 additions & 5 deletions reproschema/tests/test_redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
import yaml
from click.testing import CliRunner
from ..cli import main
from ..cli import main

CSV_FILE_NAME = "redcap_dict.csv"
YAML_FILE_NAME = "redcap2rs.yaml"
Expand All @@ -14,6 +14,7 @@
os.path.dirname(__file__), "test_redcap2rs_data", YAML_FILE_NAME
)


def test_redcap2reproschema(tmpdir):
runner = CliRunner()

Expand All @@ -26,13 +27,22 @@ def test_redcap2reproschema(tmpdir):
# Change the current working directory to tmpdir
with tmpdir.as_cwd():
# Read YAML to find the expected output directory name
with open(str(temp_yaml_file), 'r') as file: # Convert to string
with open(str(temp_yaml_file), "r") as file: # Convert to string
protocol = yaml.safe_load(file)
protocol_name = protocol.get("protocol_name", "").replace(" ", "_")

result = runner.invoke(
main, ["redcap2reproschema", str(temp_csv_file), str(temp_yaml_file)] # Convert to string
main,
[
"redcap2reproschema",
str(temp_csv_file),
str(temp_yaml_file),
], # Convert to string
)

assert result.exit_code == 0, f"The command failed to execute successfully: {result.output}"
assert os.path.isdir(protocol_name), f"Expected output directory '{protocol_name}' does not exist"
assert (
result.exit_code == 0
), f"The command failed to execute successfully: {result.output}"
assert os.path.isdir(
protocol_name
), f"Expected output directory '{protocol_name}' does not exist"
2 changes: 1 addition & 1 deletion reproschema/tests/test_redcap2rs_data/redcap2rs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ protocol_display_name: "redcap protocols"
# Provide a brief description of your protocol.
protocol_description: "testing" # Example: "This protocol is for ..."

redcap_version: "3.0.0"
redcap_version: "3.0.0"
9 changes: 6 additions & 3 deletions reproschema/tests/test_reproschema2redcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
import csv


def test_reproschema2redcap(tmpdir):
runner = CliRunner()

Expand All @@ -16,7 +17,7 @@ def test_reproschema2redcap(tmpdir):
)
copytree(original_data_dir, "input_data")

input_path = Path("input_data")
input_path = Path("input_data")
output_csv_path = os.path.join(tmpdir, "output.csv")

result = runner.invoke(
Expand All @@ -33,7 +34,9 @@ def test_reproschema2redcap(tmpdir):
reader = csv.reader(csv_file)
csv_contents = list(reader)

assert len(csv_contents) > 1 # More than one row indicates content beyond headers
assert (
len(csv_contents) > 1
) # More than one row indicates content beyond headers

# Clean up temporary directory after use (optional)
# rmtree(tmpdir)
# rmtree(tmpdir)
2 changes: 1 addition & 1 deletion templates/redcap2rs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ protocol_display_name: "Your protocol display name"
# Provide a brief description of your protocol.
protocol_description: "Description for your protocol" # Example: "This protocol is for ..."

redcap_version: "x.y.z" # Example: "3.0.0"
redcap_version: "x.y.z" # Example: "3.0.0"

0 comments on commit ab7c051

Please sign in to comment.