Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def switch(self):
"""
block has been split via | so we need to start a new block for that
option and return it to the user.
"""
base_block = self.base_block or self
self.next_block = Block(
self.parent, base_block=base_block, py3_wrapper=self.py3_wrapper
)
return self.next_block
or getattr(module, threshold_color_name, None)
or getattr(module.py3, color_name.upper(), None)
)
if color == "hidden":
return False, []
text = ""
out = []
if isinstance(output, str):
output = [output]
# merge as much output as we can.
first = True
last_block = None
for index, item in enumerate(output):
is_block = isinstance(item, Block)
if not is_block and item:
last_block = None
if isinstance(item, (str, bool, int, float, bytes)) or item is None:
text += str(item)
continue
elif text:
if not first and (text == "" or out and out[-1].get("color") == color):
out[-1]["full_text"] += text
else:
part = {"full_text": text}
if color:
part["color"] = color
out.append(part)
text = ""
if isinstance(item, Composite):
if color:
def build_block(self, format_string):
"""
Parse the format string into blocks containing Literals, Placeholders
etc that we can cache and reuse.
"""
first_block = Block(None, py3_wrapper=self.py3_wrapper)
block = first_block
# Tokenize the format string and process them
for token in self.tokens(format_string):
value = token.group(0)
if token.group("block_start"):
# Create new block
block = block.new_block()
elif token.group("block_end"):
# Close block setting any valid state as needed
# and return to parent block to continue
if not block.parent:
raise Exception("Too many `]`")
block = block.parent
elif token.group("switch"):
# a new option has been created
def new_block(self):
"""
create a new sub block to the current block and return it.
the sub block is added to the current block.
"""
child = Block(self, py3_wrapper=self.py3_wrapper)
self.add(child)
return child