Skip to content

Commit ab7c051

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 51d30b7 commit ab7c051

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

reproschema/redcap2reproschema.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
matrix_group_count = {}
1010

11+
1112
def clean_header(header):
1213
cleaned_header = {}
1314
for k, v in header.items():
1415
# Strip BOM, whitespace, and enclosing quotation marks if present
15-
cleaned_key = k.lstrip('\ufeff').strip().strip('"')
16+
cleaned_key = k.lstrip("\ufeff").strip().strip('"')
1617
cleaned_header[cleaned_key] = v
1718
return cleaned_header
1819

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

2930
# Apply regex replacements
3031
condition_str = re_parentheses.sub(r"___\1", condition_str)
3132
condition_str = re_non_gt_lt_equal.sub(r"\1 ==", condition_str)
3233
condition_str = re_brackets.sub(r" \1 ", condition_str)
3334

3435
# Replace 'or' with '||', ensuring not to replace '||'
35-
condition_str = re_or.sub('||', condition_str)
36+
condition_str = re_or.sub("||", condition_str)
3637

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

4041
# Trim extra spaces and replace double quotes with single quotes
41-
condition_str = re_extra_spaces.sub(' ', condition_str).strip() # Reduce multiple spaces to a single space
42-
condition_str = re_double_quotes.sub("'", condition_str) # Replace double quotes with single quotes
42+
condition_str = re_extra_spaces.sub(
43+
" ", condition_str
44+
).strip() # Reduce multiple spaces to a single space
45+
condition_str = re_double_quotes.sub(
46+
"'", condition_str
47+
) # Replace double quotes with single quotes
4348

44-
return condition_str.strip()
49+
return condition_str.strip()
4550

4651

4752
def process_visibility(data):
@@ -116,7 +121,7 @@ def process_choices(field_type, choices_str):
116121
value = parts[0]
117122

118123
choice_obj = {"name": " ".join(parts[1:]), "value": value}
119-
# remove image for now
124+
# remove image for now
120125
# if len(parts) == 3:
121126
# # Handle image url
122127
# choice_obj["image"] = f"{parts[2]}.png"
@@ -204,10 +209,7 @@ def process_row(
204209
}
205210

206211
for key, value in field.items():
207-
if (
208-
schema_map.get(key) in ["question", "description", "preamble"]
209-
and value
210-
):
212+
if schema_map.get(key) in ["question", "description", "preamble"] and value:
211213
rowData.update({schema_map[key]: parse_html(value)})
212214

213215
elif schema_map.get(key) == "allow" and value:
@@ -349,7 +351,9 @@ def create_protocol_schema(
349351
"variableName": f"{activity}_schema",
350352
# Assuming activity name as prefLabel, update as needed
351353
"prefLabel": activity.replace("_", " ").title(),
352-
"isVis": protocol_visibility_obj.get(activity, True), # Default to True if not specified
354+
"isVis": protocol_visibility_obj.get(
355+
activity, True
356+
), # Default to True if not specified
353357
}
354358
protocol_schema["ui"]["addProperties"].append(add_property)
355359
# Add the full path to the order list

reproschema/reproschema2redcap.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,17 @@ def get_csv_data(dir_path):
150150
activity_order = parsed_protocol_json.get("ui", {}).get("order", [])
151151
for relative_activity_path in activity_order:
152152
# Normalize the relative path and construct the absolute path
153-
normalized_relative_path = Path(relative_activity_path.lstrip("../"))
153+
normalized_relative_path = Path(
154+
relative_activity_path.lstrip("../")
155+
)
156+
157+
activity_path = (
158+
dir_path
159+
/ "activities"
160+
/ normalized_relative_path
161+
/ (normalized_relative_path.name + "_schema")
162+
)
154163

155-
activity_path = dir_path / "activities" / normalized_relative_path / (normalized_relative_path.name + "_schema")
156-
157164
parsed_activity_json = read_json_file(activity_path)
158165

159166
if parsed_activity_json:
@@ -239,4 +246,4 @@ def main(input_dir_path, output_csv_filename):
239246
sys.exit(1)
240247
input_dir_path = Path(sys.argv[1])
241248
output_csv_filename = sys.argv[2]
242-
main(input_dir_path, output_csv_filename)
249+
main(input_dir_path, output_csv_filename)

reproschema/tests/test_redcap2reproschema.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
import yaml
55
from click.testing import CliRunner
6-
from ..cli import main
6+
from ..cli import main
77

88
CSV_FILE_NAME = "redcap_dict.csv"
99
YAML_FILE_NAME = "redcap2rs.yaml"
@@ -14,6 +14,7 @@
1414
os.path.dirname(__file__), "test_redcap2rs_data", YAML_FILE_NAME
1515
)
1616

17+
1718
def test_redcap2reproschema(tmpdir):
1819
runner = CliRunner()
1920

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

3334
result = runner.invoke(
34-
main, ["redcap2reproschema", str(temp_csv_file), str(temp_yaml_file)] # Convert to string
35+
main,
36+
[
37+
"redcap2reproschema",
38+
str(temp_csv_file),
39+
str(temp_yaml_file),
40+
], # Convert to string
3541
)
3642

37-
assert result.exit_code == 0, f"The command failed to execute successfully: {result.output}"
38-
assert os.path.isdir(protocol_name), f"Expected output directory '{protocol_name}' does not exist"
43+
assert (
44+
result.exit_code == 0
45+
), f"The command failed to execute successfully: {result.output}"
46+
assert os.path.isdir(
47+
protocol_name
48+
), f"Expected output directory '{protocol_name}' does not exist"

reproschema/tests/test_redcap2rs_data/redcap2rs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ protocol_display_name: "redcap protocols"
1313
# Provide a brief description of your protocol.
1414
protocol_description: "testing" # Example: "This protocol is for ..."
1515

16-
redcap_version: "3.0.0"
16+
redcap_version: "3.0.0"

reproschema/tests/test_reproschema2redcap.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
import csv
88

9+
910
def test_reproschema2redcap(tmpdir):
1011
runner = CliRunner()
1112

@@ -16,7 +17,7 @@ def test_reproschema2redcap(tmpdir):
1617
)
1718
copytree(original_data_dir, "input_data")
1819

19-
input_path = Path("input_data")
20+
input_path = Path("input_data")
2021
output_csv_path = os.path.join(tmpdir, "output.csv")
2122

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

36-
assert len(csv_contents) > 1 # More than one row indicates content beyond headers
37+
assert (
38+
len(csv_contents) > 1
39+
) # More than one row indicates content beyond headers
3740

3841
# Clean up temporary directory after use (optional)
39-
# rmtree(tmpdir)
42+
# rmtree(tmpdir)

templates/redcap2rs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ protocol_display_name: "Your protocol display name"
1313
# Provide a brief description of your protocol.
1414
protocol_description: "Description for your protocol" # Example: "This protocol is for ..."
1515

16-
redcap_version: "x.y.z" # Example: "3.0.0"
16+
redcap_version: "x.y.z" # Example: "3.0.0"

0 commit comments

Comments
 (0)