3
0
Fork 0

fix: add AppHandler.make_title() convenience method

This commit is contained in:
Lance Edgar 2024-08-04 14:23:02 -05:00
parent 27b859c1c7
commit 739dd285aa
4 changed files with 41 additions and 1 deletions

View file

@ -28,7 +28,7 @@ import importlib
import os
import warnings
from wuttjamaican.util import load_entry_points, load_object, make_uuid, parse_bool
from wuttjamaican.util import load_entry_points, load_object, make_title, make_uuid, parse_bool
class AppHandler:
@ -232,6 +232,20 @@ class AppHandler:
return Session(**kwargs)
def make_title(self, text, **kwargs):
"""
Return a human-friendly "title" for the given text.
This is mostly useful for converting a Python variable name (or
similar) to a human-friendly string, e.g.::
make_title('foo_bar') # => 'Foo Bar'
By default this just invokes
:func:`wuttjamaican.util.make_title()`.
"""
return make_title(text)
def make_uuid(self):
"""
Generate a new UUID value.

View file

@ -114,6 +114,21 @@ def load_object(spec):
return getattr(module, name)
def make_title(text):
"""
Return a human-friendly "title" for the given text.
This is mostly useful for converting a Python variable name (or
similar) to a human-friendly string, e.g.::
make_title('foo_bar') # => 'Foo Bar'
"""
text = text.replace('_', ' ')
text = text.replace('-', ' ')
words = text.split()
return ' '.join([x.capitalize() for x in words])
def make_uuid():
"""
Generate a universally-unique identifier.

View file

@ -116,6 +116,10 @@ class TestAppHandler(TestCase):
def test_get_title(self):
self.assertEqual(self.app.get_title(), 'WuttJamaican')
def test_make_title(self):
text = self.app.make_title('foo_bar')
self.assertEqual(text, "Foo Bar")
def test_make_uuid(self):
uuid = self.app.make_uuid()
self.assertEqual(len(uuid), 32)

View file

@ -232,3 +232,10 @@ class TestParseList(TestCase):
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma')
self.assertEqual(value[2], 'baz')
class TestMakeTitle(TestCase):
def test_basic(self):
text = util.make_title('foo_bar')
self.assertEqual(text, "Foo Bar")