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 Jan 19, 2024
1 parent 003e850 commit 37853b2
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions reproschema/redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

matrix_group_count = {}


def clean_header(header):
return {k.lstrip('\ufeff'): v for k, v in header.items()}
return {k.lstrip("\ufeff"): v for k, v in header.items()}


def normalize_condition(condition_str):
re_parentheses = re.compile(r"\(([0-9]*)\)")
Expand Down Expand Up @@ -37,6 +39,7 @@ def process_visibility(data):
}
return visibility_obj


def parse_field_type_and_value(field, input_type_map):
field_type = field.get("Field Type", "")
input_type = input_type_map.get(field_type, field_type)
Expand All @@ -52,10 +55,12 @@ def parse_field_type_and_value(field, input_type_map):
"time_": "xsd:time",
"email": "xsd:string",
"phone": "xsd:string",
} # todo: input_type="signature"
} # todo: input_type="signature"

# Get the validation type from the field, if available
validation_type = field.get("Text Validation Type OR Show Slider Number", "").strip()
validation_type = field.get(
"Text Validation Type OR Show Slider Number", ""
).strip()

if validation_type:
# Map the validation type to an XSD type if it's in the map
Expand All @@ -66,15 +71,18 @@ def parse_field_type_and_value(field, input_type_map):

return input_type, value_type


def process_choices(field_type, choices_str):
if field_type not in ['radio', 'dropdown']: # Handle only radio and dropdown types
if field_type not in ["radio", "dropdown"]: # Handle only radio and dropdown types
return None

choices = []
for choice in choices_str.split("|"):
parts = choice.split(", ")
if len(parts) < 2:
print(f"Warning: Skipping invalid choice format '{choice}' in a {field_type} field")
print(
f"Warning: Skipping invalid choice format '{choice}' in a {field_type} field"
)
continue

# Try to convert the first part to an integer, if it fails, keep it as a string
Expand All @@ -90,6 +98,7 @@ def process_choices(field_type, choices_str):
choices.append(choice_obj)
return choices


def write_to_file(abs_folder_path, form_name, field_name, rowData):
file_path = os.path.join(
f"{abs_folder_path}", "activities", form_name, "items", f"{field_name}"
Expand Down Expand Up @@ -117,7 +126,9 @@ def parse_html(input_string, default_language="en"):
if not result: # If no text was extracted
result[default_language] = soup.get_text(strip=True)
else:
result[default_language] = soup.get_text(strip=True) # Use the entire text as default language text
result[default_language] = soup.get_text(
strip=True
) # Use the entire text as default language text

return result

Expand All @@ -136,7 +147,9 @@ def process_row(
global matrix_group_count
matrix_group_name = field.get("Matrix Group Name", "")
if matrix_group_name:
matrix_group_count[matrix_group_name] = matrix_group_count.get(matrix_group_name, 0) + 1
matrix_group_count[matrix_group_name] = (
matrix_group_count.get(matrix_group_name, 0) + 1
)
item_id = f"{matrix_group_name}_{matrix_group_count[matrix_group_name]}"
else:
item_id = field.get("Variable / Field Name", "")
Expand All @@ -146,7 +159,7 @@ def process_row(
"@type": "reproschema:Field",
"@id": item_id,
"prefLabel": item_id,
"description": f"{item_id} of {form_name}"
"description": f"{item_id} of {form_name}",
}

field_type = field.get("Field Type", "")
Expand All @@ -162,16 +175,16 @@ def process_row(
if field_type == "yesno":
rowData["responseOptions"] = {
"valueType": "xsd:boolean",
"choices": [
{"name": "Yes", "value": 1},
{"name": "No", "value": 0}
]
"choices": [{"name": "Yes", "value": 1}, {"name": "No", "value": 0}],
}

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

elif schema_map.get(key) == "allow" and value:
rowData.setdefault("ui", {}).update({schema_map[key]: value.split(", ")})

Expand All @@ -191,7 +204,6 @@ def process_row(
{"choices": process_choices(field_type, value)}
)


elif schema_map.get(key) == "scoringLogic" and value:
condition = normalize_condition(value)
rowData.setdefault("ui", {}).update({"hidden": True})
Expand Down

0 comments on commit 37853b2

Please sign in to comment.