You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
701 B
28 lines
701 B
"""Helper functions."""
|
|
|
|
import pathlib
|
|
import re
|
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
def change_extn( fname, extn ):
|
|
"""Change a filename's extension."""
|
|
return pathlib.Path( fname ).with_suffix( extn )
|
|
|
|
def slugify( val ):
|
|
"""Convert a string to a slug."""
|
|
val = re.sub( r"\s+", " ", val ).lower()
|
|
def fix( ch ):
|
|
if ch.isalnum() or ch == "-":
|
|
return ch
|
|
if ch in " _":
|
|
return "-"
|
|
return "_"
|
|
return "".join( fix(ch) for ch in val )
|
|
|
|
def parse_int( val, default=None ):
|
|
"""Parse an integer."""
|
|
try:
|
|
return int( val )
|
|
except (ValueError, TypeError):
|
|
return default
|
|
|