Bricabrac classes (bricabrac)

Modules

fileIO - Bits and bots dealing with input and output operations

class Printer[source]

Class to print strings in relation to a verbosity level. The verbosity level is shared with all Printer.verbosePrint calls in a python instance.

Usage:

Printer.setVerbosity(<NUMBER>)   # set verbosity level to NUMBER
Printer.verbosePrint(<TEXT>, <VERBOSITY>, *args, **kwargs)  # print TEXT with verbosity VERBOSITY
class Verbosity(value)

An enumeration.

classmethod error(text, printLevel=<Verbosity.ALWAYS: 0>, *args, **kwargs)[source]

Prints an error message only, if its verbosity level is smaller than the global verbosity level.

Parameters
  • text (string) – text to print

  • printLevel (integer) – print level of the text, see ‘verbosePrint’

  • *args (list) – argument list for print

  • **kwargs (dictionary) – keyword argument dictionary for print

Returns

True if the printer actually printed something

Return type

bool

classmethod setVerbosity(verbosity)[source]

Sets verbosity to verbosity.

Parameters

verbosity (integer) – new global verbosity level

classmethod verbosePrint(text, printLevel=<Verbosity.DEFAULT: 2>, *args, **kwargs)[source]

Prints a message only, if its verbosity level is smaller than the global verbosity level.

Parameters
  • text (string) – text to print

  • printLevel (integer) – print level of the text, 0 - ALWAYS, 1 - JENKINS, 2 - DEFAULT, 3 - DEBUG

  • *args (list) – argument list for print

  • **kwargs (dictionary) – keyword argument dictionary for print

Returns

True if the printer actually printed something

Return type

bool

classmethod warning(text, printLevel=<Verbosity.JENKINS: 1>, *args, **kwargs)[source]

Prints a warning message only, if its verbosity level is smaller than the global verbosity level.

Parameters
  • text (string) – text to print

  • printLevel (integer) – print level of the text, see ‘verbosePrint’

  • *args (list) – argument list for print

  • **kwargs (dictionary) – keyword argument dictionary for print

Returns

True if the printer actually printed something

Return type

bool

checkExecutables(*args)[source]

Tests if a executable is found under the given path.

Parameters

*args (list) – list of executables

ensurePath(filepath, verbose=0)[source]

Ensure that the path to the file or directory exists If the path is not valid necessary sub-directories are created.

parameters:
  • filepath (string) - path to directory

optional parameters:
  • verbose (int) - level of verbosity

Forces the creation of a symbolic link.

getAbsoluteFilePath(path, allowDirectories=False)[source]

Returns the absolute path to an existing FILE handling following any symbolic link. Directories are not allowed when the allowDirectoriesFlag is not set to True and will raise an error.

Parameters
  • path (str) – file path

  • allowDirectories (bool) – this flag allows the file path to be a direcotry

Returns

absolute path of file

Return type

str

globCopy(source, destination, match=False, verbose=0)[source]

Allows to copy files or complete directories using the bash wild card syntax.

If a directory is specified the content will be overwritten by trying to remove the destination folder first.

Parameters
  • source (string) – source path (Bash wild card syntax allowed)

  • destination (string) – destination path

  • match (boolean) – if set at least one match has to be found

  • verbose (integer) – verbosity level (0 - None, 1 - print commands)

globCopy2(filepath, srcstring, deststring, verbose=0)[source]

Allows to copy files by replacing parts of the file path. Bash wild card syntax is allowed.

Parameters
  • filepath (string) – source path (Bash wild card syntax allowed)

  • srcstring (string) – source string

  • deststring (string) – destination string

  • verbose (integer) – verbosity level (0 - None, 1 - print commands)

globMove(source, destination, match=False, verbose=0)[source]

Allows to move files using the bash wild card syntax.

Parameters
  • source (string) – source path (Bash wild card syntax allowed)

  • destination (string) – destination path

  • match (boolean) – if set at least one match has to be found

  • verbose (integer) – verbosity level (0 - None, 1 - print commands)

globRm(*paths, **kwargs)[source]

Allows to remove files using the bash wild card syntax.

Parameters
  • *paths (list) – source paths (Bash wild card syntax allowed)

  • (via **kwargs) (verbose) – verbosity level (0 - None, 1 - print commands)

md(directory, verbose=0)[source]

Create directory if it does not already exist

parameters:
  • directory (string) - path to directory

optional parameters:
  • verbose (int) - level of verbosity

return:
  • return code (int) (-1 : path is a file, 0 : path to directory is created, 1 : path is already a directory)

pureText(path, comment='#', lineNumbers=False)[source]

A generator to get only text lines without comments from a file. To use in a for-loop:

Usage:

for line in pureText(path):
    do something with line
Parameters
  • path (string) – path to the file to parse

  • comment (string) – string a comment line starts with

removeFile(fileToRemove)[source]

Removes a file or a folder from the file system without asking anything. Deletes sub folders, too. If the given path does not exist the method just returns False. It will raise an error only if it is not possible to remove the existing file or folder

Warning

Use with caution.

Parameters

fileToRemove (string) – path which will be removed

Returns

returns True if something was removed, False otherwise

Return type

boolean

searchReplaceInFile(fileToSearch, textToSearch, textToReplace)[source]

search replace in a text file

Parameters
  • fileToSearch (string) – file

  • textToSearch (string) – search string

  • textToReplace (string) – replace string

workingDirectory(path)[source]

Executes arbitrary commands in a given working directory. Changes back to the starting directory after execution.

