Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __getitem__(self, key):
if isinstance(key, slice):
return Composite(self._content[key])
return self._content[key]
def process_composite(self, response):
"""
Process a composite response.
composites do not have inter item separators as they appear joined.
We need to respect the universal options too.
"""
composite = response["composite"]
# if the composite is of not Composite make it one so we can simplify
# it.
if not isinstance(composite, Composite):
composite = Composite(composite)
# simplify and get underlying list.
composite = composite.simplify().get_content()
response["composite"] = composite
if not isinstance(composite, list):
raise Exception('expecting "composite" key in response')
# if list is empty nothing to do
if not len(composite):
return
if "full_text" in response:
err = 'conflicting "full_text" and "composite" in response'
raise Exception(err)
# set markup
if "markup" in self.py3status_module_options:
def composite_join(separator, items):
"""
Join a list of items with a separator.
This is used in joining strings, responses and Composites.
The output will be a Composite.
"""
output = Composite()
first_item = True
for item in items:
# skip empty items
if not item:
continue
# skip separator on first item
if first_item:
first_item = False
else:
output.append(separator)
output.append(item)
return output
def copy(self):
"""
Return a shallow copy of the Composite
"""
return Composite([x.copy() for x in self._content])
def process_composite(self, response):
"""
Process a composite response.
composites do not have inter item separators as they appear joined.
We need to respect the universal options too.
"""
composite = response["composite"]
# if the composite is of not Composite make it one so we can simplify
# it.
if not isinstance(composite, Composite):
composite = Composite(composite)
# simplify and get underlying list.
composite = composite.simplify().get_content()
response["composite"] = composite
if not isinstance(composite, list):
raise Exception('expecting "composite" key in response')
# if list is empty nothing to do
if not len(composite):
return
if "full_text" in response:
err = 'conflicting "full_text" and "composite" in response'
raise Exception(err)
# set markup
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:
item.composite_update(item, {"color": color}, soft=True)
out.extend(item.get_content())
elif is_block:
# if this is a block then likely it is soft.
if not out:
continue
for x in range(index + 1, len(output)):
if output[x] and not isinstance(output[x], Block):
valid, _output = item.render(get_params, module, _if=True)
if _output and _output != last_block:
last_block = _output
out.extend(_output)
break
else:
if item:
def composite_update(item, update_dict, soft=False):
"""
Takes a Composite (item) and updates all entries with values from
update_dict. Updates can be soft in which case existing values are not
overwritten.
If item is of type string it is first converted to a Composite
"""
item = Composite(item)
for part in item.get_content():
if soft:
for key, value in update_dict.items():
if key not in part:
part[key] = value
else:
part.update(update_dict)
return item
def append(self, item):
"""
Add an item to the Composite. Item can be a Composite, list etc
"""
if isinstance(item, Composite):
self._content += item.get_content()
elif isinstance(item, list):
self._content += item
elif isinstance(item, dict):
self._content.append(item)
elif isinstance(item, str):
self._content.append({"full_text": item})
else:
msg = "{!r} not suitable to append to Composite"
raise Exception(msg.format(item))
if isinstance(param, Composite):
if param.text():
param = param.copy()
else:
param = ""
return param
# render our processed format
valid, output = first_block.render(get_parameter, module)
# clean things up a little
if isinstance(output, list):
output = Composite(output)
if not output:
if force_composite:
output = Composite()
else:
output = ""
return output