Skip to content

Commit

Permalink
Improve logic for regex and macro
Browse files Browse the repository at this point in the history
detection. Throw an error when four
or more ```` appear in a row in the
search field, which is invalid SPL.
  • Loading branch information
pyth0n1c committed Oct 10, 2024
1 parent c627d2e commit d79a0a4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions contentctl/objects/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from contentctl.input.director import DirectorOutputDto
from contentctl.objects.security_content_object import SecurityContentObject


#The following macros are included in commonly-installed apps.
#As such, we will ignore if they are missing from our app.
#Included in
Expand Down Expand Up @@ -55,10 +54,15 @@ def get_macros(text_field:str, director:DirectorOutputDto , ignore_macros:set[st
#If a comment ENDS in a macro, for example ```this is a comment with a macro `macro_here````
#then there is a small edge case where the regex below does not work properly. If that is
#the case, we edit the search slightly to insert a space
text_field = re.sub(r"\`\`\`\`", r"` ```", text_field)
text_field = re.sub(r"\`\`\`.*?\`\`\`", " ", text_field)

if re.findall(r"\`\`\`\`", text_field):
raise ValueError("Search contained four or more '`' characters in a row which is invalid SPL"
"This may have occurred when a macro was commented out.\n"
"Please ammend your search to remove the substring '````'")

# replace all the macros with a space
text_field = re.sub(r"\`\`\`[\s\S]*?\`\`\`", " ", text_field)


macros_to_get = re.findall(r'`([^\s]+)`', text_field)
#If macros take arguments, stop at the first argument. We just want the name of the macro
macros_to_get = set([macro[:macro.find('(')] if macro.find('(') != -1 else macro for macro in macros_to_get])
Expand All @@ -68,4 +72,3 @@ def get_macros(text_field:str, director:DirectorOutputDto , ignore_macros:set[st
macros_to_get -= macros_to_ignore
return Macro.mapNamesToSecurityContentObjects(list(macros_to_get), director)


0 comments on commit d79a0a4

Please sign in to comment.