Repository: damnever/fmt

Issue:
datetime format
- `f('{datetime(1994, 11, 6, 0, 0, 0):%Y-%m-%d %H:%M:%S}')`

- `f('{(lambda: datetime(1994, 11, 6, 0, 0, 0))():%Y-%m-%d %H:%M:%S}')`

Discussion:

Patch:
diff --git a/fmt/fmt.py b/fmt/fmt.py
index 019eab3..8344646 100644
--- a/fmt/fmt.py
+++ b/fmt/fmt.py
@@ -104,7 +104,7 @@ class Expression(Node):
     def __init__(self, expr, fmt, name='name'):
         self._expr = expr
         self._name = name
-        self._fmt = fmt.replace(expr, name).replace(' ', '')
+        self._fmt = name.join(s.strip() for s in fmt.split(expr))
 
     def generate(self, namespace):
         ns = {}
@@ -314,15 +314,37 @@ class Parser(object):
         #   <optional : format specifier>
         # } <text> ...'''
         expr = None
-        splitd = node_str.rsplit(':', 1)
-        if len(splitd) > 1:
-            left, fmt_spec = splitd
-            if not fmt_spec:
+        if ':' in node_str:
+            if node_str.startswith('('):
+                brackets = 1
+                for i, c in enumerate(node_str[1:]):
+                    if c == '(':
+                        brackets += 1
+                    elif c == ')':
+                        brackets -= 1
+                        if brackets == 0:
+                            break
+                i += 1
+                if brackets != 0:
+                    raise SyntaxError('unexpected EOF at {}'.format(
+                        node_str[i]))
+                i += 1
+                if len(node_str) > i+2 and node_str[i] == '(':
+                    i += 2
+                    if node_str[i] != ':':
+                        raise SyntaxError('unexpected EOF at {}'.format(
+                            node_str[i]))
+                    left, fmt_spec = node_str[:i], node_str[i+1:]
+                else:
+                    left, fmt_spec = node_str, None
+            else:
+                left, fmt_spec = node_str.split(':', 1)
+                if 'lambda' in left:
+                    left = node_str
+            if fmt_spec is not None and not fmt_spec:
                 raise SyntaxError('need format specifier after ":"')
-            elif 'lambda' in left:
-                left = node_str
         else:
-            left = splitd[0]
+            left = node_str
 
         splitd = left.rsplit('!', 1)
         if len(splitd) > 1:


Test patch:
diff --git a/tests/test_fmt.py b/tests/test_fmt.py
index 19033b8..36b3765 100644
--- a/tests/test_fmt.py
+++ b/tests/test_fmt.py
@@ -133,6 +133,10 @@ def test_fmt():
             f('int:{23:d}, hex:{23:x}, oct:{23:#o}, bin:{23:#b}'))
     assert '1,234,567,890' == f('{1234567890:,}')
     assert '1994-11-06' == f('{datetime(1994, 11, 6):%Y-%m-%d}')
+    assert '1994-11-06 00:00:00' == f('{datetime(1994, 11, 6, 0, 0, 0):%Y-%m-%d %H:%M:%S}')
+    assert 'None' == f('{(lambda: None)()}')
+    assert '1994:11:06' == f('{(lambda: datetime(1994, 11, 6))():%Y:%m:%d}')
+    assert '1994-11-06 00:00:00' == f('{(lambda: datetime(1994, 11, 6, 0, 0, 0))():%Y-%m-%d %H:%M:%S}')
 
     assert g_foo not in f._g_ns
     assert g_bar not in f._g_ns