Move comment support out of comfy.choices and into comfy.comments module.

This commit is contained in:
Barry Downes 2023-08-05 17:08:11 +10:00
parent dcb4bdfb9b
commit 57ae0987db
3 changed files with 72 additions and 24 deletions

View File

@ -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:

67
comfy/comments.py Normal file
View File

@ -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)

View File

@ -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 = {