util package

Submodules

util.configparser module

class util.configparser.ConfigParser(description=None)

Bases: object

Class for parsing python configuration files

Case1: Normal usage >>> f = open(“test.py”, “w”) >>> f.write(‘modules = {“local”:”/path/to/local”, “svn”:”path/to/svn”}; ‘) >>> f.write(‘fetchto = ”..”’ ) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“modules”, type={}) >>> p.add_option(“fetchto”, type=’‘) >>> p.add_config_file(“test.py”) >>> p.parse() {‘modules’: {‘svn’: ‘path/to/svn’, ‘local’: ‘/path/to/local’}, ‘fetchto’: ‘..’}

Case2: Default value and lack of a variable >>> f = open(“test.py”, “w”) >>> f.write(‘a=”123”’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“a”, type=’‘) >>> p.add_option(“b”, type=’‘, default=’borsuk’) >>> p.add_config_file(“test.py”) >>> p.parse() {‘a’: ‘123’, ‘b’: ‘borsuk’}

Case3: Multiple types for a variable >>> f = open(“test.py”, “w”) >>> f.write(‘a=[1,2,3]’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“a”, type=1, default=12) >>> p.add_type(“a”, type=[]) >>> p.add_config_file(“test.py”) >>> p.parse() {‘a’: [1, 2, 3]}

Case4: Unrecognized options >>> f = open(“test.py”, “w”) >>> f.write(‘a = 123’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“b”, type=’‘) >>> p.add_config_file(“test.py”) >>> p.parse() Traceback (most recent call last):

File “<stdin>”, line 1, in <module> File “configparser.py”, line 107, in parse raise NameError(“Unrecognized option: ” + key)

NameError: Unrecognized option: a

Case5: Invalid parameter type >>> f = open(“test.py”,”w”) >>> f.write(‘a=”123”’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“a”, type=0) >>> p.add_config_file(“test.py”) >>> p.parse() Traceback (most recent call last):

File “<stdin>”, line 1, in <module> File “configparser.py”, line 110, in parse raise RuntimeError(“Given option: “+str(type(val))+” doesn’t match specified types:”+str(opt.types))

RuntimeError: Given option: <type ‘str’> doesn’t match specified types:[<type ‘int’>]

Case6: >>> f = open(“test.py”,”w”) >>> f.write(‘a={“zupa”:1}’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“a”, type={}) >>> p.add_allowed_key(“a”, “zupa”) >>> p.add_config_file(“test.py”) >>> p.parse() {‘a’: {‘zupa’: 1}}

Case7 >>> f = open(“test.py”,”w”) >>> f.write(‘a={“kot”:1}’) >>> f.close() >>> p = ConfigParser() >>> p.add_option(“a”, type={}) >>> p.add_allowed_key(“a”, “kniaz”) >>> p.add_config_file(“test.py”) >>> p.parse() Traceback (most recent call last):

File “<stdin>”, line 1, in <module> File “configparser.py”, line 184, in parse raise RuntimeError(“Encountered unallowed key: ” +key+ ” for options ‘”+opt_name+”’”)

RuntimeError: Encountered unallowed key: kot for options ‘a’

Cleanup: >>> import os >>> os.remove(“test.py”)

class Option(name, **others)
add_type(type_obj)
ConfigParser.add_allowed_key(name, key)
ConfigParser.add_arbitrary_code(code)
ConfigParser.add_config_file(config_file)
ConfigParser.add_delimiter()
ConfigParser.add_option(name, **others)
ConfigParser.add_type(name, type)
ConfigParser.help()
ConfigParser.parse(allow_unknown=False, verbose=False, extra_context=None)
util.configparser.stdoutIO(*args, **kwds)

util.path module

util.path.commonpath(l1, l2, common=None)
util.path.is_abs_path(path)
util.path.is_rel_path(path)
util.path.pathsplit(p, rest=None)
util.path.rel2abs(path, base=None)

converts a relative path to an absolute path.

@param path the path to convert - if already absolute, is returned without conversion. @param base - optional. Defaults to the current directory. The base is intelligently concatenated to the given relative path. @return the relative path of path from base

util.path.relpath(p1, p2=None)
util.path.search_for_manifest(search_path)

Look for manifest in the given folder

util.path.svn_basename(url)
util.path.url_basename(url)

Get basename from an url

util.path.url_parse(url)

Check if link to a repo seems to be correct. Filter revision number and branch

util.termcolor module

ANSII Color formatting for output in terminal.

util.termcolor.colored(text, color=None, on_color=None, attrs=None)

Colorize text.

Available text colors:
red, green, yellow, blue, magenta, cyan, white.
Available text highlights:
on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
Available attributes:
bold, dark, underline, blink, reverse, concealed.
Example:
colored(‘Hello, World!’, ‘red’, ‘on_grey’, [‘blue’, ‘blink’]) colored(‘Hello, World!’, ‘green’)
util.termcolor.cprint(text, color=None, on_color=None, attrs=None, **kwargs)

Print colorize text.

It accepts arguments of print function.

Module contents

Table Of Contents

This Page