def __init__(self, cmd="", inputs=None, outputs=None, desc="", **kwds): self._inputs = as_tuple(inputs) self._outputs = as_tuple(outputs) desc_kw = dict(i=self._inputs, o=self._outputs) desc_kw.update(kwds) desc = desc.format(**desc_kw) cmd_kw = dict(i=shquote(self._inputs), o=shquote(self._outputs)) cmd_kw.update(kwds) cmd = vformat_list(string.Formatter(), cmd, [], cmd_kw) if not desc: desc = cmd super().__init__(cmd=cmd, desc=desc)
def _parse_template(format_strings, cls_name, cls_members, cls_bases): """Parse the template in *format_strings* in the given class context. The new placeholders are inserted in the *cls_members* namespace modifying it in-place. """ ctxt = _ClsContext(cls_name, cls_members, cls_bases) parser = _TemplateParser(ctxt) parser.parse(as_tuple(format_strings))
def vformat_template(format_strings, command, kwds=None): """Format *format_string* using *command* as a mapping. *kwds* can be used as an alternative mapping to the default one provided by the FormatView of the given *command*. If *kwds* is not a sub-class of FormatView, that class is used instead. """ if isinstance(kwds, Mapping): kwds = FormatView(command, kwds) if not kwds: kwds = FormatView if isinstance(kwds, type) and issubclass(kwds, FormatView): kwds = kwds(command) format_strings = as_tuple(format_strings) # Filter out empty string and join with one space. return vformat_list(Formatter(), format_strings, [], kwds)
def test_set(self): self.assertCountEqual(as_tuple(set(("foo", "bar"))), ("foo", "bar"))
def test_tuple(self): self.assertEqual(as_tuple(("foo", "bar")), ("foo", "bar"))
def test_list(self): self.assertEqual(as_tuple(["foo", "bar"]), ("foo", "bar"))
def test_singleton(self): self.assertEqual(as_tuple("foo"), ("foo",))
def test_none(self): self.assertEqual(as_tuple(None), ())
def parse(self, format_strings): for format_string in as_tuple(format_strings): self._formatter.vformat(format_string, [], self._view)