finiteStateMachine

Offers a python implementation of a finite state machine.

It is used in the compareCGNS method to parse a POST cmpc file.

A tutorial for simple text file parsing state machines can be found under: https://www.ibm.com/developerworks/library/l-python-state/

@author: baye_fe

class FiniteStateMachine[source]

Python implementation of a finite state machine

addState(state, handler, isEndState=False)[source]

Add a new state and its handler to the state machine. A flag tags the state as end state.

Parameters
  • state (str) – name of the state

  • handler (Optional[function]) – a method to handle the state

  • isEndState (bool) – If true the state machine terminates when reaching this state

run(consumee, *args, **kwargs)[source]

Run the previously set up state machine.

Starts using the initial state. ‘consumee’ should be the input stream for the state machine. ‘args’ and ‘kwargs’ can be used to pass arguments to the handlers and receive output. The state can be changed by returning the name of the new state in the handler method. If the returned state is a end state the state machine terminates.

Parameters
  • consumee (iterable) – The input stream for the handlers

  • args – arbitrary arguments

  • kwargs – arbitrary keyword arguments

setInitialState(initialState)[source]

Set a state as initial state. Only one state can be the initial state.

Parameters

initialState (str) – name of the initial state

textIO

textIO provides a class to print formatted text into the console.

class Color[source]

Container to store color codes

classmethod colorFileListing(directoryPath, showHidden=False, ignoreFolders=None, ignoreExtensions=None, ignorePrefix=None)[source]

Creates colorful file listing of a folder

Parameters
  • directoryPath (str) – directory path

  • showHidden (bool) – show hidden files and directories listed in dictionary Colors.HIDDEN_FILES

  • ignoreFolders (tuple(str)) – list of folders to be ignored

  • ignoreExtensions (tuple(str)) – list of extensions to be ignored

  • ignorePrefix (tuple(str)) – list of prefixes for directories and files to be ignored

Returns

formatted string

Return type

str

classmethod colorString(text, *args, **kwargs)[source]

Manipulates a string, using keyword args.

Parameters
  • text (str) – text to format

  • *args (list(str)) – Color class constant format attributes

  • **kwargs (dict('indentation' = 0)) – optional argument ‘indentation’

Returns

the formatted string

Return type

str

static printColorful(text, *args)[source]

Manipulates and prints a string, using keyword arguments.

Parameters
  • text (str) – text to format

  • *args (Color attributes) – Color class constant format attributes

static printFileListing(directoryPath, showHidden=False, ignoreFolders=None, ignoreExtensions=None, ignorePrefix=None)[source]

Creates colorful file listing of a folder and prints it

Parameters
  • directoryPath (str) – directory path

  • showHidden (bool) – show hidden files and directories listed in dictionary Colors.HIDDEN_FILES, default: False

  • ignoreFolders (tuple(str)) – list of folders to be ignored

  • ignoreExtensions (tuple(str)) – list of extensions to be ignored

  • ignorePrefix (tuple(str)) – list of prefixes for directories and files to be ignored

convertToJSON(instance)[source]

Convert instance to JSON formatted string.

Parameters

instance (any instance) – instance of an object

Returns

JSON formatted string

Return type

str

Email - Contains a method to send mails.

Created on 10.05.2012

author: well_jn

requires

mimetypes smtplib os re email socket

supplies

Email class - send a mail

class Email(smtp_server)[source]

This class handles the creation and sending of email messages via SMTP. This class also handles attachments and can send HTML messages.

add_attachment(fname, attachname=None)[source]

Add a file attachment to this email message.

add_recipient(address)[source]

Add a new recipient to the email message.

clear_attachments()[source]

Remove all file attachments.

clear_recipients()[source]

Remove all currently defined recipients for the email message.

send()[source]

Send the email message represented by this object.

set_from(address)[source]

Set the email sender.

set_htmlbody(body)[source]

Set the HTML portion of the email message.

set_subject(subject)[source]

Set the subject of the email message.

set_textbody(body)[source]

Set the plain text body of the email message.

validate_emailaddress(address)[source]

Validate the specified email address.

stringFormatting - bits and bots dealing with string formatting

requires

math

class Splashscreen(lines, newMapping=False)[source]

Class to create automated splash screens for your executables. Usage:

someSplashscreen = Splashscreen(["SOFTWARE", "", "version 42"])
someSplashscreen.printScreen()
Parameters
  • line (list) – list with lines to print in the box

  • newMapping (boolean) – Flag if to use unicode box drawing characters, can cause errors in shells which does not support unicode

printScreen(indent=1)[source]

Prints the screen.

Parameters

indent (integer) – indentation for the flashscreen; default is 1

convertToHumandReadableSize(filesize)[source]

Convert a file size in Byte to a string giving the size in GB, MB, kB or B.

getColonDelimitedTimeString(seconds)[source]

Convert a time span into Colon Delimited Time Format, e.g. HH:MM:SS

getHoursMinutesSeconds(seconds)[source]

Convert a time span in seconds to appropriate format in hours, minutes and seconds

sci_notation(num, decimal_digits=1, precision=None, exponent=None)[source]

Returns a string representation of the scientific notation of the given number formatted for use with LaTeX or Mathtext, with specified number of significant decimal digits and precision (number of decimal digits to show). The exponent to be used can also be specified explicitly.

splitListInChunks(givenList, n=8)[source]

Yield successive n-sized chunks from l.

Parameters
  • givenList (list) – list of given elements

  • n (integer) – length of chunk

Returns

chunk of data

Return type

list