Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_group_action(section, line, config, section_name):
"""parse a group action, either FIELD or SPLIT, which must belong to
either a fields or values section.
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(group_actions):
bot.exit("%s is not a valid group action." % line)
if not line.upper().startswith("FIELD") and section == "fields":
bot.exit("%fields only supports FIELD actions.")
# We may have to deal with cases of spaces
bot.debug("%s: adding %s" % (section, line))
parts = line.split(" ")
action = parts.pop(0).replace(" ", "")
# Both require some parts
if not parts:
bot.exit("%s action %s requires additional arguments" % (section, action))
# For both, the second is always a field or field expander
field = parts.pop(0)
# Fields supports one or more fields with expanders (no third arguments)
if section == "fields":
config[section][section_name].append({"action": action, "field": field})
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)
value = _remove_comments(parts)
bot.debug("%s: adding %s" % (section, line))
config[section].append({"action": action, "field": field, "value": value})
# Actions that can optionally have a value
elif action in ["REMOVE"]:
bot.debug("%s: adding %s" % (section, line))
# Case 1: removing without any criteria
if len(parts) == 0:
config[section].append({"action": action, "field": field})
# Case 2: REMOVE can have a func:is_thing to return boolean
else:
value = _remove_comments(parts)
"""add section will add a section (and optionally)
section name to a config
Parameters
==========
config: the config (dict) parsed thus far
section: the section name to add
section_name: an optional name, added as a level
"""
if section is None:
bot.exit("You must define a section (e.g. %header) before any action.")
if section in ["filter", "values", "fields"] and section_name is None:
bot.exit("You must provide a name for a filter section.")
if section not in sections:
bot.exit("%s is not a valid section." % section)
if section not in config:
# If a section is named, we have more one level (dict)
if section_name is not None:
config[section] = OrderedDict()
config[section][section_name] = []
bot.debug("Adding section %s %s" % (section, section_name))
else:
config[section] = []
bot.debug("Adding section %s" % section)
return config
def add_section(config, section, section_name=None):
"""add section will add a section (and optionally)
section name to a config
Parameters
==========
config: the config (dict) parsed thus far
section: the section name to add
section_name: an optional name, added as a level
"""
if section is None:
bot.exit("You must define a section (e.g. %header) before any action.")
if section in ["filter", "values", "fields"] and section_name is None:
bot.exit("You must provide a name for a filter section.")
if section not in sections:
bot.exit("%s is not a valid section." % section)
if section not in config:
# If a section is named, we have more one level (dict)
if section_name is not None:
config[section] = OrderedDict()
config[section][section_name] = []
bot.debug("Adding section %s %s" % (section, section_name))
else:
config[section] = []
Parameters
==========
config: the config (dict) parsed thus far
section: the section name to add
section_name: an optional name, added as a level
"""
if section is None:
bot.exit("You must define a section (e.g. %header) before any action.")
if section in ["filter", "values", "fields"] and section_name is None:
bot.exit("You must provide a name for a filter section.")
if section not in sections:
bot.exit("%s is not a valid section." % section)
if section not in config:
# If a section is named, we have more one level (dict)
if section_name is not None:
config[section] = OrderedDict()
config[section][section_name] = []
bot.debug("Adding section %s %s" % (section, section_name))
else:
config[section] = []
bot.debug("Adding section %s" % section)
return config
# Section is in config
if section_name is not None and section_name not in config[section]:
config[section][section_name] = []
--> This is what a flagged image looks like:
{'flagged': True,
'results': [
{'reason': ' ImageType missing or ImageType empty ',
'group': 'blacklist',
'coordinates': []}
]
}
"""
dicom = read_file(dicom_file, force=force)
# Load criteria (actions) for flagging
filters = deid.get_filters()
if not filters:
bot.exit("Deid provided does not have %filter, exiting.")
# Return list with lookup as dicom_file
results = []
global_flagged = False
for name, items in filters.items():
for item in items:
flags = []
descriptions = [] # description for each group across items
for group in item["filters"]:
group_flags = [] # evaluation for a single line
group_descriptions = []
# You cannot pop from the list
if len(contenders) == 0:
bot.warning(
"No deid settings files found in %s, will use default dicom.deid."
% path
)
contenders.append(default_deid)
elif len(contenders) > 1:
bot.warning("Multiple deid files found in %s, will use first." % (path))
path = contenders[0]
# We have a file path at this point
if not os.path.exists(path):
bot.exit("Cannot find deid file %s, exiting." % (path))
return path
# Split the statement based on found operator
operator = operator.replace("||", "or").replace("+", "and")
operators.append(operator)
else:
member = operator.join([member, rest])
# Parse the member
action, member = member.split(" ", 1)
action = action.lower().strip()
# Contains, notcontains, equals, not equals expects FieldName Values
if action in ["contains", "notcontains", "equals", "notequals"]:
try:
field, value = member.split(" ", 1)
except ValueError:
bot.exit(
"%s for line %s must have field and values, exiting."
% (action, member)
)
# Missing, empty, expect only a field
elif action in ["missing", "empty", "present"]:
field = member.strip()
else:
bot.exit("%s is not a valid filter action." % action)
actions.append(action)
fields.append(field.strip())
if value is not None:
values.append(value.strip())