Burgerlib Python
Loading...
Searching...
No Matches
Functions | Variables
burger.strutils Namespace Reference

Package that contains string manipulation functions. More...

Functions

def unicode_print (input_string)
 Handle printing a unicode string to stdout. More...
 
def is_string (item)
 Return True if input is a string object. More...
 
def convert_to_array (input_array)
 Convert a string to a string array (list) More...
 
def norm_paths (working_directory, items)
 Convert an iterable list of pathnames and convert them all into a list of normalized absolute pathnames. More...
 
def string_to_bool (item)
 Convert an item to a boolean. More...
 
def TrueFalse (item)
 Convert the input into a boolean and return the string "True" or "False". More...
 
def truefalse (item)
 Convert the input into a boolean and return the string "true" or "false". More...
 
def TRUEFALSE (item)
 Convert the input into a boolean and return the string "TRUE" or "FALSE". More...
 
def convert_to_windows_slashes (path_name, force_ending_slash=False)
 Convert a filename from Linux/macOS to Windows format. More...
 
def convert_to_linux_slashes (path_name, force_ending_slash=False)
 Convert a filename from Windows to Linux/macOS format. More...
 
def encapsulate_path_windows (input_path)
 Quote a pathname for use in the Windows system shell. More...
 
def encapsulate_path_linux (input_path)
 Quote a pathname for use in the linux or BSD system shell. More...
 
def encapsulate_path (input_path)
 Quote a pathname for use in the native system shell. More...
 
def encapsulate_hosted_path (input_path)
 Quote a pathname for use in the windows system shell. More...
 
def split_comma_with_quotes (comma_string)
 Split comma seperated string while handling quotes. More...
 
def parse_csv (csv_string)
 Parse a comma seperated string allowing quoted strings. More...
 
def translate_to_regex_match (file_list)
 Translate filename wildcards into regexes. More...
 
def host_machine ()
 Return the high level operating system's name. More...
 
def get_windows_host_type (wsl_allowed=False)
 Return windows host type (32 or 64 bit) More...
 
def get_mac_host_type ()
 Return Mac OSX host type (PowerPC/Intel) More...
 
def escape_xml_cdata (xml_string)
 Convert escape codes for CDATA XML records. More...
 
def escape_xml_attribute (xml_string)
 Convert escape codes for XML element attribute records. More...
 
def packed_paths (entries, slashes=None, separator=None, force_ending_slash=False)
 Convert a list of paths and convert to a PATH string. More...
 
def make_version_tuple (version_string)
 Convert numeric version string into an int tuple. More...
 

Variables

int PY2 = 2
 True if the interpreter is Python 2.x. More...
 
int PY3_OR_HIGHER = 3
 True if the interpreter is Python 3.x or higher. More...
 
tuple PY3_3_OR_HIGHER = (3, 3, 0)
 True if the interpreter is Python 3.3 or higher. More...
 
tuple PY3_4_OR_HIGHER = (3, 4, 0)
 True if the interpreter is Python 3.4 or higher. More...
 
tuple PY3_5_OR_HIGHER = (3, 5, 0)
 True if the interpreter is Python 3.5 or higher. More...
 
string PYPY = "PyPy"
 True if the interpreter is PyPy. More...
 
 _WINDOWSSAFESET
 Valid characters for windows filenames without quoting. More...
 
 _LINUXSAFESET
 Valid characters for macOS and Linux files without quoting. More...
 
 _RE_COMMA_QUOTES
 Regex to match comma and quotes. More...
 
 _MAC_HOST_TYPE
 Cached result for get_mac_host_type() More...
 
 IS_LINUX = sys.platform.startswith("linux")
 Running on linux? More...
 
 IS_MACOSX = sys.platform.startswith("darwin")
 Running on macOS X. More...
 
 IS_WINDOWS = sys.platform.startswith("win")
 Running on Windows. More...
 
 IS_CYGWIN = sys.platform.startswith("cygwin")
 Running on Cygwin. More...
 
 IS_MSYS = sys.platform.startswith("msys")
 Running on MSYS. More...
 
def IS_WSL = IS_LINUX and "icrosoft" in platform.platform()
 Running on Windows Subsystem for Linux. More...
 
def IS_WINDOWS_HOST = IS_WINDOWS or IS_CYGWIN or IS_MSYS or IS_WSL
 Running on Windows (Including Linux shells on windows) More...
 
string _WINDOWS_HOST_PREFIX
 Prefix string for conversion of Windows paths to Linux.
 
 _IS_STRING_TEST
 Internal type to pass to isinstance() for is_string() More...
 
 UNICODE = unicode
 Class for declaring unicode strings. More...
 
 LONG = long
 Class for declaring 64 bit integers.
 

Detailed Description

Package that contains string manipulation functions.

Function Documentation

◆ convert_to_array()

def burger.strutils.convert_to_array (   input_array)

Convert a string to a string array (list)

If the input is None, return an empty list. If it's a string, convert the string to a single entry list. Otherwise, assume it's an iterable dir, list or tuple of strings.

Parameters
input_arrayThe object to test
Returns
The input, or a string encapsulated into a single entry list.

◆ convert_to_linux_slashes()

def burger.strutils.convert_to_linux_slashes (   path_name,
  force_ending_slash = False 
)

Convert a filename from Windows to Linux/macOS format.

Convert all "\" characters into "/" characters @param path_name A string object that text substitution will occur @param force_ending_slash True if a "/" character is to be forced at the end of the output @return A pathname using Linux/BSD type slashes "/"

See also
convert_to_windows_slashes

◆ convert_to_windows_slashes()

def burger.strutils.convert_to_windows_slashes (   path_name,
  force_ending_slash = False 
)

Convert a filename from Linux/macOS to Windows format.

Convert all "/" characters into "\" characters If force_ending_slash is True, append a "" if one is not present in the final string

Parameters
path_nameA pathname to be converted to Windows slashes
force_ending_slashTrue if a "\\" character is to be forced at the end of the output @return A pathname using Windows type slashes "\"
See also
convert_to_linux_slashes

◆ encapsulate_hosted_path()

def burger.strutils.encapsulate_hosted_path (   input_path)

Quote a pathname for use in the windows system shell.

If the platform is hosted on Windows, convert the pathname to Windows and then use Windows encapsulation rules.

Parameters
input_pathstring with the path to encapsulate
Returns
Input string or input properly quoted
See also
encapsulate_path_windows
encapsulate_path_linux
encapsulate_path

◆ encapsulate_path()

def burger.strutils.encapsulate_path (   input_path)

Quote a pathname for use in the native system shell.

On Windows platforms, if the path has a space or other character that could confuse COMMAND.COM, the string will be quoted, and for other platforms, it will be quoted using rules that work best for BASH. This will also quote if the path has a ";" which could be used to confuse bash.

Parameters
input_pathstring with the path to encapsulate
Returns
Input string or input properly quoted
See also
encapsulate_path_windows
encapsulate_path_linux

◆ encapsulate_path_linux()

def burger.strutils.encapsulate_path_linux (   input_path)

Quote a pathname for use in the linux or BSD system shell.

On Linux platforms, if the path has a space or other character that could confuse bash, the string will be quoted and double quotes within the string handled properly. All backslash characters will be replaced with slash characters.

Parameters
input_pathstring with the path to encapsulate using Windows rules
Returns
Original input string if Windows can accept it or input properly quoted
See also
encapsulate_path

◆ encapsulate_path_windows()

def burger.strutils.encapsulate_path_windows (   input_path)

Quote a pathname for use in the Windows system shell.

On Windows platforms, if the path has a space or other character that could confuse COMMAND.COM, the string will be quoted and double quotes within the string handled properly. All slash characters will be replaced with backslash characters.

Parameters
input_pathstring with the path to encapsulate using Windows rules
Returns
Original input string if Windows can accept it or input properly quoted
See also
encapsulate_path

◆ escape_xml_attribute()

def burger.strutils.escape_xml_attribute (   xml_string)

Convert escape codes for XML element attribute records.

According to the XML docs, ", &, < and > cannot exist in a string so they must be replaced with """, &amp;", "&lt;" and "&gt;" respectively.

Tabs and line feeds will be converted to "&#10;" and "&#09;".

Note
https://www.w3.org/TR/REC-xml/#sec-line-ends
Parameters
xml_stringString to convert to XML attribute strings.
Return
Original string if no changes, or a new string with escaped characters.

◆ escape_xml_cdata()

def burger.strutils.escape_xml_cdata (   xml_string)

Convert escape codes for CDATA XML records.

According to the XML docs, &, < and > cannot exist in a string so they must be replaced with "&amp;", "&lt;" and "&gt;" respectively.

Parameters
xml_stringString to convert to one compatible with XML CDATA
Return
Original string if no changes, or a new string with escaped characters.

◆ get_mac_host_type()

def burger.strutils.get_mac_host_type ( )

Return Mac OSX host type (PowerPC/Intel)

Return False if the host is not Mac OSX. "ppc" or "ppc64" if it's a Power PC based system, "x86" or "x64" for Intel (Both 32 and 64 bit)

Returns
The string "x86", "x64", "ppc", "ppc64" or False.
See also
get_windows_host_type
host_machine

◆ get_windows_host_type()

def burger.strutils.get_windows_host_type (   wsl_allowed = False)

Return windows host type (32 or 64 bit)

Return False if the host is not Windows, "x86" if it's a 32 bit host and "x64" if it's a 64 bit host, and possibly "arm" if an arm host

Parameters
wsl_allowedIf True, allow returning a host type if cygwin is the shell.
Returns
The string "x64", "x86", "arm", "arm64", "ia64" or False
See also
get_mac_host_type
host_machine

◆ host_machine()

def burger.strutils.host_machine ( )

Return the high level operating system's name.

Return the machine this script is running on, "windows", "macosx",

"linux" or "unknown"
Returns
The string "windows", "macosx", "linux", or "unknown"
See also
get_mac_host_type
get_windows_host_type

◆ is_string()

def burger.strutils.is_string (   item)

Return True if input is a string object.

Test the input if it's either an instance of basestring in Python 2.x or (str, bytes) in Python 3.x

Parameters
itemObject to test
Returns
True if the object is a string instance, False if not.

◆ make_version_tuple()

def burger.strutils.make_version_tuple (   version_string)

Convert numeric version string into an int tuple.

Given a string like "1.0.5rc" and return a tuple of (1, 0, 5). The numbers are seperated by periods and members that start with a non number are skipped.

Parameters
version_stringString for the version number.
Returns
tuple of integers with the version. Can be an empty tuple.

◆ norm_paths()

def burger.strutils.norm_paths (   working_directory,
  items 
)

Convert an iterable list of pathnames and convert them all into a list of normalized absolute pathnames.

Parameters
working_directoryBase directory for incomplete paths
itemsIterable list of pathnames
Returns
A list of absolute pathnames

◆ packed_paths()

def burger.strutils.packed_paths (   entries,
  slashes = None,
  separator = None,
  force_ending_slash = False 
)

Convert a list of paths and convert to a PATH string.

Convert ["a","b","c"] to "a;b;c"

Parameters
entrieslist of strings to concatenate
separatorCharacter to use to seperate entries, ";" is used for None
force_ending_slashEnforce a trailing slash if True
slashesNone for no conversion "/" or "\\" path separator @par Return String of all entries seperated by ";" or separator

◆ parse_csv()

def burger.strutils.parse_csv (   csv_string)

Parse a comma seperated string allowing quoted strings.

Given a string of comma seperated entries and handle quotes properly.

Examples

# Result is ["foo,bar","foo","bar"]
lines = burger.strutils.split_comma_with_quotes("\"foo,bar\",foo,bar")
# Result is ["foo\"bar","'boo'boo'"]
"\"foo\"\"bar\"","'boo,boo'")
# Will raise an error due to missing end quote
willraise = burger.strutils.split_comma_with_quote("\"foo,bar")
def split_comma_with_quotes(comma_string)
Split comma seperated string while handling quotes.
Definition: strutils.py:652
Parameters
csv_stringString of comma seperated entries
Returns
List of entries with whitespace stripped from prefix and suffix
Exceptions
ValueError

◆ split_comma_with_quotes()

def burger.strutils.split_comma_with_quotes (   comma_string)

Split comma seperated string while handling quotes.

str.split(",") will split a string into a list but it doesn't handle entries that are encased in quotes. This function will scan for quote characters and skip over any comma that's encased in quotes.

Examples

# Result is ["\"foo,bar\"","foo","bar"]
lines = burger.strutils.split_comma_with_quotes("\"foo,bar\",foo,bar")
# Will raise an error due to missing end quote
willraise = burger.strutils.split_comma_with_quote("\"foo,bar")
Parameters
comma_stringString of comma seperated strings
Return
List of string fragments for each comma seperated entries
Exceptions
ValueError

◆ string_to_bool()

def burger.strutils.string_to_bool (   item)

Convert an item to a boolean.

Strings "true", "t", "on", "yes", and, "y" return True "false", "f", "off", "no", and "n" return False

Non zero numbers or strings that are numbers become True Zero and "0" become False.

Parameters
itemString or integer to convert.
Returns
True or False
Exception
ValueError on invalid input.

◆ translate_to_regex_match()

def burger.strutils.translate_to_regex_match (   file_list)

Translate filename wildcards into regexes.

Parameters
file_listList of filename wildcards
Results
List of re.compile().match entries

◆ TrueFalse()

def burger.strutils.TrueFalse (   item)

Convert the input into a boolean and return the string "True" or "False".

If the input was a string of "0" or "False" (Case insensitive comparision), this function will return "False". Empty dictionary, string or list objects, or the number zero will also return "False"

Parameters
itemObject to convert to a bool before converting into a string
Returns
The string "True" or "False"
See also
truefalse
TRUEFALSE

◆ truefalse()

def burger.strutils.truefalse (   item)

Convert the input into a boolean and return the string "true" or "false".

If the input was a string of "0" or "False" (Case insensitive comparision), this function will return "false". Empty dictionary, string or list objects, or the number zero will also return "false"

Parameters
itemObject to convert to a bool before converting into a string
Returns
The string "true" or "false"
See also
TRUEFALSE
TrueFalse

◆ TRUEFALSE()

def burger.strutils.TRUEFALSE (   item)

Convert the input into a boolean and return the string "TRUE" or "FALSE".

If the input was a string of "0" or "False" (Case insensitive comparision), this function will return "FALSE". Empty dictionary, string or list objects, or the number zero will also return "FALSE"

Parameters
itemObject to convert to a bool before converting into a string
Returns
The string "TRUE" or "FALSE"
See also
truefalse
TrueFalse

◆ unicode_print()

def burger.strutils.unicode_print (   input_string)

Handle printing a unicode string to stdout.

On some platforms, printing a unicode string will trigger a UnicodeEncodeError exception. In these cases, handle the exception and recode the string to the native string encoding.

Parameters
input_stringA unicode string to print to stdout.

Variable Documentation

◆ _IS_STRING_TEST

tuple burger.strutils._IS_STRING_TEST
private

Internal type to pass to isinstance() for is_string()

◆ _LINUXSAFESET

burger.strutils._LINUXSAFESET
private

Valid characters for macOS and Linux files without quoting.

◆ _MAC_HOST_TYPE

burger.strutils._MAC_HOST_TYPE
private

Cached result for get_mac_host_type()

◆ _RE_COMMA_QUOTES

burger.strutils._RE_COMMA_QUOTES
private

Regex to match comma and quotes.

◆ _WINDOWSSAFESET

burger.strutils._WINDOWSSAFESET
private

Valid characters for windows filenames without quoting.

◆ IS_CYGWIN

burger.strutils.IS_CYGWIN = sys.platform.startswith("cygwin")

Running on Cygwin.

◆ IS_LINUX

burger.strutils.IS_LINUX = sys.platform.startswith("linux")

Running on linux?

◆ IS_MACOSX

burger.strutils.IS_MACOSX = sys.platform.startswith("darwin")

Running on macOS X.

◆ IS_MSYS

burger.strutils.IS_MSYS = sys.platform.startswith("msys")

Running on MSYS.

◆ IS_WINDOWS

burger.strutils.IS_WINDOWS = sys.platform.startswith("win")

Running on Windows.

◆ IS_WINDOWS_HOST

burger.strutils.IS_WINDOWS_HOST = IS_WINDOWS or IS_CYGWIN or IS_MSYS or IS_WSL

Running on Windows (Including Linux shells on windows)

◆ IS_WSL

burger.strutils.IS_WSL = IS_LINUX and "icrosoft" in platform.platform()

Running on Windows Subsystem for Linux.

◆ PY2

burger.strutils.PY2 = 2

True if the interpreter is Python 2.x.

◆ PY3_3_OR_HIGHER

burger.strutils.PY3_3_OR_HIGHER = (3, 3, 0)

True if the interpreter is Python 3.3 or higher.

◆ PY3_4_OR_HIGHER

burger.strutils.PY3_4_OR_HIGHER = (3, 4, 0)

True if the interpreter is Python 3.4 or higher.

◆ PY3_5_OR_HIGHER

burger.strutils.PY3_5_OR_HIGHER = (3, 5, 0)

True if the interpreter is Python 3.5 or higher.

◆ PY3_OR_HIGHER

burger.strutils.PY3_OR_HIGHER = 3

True if the interpreter is Python 3.x or higher.

◆ PYPY

burger.strutils.PYPY = "PyPy"

True if the interpreter is PyPy.

◆ UNICODE

burger.strutils.UNICODE = unicode

Class for declaring unicode strings.