##// END OF EJS Templates
Allow 2033 to get literal $ in shell commands.
Thomas Kluyver -
Show More
@@ -107,3 +107,5 b' def test_dollar_formatter():'
107 nt.assert_equals(s, "12")
107 nt.assert_equals(s, "12")
108 s = f.format("$n/{stuff[:5]}", **ns)
108 s = f.format("$n/{stuff[:5]}", **ns)
109 nt.assert_equals(s, "12/hello")
109 nt.assert_equals(s, "12/hello")
110 s = f.format("$n $$HOME", **ns)
111 nt.assert_equals(s, "12 $HOME")
@@ -663,20 +663,26 b' class DollarFormatter(FullEvalFormatter):'
663 In [4]: f.format('$a or {b}', a=1, b=2)
663 In [4]: f.format('$a or {b}', a=1, b=2)
664 Out[4]: u'1 or 2'
664 Out[4]: u'1 or 2'
665 """
665 """
666 _dollar_pattern = re.compile("(.*)\$([\w\.]+)")
666 _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)")
667 def parse(self, fmt_string):
667 def parse(self, fmt_string):
668 for literal_txt, field_name, format_spec, conversion \
668 for literal_txt, field_name, format_spec, conversion \
669 in Formatter.parse(self, fmt_string):
669 in Formatter.parse(self, fmt_string):
670
670
671 # Find $foo patterns in the literal text.
671 # Find $foo patterns in the literal text.
672 continue_from = 0
672 continue_from = 0
673 txt = ""
673 for m in self._dollar_pattern.finditer(literal_txt):
674 for m in self._dollar_pattern.finditer(literal_txt):
674 new_txt, new_field = m.group(1,2)
675 new_txt, new_field = m.group(1,2)
675 yield (new_txt, new_field, "", None)
676 # $$foo --> $foo
677 if new_field.startswith("$"):
678 txt += new_txt + new_field
679 else:
680 yield (txt + new_txt, new_field, "", None)
681 txt = ""
676 continue_from = m.end()
682 continue_from = m.end()
677
683
678 # Re-yield the {foo} style pattern
684 # Re-yield the {foo} style pattern
679 yield (literal_txt[continue_from:], field_name, format_spec, conversion)
685 yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
680
686
681
687
682 def columnize(items, separator=' ', displaywidth=80):
688 def columnize(items, separator=' ', displaywidth=80):
General Comments 0
You need to be logged in to leave comments. Login now