From 57ae0987dbd3b1de09c2910de2f5f0a244b254eb Mon Sep 17 00:00:00 2001 From: Barry Downes Date: Sat, 5 Aug 2023 17:08:11 +1000 Subject: [PATCH] Move comment support out of comfy.choices and into comfy.comments module. --- comfy/choices.py | 23 +--------------- comfy/comments.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ nodes.py | 6 +++-- 3 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 comfy/comments.py diff --git a/comfy/choices.py b/comfy/choices.py index 2833b246e..fe9e085ff 100644 --- a/comfy/choices.py +++ b/comfy/choices.py @@ -67,28 +67,7 @@ def translate(text, seed=None, strict=True, reescape=frozenset()): if strict: raise ParseError(openbrace, f"Missing matching closing brace '}}' for earlier open brace '{{'") out.append(chosen_text) - elif m := input.match(r'\/'): - # C-style block "/* */" and line "//" comments - comment = input.prior() - if 0: pass - elif m := input.match(r'\/'): - # // line comment - if not input.match(r'.*?(?:\n|$)'): - if strict: - raise ParseLogicError(comment, f"Failed to find end of C-style // line comment") - input.match(r'.*') # consume unterminated comment (however that might be possible) - out.append('\n') - elif m := input.match(r'\*'): - # /* ... */ block comment - if not input.match(r'.*?\*\/'): - if strict: - raise ParseError(comment, f"Unterminated C-style /* ... */ block comment") - input.match(r'.*') # consume unterminated comment - out.append(' ') - else: - # it was a literal /, not a comment after all - out.append('/'); - elif m := input.match(r'[^\\\{\}\|\/]+'): + elif m := input.match(r'[^\\\{\}\|]+'): # 1 or more non-metacharacters out.append(m.group(0)) else: diff --git a/comfy/comments.py b/comfy/comments.py new file mode 100644 index 000000000..3f46fa9f4 --- /dev/null +++ b/comfy/comments.py @@ -0,0 +1,67 @@ + +import comfy.parse as parse +from comfy.parse import ParseError, ParseLogicError + +def strip_c_comments(text, strict=True): + ''' + Processes the text and strips out any C-style block "/* ... */" or line "// ..." comments found. + ''' + out = [] + input = parse.Cursor(text) + while True: + if 0: pass + elif input.match(r'\/'): + # C-style block "/* */" or line "//" comment + comment = input.prior() + if 0: pass + elif m := input.match(r'\/'): + # // line comment + if not input.match(r'.*?(?:\n|$)'): + if strict: + raise ParseLogicError(comment, f"Failed to find end of C-style // line comment") + input.match(r'.*') # consume unterminated comment (however that might be possible) + out.append('\n') + elif m := input.match(r'\*'): + # /* ... */ block comment + if not input.match(r'.*?\*\/'): + if strict: + raise ParseError(comment, f"Unterminated C-style /* ... */ block comment") + input.match(r'.*') # consume unterminated comment + out.append(' ') + else: + # it was a literal /, not a comment after all + out.append('/'); + elif m := input.match(r'[^/]+'): + out.append(m.group(0)) + elif input.match(r'$'): + break; + else: + raise ParseLogicError(input, f"Failed to match") + + return ''.join(out) + +def strip_hash_comments(text, strict=True): + ''' + Processes the text and strips out any hash "# ... " comments found. + ''' + out = [] + input = parse.Cursor(text) + while True: + if 0: pass + elif input.match(r'\#'): + comment = input.prior() + # scripting-language-style "# ...\n" comment + if not input.match(r'.*?(?:\n|$)'): + if strict: + raise ParseLogicError(comment, f"Failed to find end of script-style # ... comment") + input.match(r'.*') # consume unterminated comment (however that might be possible) + out.append('\n') + elif m := input.match(r'[^#]+'): + out.append(m.group(0)) + elif input.match(r'$'): + break; + else: + raise ParseLogicError(input, f"Failed to match") + + return ''.join(out) + diff --git a/nodes.py b/nodes.py index e6455eb93..21a2054df 100644 --- a/nodes.py +++ b/nodes.py @@ -15,6 +15,7 @@ import numpy as np import safetensors.torch import comfy.choices +import comfy.comments sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy")) @@ -1479,8 +1480,9 @@ class DynamicPrompt: CATEGORY = "conditioning" def dynamic_prompt(self, text, seed): - translated_prompt_text = comfy.choices.translate(text, seed=seed, strict=False, reescape=r'\()') - return (translated_prompt_text,) + text = comfy.comments.strip_c_comments(text) + text = comfy.choices.translate(text, seed=seed, strict=False, reescape=r'\()') + return (text,) NODE_CLASS_MAPPINGS = {