def rewrite_subject_group(root, subjects, subject_group_type, overwrite=True):
parent_tag_name = '<STR_LIT>'<EOL>tag_name = '<STR_LIT>'<EOL>wrap_tag_name = '<STR_LIT>'<EOL>tag_attribute = '<STR_LIT>'<EOL>xpath_parent = '<STR_LIT>'<EOL>xpath_article_meta = '<STR_LIT>'<EOL>xpath = '<STR_LIT>'.format(<EOL>parent_tag_name=parent_tag_name,<EOL>tag_attribute=tag_attribute,<EOL>group_type=subject_group_type)<EOL>count = <NUM_LIT:0><EOL>parent_tag = root.find(xpath_parent)<EOL>if parent_tag is None:<EOL><INDENT>wrap_tag = root.find(xpath_article_meta)<EOL>article_categories_tag = SubElement(wrap_tag, wrap_tag_name)<EOL>parent_tag = article_categories_tag<EOL><DEDENT>insert_index = <NUM_LIT:0><EOL>if parent_tag is not None:<EOL><INDENT>for tag_index, tag in enumerate(parent_tag.findall('<STR_LIT:*>')):<EOL><INDENT>if tag.tag == parent_tag_name and tag.get(tag_attribute) == subject_group_type:<EOL><INDENT>insert_index = tag_index<EOL>if overwrite is True:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if overwrite is not True:<EOL><INDENT>insert_index += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>if overwrite is True:<EOL><INDENT>for tag in root.findall(xpath):<EOL><INDENT>parent_tag.remove(tag)<EOL><DEDENT><DEDENT>for subject in subjects:<EOL><INDENT>subj_group_tag = Element(parent_tag_name)<EOL>subj_group_tag.set(tag_attribute, subject_group_type)<EOL>subject_tag = SubElement(subj_group_tag, tag_name)<EOL>subject_tag.text = subject<EOL>parent_tag.insert(insert_index, subj_group_tag)<EOL>count += <NUM_LIT:1><EOL>insert_index += <NUM_LIT:1><EOL><DEDENT>return count<EOL>
add or rewrite subject tags inside subj-group tags