[formatter] support filesystem paths for \fM
This commit is contained in:
@@ -272,9 +272,10 @@ Starting a format string with '\f<Type> ' allows to set a different format strin
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="center"><code>M</code></td>
|
<td align="center"><code>M</code></td>
|
||||||
<td> Name of a Python module followed by one of its functions.
|
<td>Path or name of a Python module
|
||||||
This function gets called with the current metadata dict as
|
followed by the name of one of its functions.
|
||||||
argument and should return a string.</td>
|
This function gets called with the current metadata dict as
|
||||||
|
argument and should return a string.</td>
|
||||||
<td><code>\fM my_module:generate_text</code></td>
|
<td><code>\fM my_module:generate_text</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ class ModuleFormatter():
|
|||||||
|
|
||||||
def __init__(self, function_spec, default=NONE, fmt=None):
|
def __init__(self, function_spec, default=NONE, fmt=None):
|
||||||
module_name, _, function_name = function_spec.partition(":")
|
module_name, _, function_name = function_spec.partition(":")
|
||||||
module = __import__(module_name)
|
module = util.import_file(module_name)
|
||||||
self.format_map = getattr(module, function_name)
|
self.format_map = getattr(module, function_name)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -374,7 +374,7 @@ def noarg():
|
|||||||
try:
|
try:
|
||||||
fmt1 = formatter.parse("\fM testmod:gentext")
|
fmt1 = formatter.parse("\fM testmod:gentext")
|
||||||
fmt2 = formatter.parse("\fM testmod:lengths")
|
fmt2 = formatter.parse("\fM testmod:lengths")
|
||||||
fmt3 = formatter.parse("\fM testmod:noarg")
|
fmt0 = formatter.parse("\fM testmod:noarg")
|
||||||
|
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(AttributeError):
|
||||||
formatter.parse("\fM testmod:missing")
|
formatter.parse("\fM testmod:missing")
|
||||||
@@ -383,11 +383,17 @@ def noarg():
|
|||||||
finally:
|
finally:
|
||||||
sys.path.pop(0)
|
sys.path.pop(0)
|
||||||
|
|
||||||
|
fmt3 = formatter.parse("\fM " + path + ":gentext")
|
||||||
|
fmt4 = formatter.parse("\fM " + path + ":lengths")
|
||||||
|
|
||||||
self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name")
|
self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name")
|
||||||
self.assertEqual(fmt2.format_map(self.kwdict), "89")
|
self.assertEqual(fmt2.format_map(self.kwdict), "89")
|
||||||
|
|
||||||
|
self.assertEqual(fmt3.format_map(self.kwdict), "'Title' by Name")
|
||||||
|
self.assertEqual(fmt4.format_map(self.kwdict), "89")
|
||||||
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
self.assertEqual(fmt3.format_map(self.kwdict), "")
|
self.assertEqual(fmt0.format_map(self.kwdict), "")
|
||||||
|
|
||||||
def _run_test(self, format_string, result, default=None, fmt=format):
|
def _run_test(self, format_string, result, default=None, fmt=format):
|
||||||
fmt = formatter.parse(format_string, default, fmt)
|
fmt = formatter.parse(format_string, default, fmt)
|
||||||
|
|||||||
Reference in New Issue
Block a user