skelpy.utils package

Collection of helper functions and classes

Submodules

skelpy.utils.defaultsubparse module

This module defines DefaultSubCommandArgParser class

class skelpy.utils.defaultsubparse.DefaultSubcommandArgParser(prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True)[source]

Bases: argparse.ArgumentParser

Extended argparse.ArgumentParser with the feature of setting the default sub-parser

This class overrides ArgumentParse._parse_known_args() to insert the default sub-parser to the argument list if no sub-command is given.

codes by Thomas Grainger, from stackoverflow

static combine_usage(parsers)[source]

combine usage-messages of multiple ArgumentParsers into one

Parameters:parsers (list) – ArgumentParsers whose usage-messages to combine
Returns:Combined usage-message
Return type:str
static get_args_by_group(arguments, group)[source]

extract arguments that belongs to an argument group

Parameters:
  • arguments (dict) – whole command-line arguments
  • group (argparse._ArgumentGroup) – argument group
Returns:

arguments belonged to the group

Return type:

dict

set_default_subparser(name)[source]

set default sub-parser

Parameters:name (str) – sub-parser’s name
Returns:None

skelpy.utils.helpers module

Other helper functions

skelpy.utils.helpers.add_metaclass(metaclass)[source]

Class decorator for creating a class with a metaclass.

This function was copied from six library

skelpy.utils.helpers.get_email()[source]

read the user’s name from .gitconfig file in the $HOME directory.

If .gitconfig file does not exist, this function improvises an email address by concatenating user’s name and host name.

Returns:user’s email address
Return type:str
skelpy.utils.helpers.get_gitConfig()[source]

read .gitconfig file in the user’s $HOME directory, if exists

Returns:ConfigParser object filled with info read from .gitconfig
Return type:ConfigParser or None
skelpy.utils.helpers.get_python_version(short=False)[source]

get system’s python version

Parameters:short (bool) – select the short version number(major.minor only) or not
Returns:major.minor if short is True, major.minor.release otherwise
Return type:str
skelpy.utils.helpers.get_userName()[source]

read the user’s name from .gitconfig file in the $HOME directory or the environmental variable USER

Returns:user’s name
Return type:str
skelpy.utils.helpers.has_command(cmd)[source]

Check if the given command is available on the system

Parameters:cmd (str) – command to check
Returns:True if available, otherwise False
Return type:bool
skelpy.utils.helpers.has_module(modName)[source]

Check if the module is installed

Parameters:modName (str) – module name to check
Returns:True if installed, otherwise False
Return type:bool
skelpy.utils.helpers.is_rootDir(path)[source]

check if the path given is the root directory

Parameters:path (str) – path to check
Returns:True if the path is root directory, False otherwise
Return type:bool
skelpy.utils.helpers.is_valid_identifier(string)[source]

Check if the string is a valid project/package name.

Note

Valid characters for projectName are:

* uppercase and lowercase letters A through Z
* underscore '_'
* the digits 0 through 9 except for the first character
Parameters:string (str) – project/package name
Returns:True if string is valid project/package name else False
Return type:bool
skelpy.utils.helpers.read_setup_cfg(cfg_file)[source]

read setup.cfg file

cfg_file may contain a path

Note

setuptools.config.read_configuration() has a problem with encoding under python2

Parameters:cfg_file (str) – path of setup.cfg file
Returns:information read from the setup.cfg file if successful, otherwise an empty dict.
Return type:dict
skelpy.utils.helpers.remove_comment_lines_in_file(oldFile, newFile=None)[source]

remove comment lines in a file

Lines starting with ‘#’ or ‘!’ or ‘REM’ are considered to be comments.

Parameters:
  • oldFile (str) – file name(possibly including path) to remove comments
  • newFile (str) – file name with being removed comments. if this argument is not provided or None(default), the newly created file will overwrite the old.
Returns:

None

skelpy.utils.helpers.remove_comment_lines_in_str(text_data)[source]

remove comment lines in binary data

Lines starting with ‘#’ or ‘!’ or ‘REM’ are considered to be comments.

borrowed from stackoverflow

Parameters:text_data (str) – string to process
Returns:string with removed comments
Return type:str
skelpy.utils.helpers.root_path()[source]

get the root path

This function is OS-independent.

skelpy.utils.logger module

This module defines Logger class

class skelpy.utils.logger.Logger(logger, extra)[source]

Bases: logging.LoggerAdapter

A simple subclass of logging.LoggerAdapter for skelpy

process(msg, kwargs)[source]

overridden method to insert the Maker’s name into the log message.

This method also add to the kwargs the extra dict which is passed when constructed.

Parameters:
  • msg (str) – original log message
  • kwargs (dict) – extra information to pass to the underlying logging.Logger
Returns:

log message modified to include Maker’s name dict: kwargs plus extra

Return type:

str

skelpy.utils.opener module

This module offers open_with_associated_application() which opens a file with the associated application.

skelpy.utils.opener.open_with_associated_application(filePath, block=False, *args)[source]

Open the file with the associated application using the “general-purpose opener” program.

A “general-purpose opener” is a small command-line program which runs a certain application associated to the file type. In a rough sense, what general-purpose opener does is like double-clicking the file on a file-manage program such as Explorer of Windows. Below are well-known OSes and their general-purpose openers:

- Linux: xdg-open
- Windows: start
- OS X: open
- Cygwin: cygstart

Note

Although subprocess.call() is a blocking function, Most–well, if not all–general-purpose openers, by default, do not wait until the application terminates. For kind-of blocking mode, Windows(start) and OSX(open) provide the option “/WAIT” and “-W”, respectively. So, it is possible to run the associated application in the blocking mode just by invoking the general-purpose opener with those options. For example, on Windows:

start -W sample.txt

would make a text editor such as notepad.exe open the sample.txt file and wait until notepad terminates.

However, that is not possible on linux and cygwin, because their general-purpose openers do not have such options. Thus, for the same effect on linux and cygwin, we have to find the associated application and directly run it.

Also be informed that startfile() function provided by python os module is only for Windows and non-blocking.

Note

On Cygwin, the path of the application to run should follow the *nix-style, which uses ‘/’ as the path separator. However, the path-style of the file to open with the application differs from applications. As a matter of fact, Windows applications can not recognize the *nix-style path. Therefore, when used with a Windows application on Cygwin, the file path should be in the Windows-style which uses ‘' as the path separator and an optional drive letter.

Parameters:
  • filePath (str) – the file name(possibly including path) to open
  • args (str) – other arguments to pass to the application to be invoked
  • block (bool) – if True, execute the application in the blocking mode, i.e., wait until the application terminates, otherwise in the non-blocking mode.
Returns:

retcode of subprocess.call() if the application successfully runs, otherwise, i.e., when failed to find an associated application, returns -1

Return type:

int