Skip to content

Commit

Permalink
Added new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Jun 20, 2020
1 parent 71ed1a0 commit e433a03
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ It also provides some simple execution functions:

**Name** | **Description**
:---: | :---:
`ts.apply` | convenience function for applying a list of functions to the given arguments and keyword-arguments
`ts.execute` | dummy alias for calling a subprocess and returning its STDOUT and STDERR
`ts.process` | decorator for turning a function into a process
`ts.processes_clean` | cleanup function for joining processes opened with `ts.process`
Expand All @@ -79,6 +80,7 @@ It also provides some other utility functions:
`ts.entropy` | computation function for the Shannon entropy of a string
`ts.get_terminal_size` | cross-platform terminal size function
`ts.pad` | String padding function, supporting `ansic9.23`, `incremental`, `iso7816-4`, `pkcs5`, `pkcs7` and `w3c` algorithms
`ts.shorten` | String shortening function, taking by default the terminal width, otherwise a length of 40 characters (unless user-defined), and using an end token (by default "`...`")
`ts.silent` | decorator for silencing `stdout` and `stderr` of a function
`ts.slugify` | slugify a string (handles unicode ; relying on [`slugify`](https://github.com/un33k/python-slugify))
`ts.stdin_pipe` | Python2/3-compatible iterator of STDIN lines
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_general_purpose_types(self):

def test_config_related_types(self):
self.assertRaises(ValueError, ini_config, "does_not_exist")
#self.assertTrue(is_ini(INI))
#self.assertTrue(is_ini(INI)) # FIXME: this test fails on Travis CI
self.assertFalse(is_ini_file("does_not_exist"))
cfg = CFNAME + "ini"
with open(cfg, 'wt') as f:
Expand Down
1 change: 1 addition & 0 deletions tests/test_helpers_fexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ def test_execution_functions(self):
self.assertIsNotNone(test2())
self.assertIsNone(processes_clean())
self.assertIsNone(threads_clean())
self.assertEqual(apply([lambda x: x+1, lambda x: x+2], (1, )), [2, 3])

7 changes: 7 additions & 0 deletions tests/test_helpers_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ def test_text_conversions(self):
self.assertRaises(ValueError, _txt_list, TXT, "md", ordered="not_bool")
self.assertRaises(ValueError, txt2title, TXT, "rst", level="not_int")
self.assertRaises(ValueError, txt2url, TXT, "md", url="bad_url")
# others
STR = "this is a test"
self.assertEqual(shorten(STR), STR)
self.assertTrue(len(shorten(100 * STR)) < len(100 * STR))
self.assertTrue(shorten(100 * STR).endswith("..."))
self.assertTrue(shorten(100 * STR, end="|||").endswith("|||"))
self.assertRaises(ValueError, shorten, "test", "BAD_LENGTH")

def test_text_rendering(self):
configure_docformat({'__docformat__': "html"})
Expand Down
2 changes: 1 addition & 1 deletion tinyscript/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22.1
1.22.2
9 changes: 8 additions & 1 deletion tinyscript/helpers/fexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
from threading import Thread


__all__ = __features__ = ["execute", "process", "processes_clean", "thread", "threads_clean"]
__all__ = __features__ = ["apply", "execute", "process", "processes_clean", "thread", "threads_clean"]


PROCESSES = []
THREADS = []


def apply(functions, args=(), kwargs={}):
"""
Shortcut to apply a list of functions to the given arguments and keyword-arguments.
"""
return [f(*args, **kwargs) for f in functions]


def execute(cmd, **kwargs):
"""
Dummy wrapper for subprocess.Popen.
Expand Down
18 changes: 17 additions & 1 deletion tinyscript/helpers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from pypandoc import convert_text

from .data.types.network import is_email, is_url
from .termsize import get_terminal_size


__features__ = ["gt", "txt2blockquote", "txt2bold", "txt2email", "txt2italic", "txt2olist", "txt2paragraph",
__features__ = ["gt", "shorten", "txt2blockquote", "txt2bold", "txt2email", "txt2italic", "txt2olist", "txt2paragraph",
"txt2title", "txt2ulist", "txt2underline", "txt2url"]
__all__ = __features__ + ["DOCFORMAT_THEME"]

Expand Down Expand Up @@ -182,6 +183,21 @@ def _txt_style(text, format=None, bold=False, italic=False, underline=False):
txt2underline = lambda text, format=None: _txt_style(text, format, underline=True)


def shorten(string, length=None, end="..."):
"""
Simple string shortening function for user-friendlier display.
:param string: the string to be shortened
:param length: maximum length of the string
"""
if length is None:
ts = get_terminal_size()
length = ts[0] if ts is not None else 40
if not isinstance(length, int):
raise ValueError("Invalid length '{}'".format(length))
return string if len(string) <= length else string[:length-len(end)] + end


def txt2blockquote(text, format=None):
""" This reformats a text to a block quote based on the selected format.
Expand Down

0 comments on commit e433a03

Please sign in to comment.