Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
section = re.sub("[%]|(\s+)", "", parts[0]).lower()
if section not in sections:
bot.exit("%s is not a valid section." % section)
config = add_section(
config=config, section=section, section_name=section_name
)
# A %fields action (only field allowed), %values allows split
elif line.upper().startswith(group_actions) and section in groups:
config = parse_group_action(
section=section, section_name=section_name, line=line, config=config
)
# An action (ADD, BLANK, JITTER, KEEP, REPLACE, REMOVE, LABEL)
elif line.upper().startswith(actions):
# Start of a filter group
if line.upper().startswith("LABEL") and section == "filter":
members = parse_filter_group(spec)
# Add the filter label to the config
config = parse_label(
config=config,
section=section,
label=line,
section_name=section_name,
members=members,
)
# Parse the action
else:
config = parse_config_action(
def parse_config_action(section, line, config, section_name=None):
"""add action will take a line from a deid config file, a config (dictionary), and
an active section name (eg header) and add an entry to the config file to perform
the action.
Parameters
=========
section: a valid section name from the deid config file
line: the line content to parse for the section/action
config: the growing/current config dictionary
section_name: optionally, a section name
"""
if not line.upper().startswith(actions):
bot.exit("%s is not a valid action line." % line)
# We may have to deal with cases of spaces
parts = line.split(" ")
action = parts.pop(0).replace(" ", "")
# What field is the action for?
if len(parts) < 1:
bot.exit("%s requires a FIELD value, but not found." % action)
field = parts.pop(0)
# Actions that require a value
if action in ["ADD", "REPLACE", "JITTER"]:
if len(parts) == 0:
bot.exit("%s requires a VALUE, but not found" % action)
def _perform_action(field,item,action,value=None):
'''_perform_action is the base function for performing an action.
It is equivalent to the dicom module version, except we work with
dictionary field/value instead of dicom headers.
If no action is done, None is returned
'''
done = False
if action not in valid_actions:
bot.warning('%s in not a valid choice [%s]. Defaulting to blanked.' %(action,
".".join(valid_actions)))
action = "BLANK"
if field in item and action != "ADD":
# Blank the value
if action == "BLANK":
item[field] = ""
done = True
# Code the value with something in the response
elif action == "REPLACE":
value = parse_value(item,value)
if value is not None:
done = True
def perform_action(self, field, value, action):
"""perform action takes an action (dictionary with field, action, value)
and performs the action on the loaded dicom.
Parameters
==========
fields: if provided, a filtered list of fields for expand
action: the action from the parsed deid to take
"field" (eg, PatientID) the header field to process
"action" (eg, REPLACE) what to do with the field
"value": if needed, the field from the response to replace with
"""
# Validate the action
if action not in valid_actions:
bot.warning("%s in not a valid choice. Defaulting to blanked." % action)
action = "BLANK"
# A values list returns fields with the value (can be private tags if not removed)
if re.search("^values", field):
values = self.lookup.get(re.sub("^values:", "", field), [])
fields = self.find_by_values(values=values)
# A fields list is used vertabim
elif re.search("^fields", field):
listing = {}
for uid, contender in self.lookup.get(
re.sub("^fields:", "", field), {}
).items():
listing.update(
expand_field_expression(
def _perform_action(field,item,action,value=None):
'''_perform_action is the base function for performing an action.
It is equivalent to the dicom module version, except we work with
dictionary field/value instead of dicom headers.
If no action is done, None is returned
'''
done = False
if action not in valid_actions:
bot.warning('%s in not a valid choice [%s]. Defaulting to blanked.' %(action,
".".join(valid_actions)))
action = "BLANK"
if field in item and action != "ADD":
# Blank the value
if action == "BLANK":
item[field] = ""
done = True
# Code the value with something in the response
elif action == "REPLACE":
value = parse_value(item,value)
if value is not None:
done = True
item[field] = value
else: