Burgerlib Python
|
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. | |
Package that contains string manipulation functions.
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.
input_array | The object to test |
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 "/"
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
path_name | A pathname to be converted to Windows slashes |
force_ending_slash | True if a "\\" character is to be forced at the end of the output @return A pathname using Windows type slashes "\" |
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.
input_path | string with the path to encapsulate |
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.
input_path | string with the path to encapsulate |
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.
input_path | string with the path to encapsulate using Windows rules |
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.
input_path | string with the path to encapsulate using Windows rules |
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 """, &", "<" and ">" respectively.
Tabs and line feeds will be converted to " " and "	".
xml_string | String to convert to XML attribute strings. |
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 "&", "<" and ">" respectively.
xml_string | String to convert to one compatible with XML CDATA |
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)
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
wsl_allowed | If True, allow returning a host type if cygwin is the shell. |
def burger.strutils.host_machine | ( | ) |
Return the high level operating system's name.
Return the machine this script is running on, "windows", "macosx",
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
item | Object to test |
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.
version_string | String for the version number. |
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.
working_directory | Base directory for incomplete paths |
items | Iterable list of pathnames |
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"
entries | list of strings to concatenate |
separator | Character to use to seperate entries, ";" is used for None |
force_ending_slash | Enforce a trailing slash if True |
slashes | None for no conversion "/" or "\\" path separator
@par Return
String of all entries seperated by ";" or separator |
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
csv_string | String of comma seperated entries |
ValueError |
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
comma_string | String of comma seperated strings |
ValueError |
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.
item | String or integer to convert. |
True
or False
def burger.strutils.translate_to_regex_match | ( | file_list | ) |
Translate filename wildcards into regexes.
file_list | List of filename wildcards |
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"
item | Object to convert to a bool before converting into a string |
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"
item | Object to convert to a bool before converting into a string |
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"
item | Object to convert to a bool before converting into a string |
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.
input_string | A unicode string to print to stdout. |
|
private |
Internal type to pass to isinstance() for is_string()
|
private |
Valid characters for macOS and Linux files without quoting.
|
private |
Cached result for get_mac_host_type()
|
private |
Regex to match comma and quotes.
|
private |
Valid characters for windows filenames without quoting.
burger.strutils.IS_CYGWIN = sys.platform.startswith("cygwin") |
Running on Cygwin.
burger.strutils.IS_LINUX = sys.platform.startswith("linux") |
Running on linux?
burger.strutils.IS_MACOSX = sys.platform.startswith("darwin") |
Running on macOS X.
burger.strutils.IS_MSYS = sys.platform.startswith("msys") |
Running on MSYS.
burger.strutils.IS_WINDOWS = sys.platform.startswith("win") |
Running on Windows.
burger.strutils.IS_WINDOWS_HOST = IS_WINDOWS or IS_CYGWIN or IS_MSYS or IS_WSL |
Running on Windows (Including Linux shells on windows)
burger.strutils.IS_WSL = IS_LINUX and "icrosoft" in platform.platform() |
Running on Windows Subsystem for Linux.
burger.strutils.PY2 = 2 |
True if the interpreter is Python 2.x.
burger.strutils.PY3_3_OR_HIGHER = (3, 3, 0) |
True if the interpreter is Python 3.3 or higher.
burger.strutils.PY3_4_OR_HIGHER = (3, 4, 0) |
True if the interpreter is Python 3.4 or higher.
burger.strutils.PY3_5_OR_HIGHER = (3, 5, 0) |
True if the interpreter is Python 3.5 or higher.
burger.strutils.PY3_OR_HIGHER = 3 |
True if the interpreter is Python 3.x or higher.
burger.strutils.PYPY = "PyPy" |
True if the interpreter is PyPy.
burger.strutils.UNICODE = unicode |
Class for declaring unicode strings.