sipi Package

sipi Package

sipi
Simple Intuitive Programming Interface

@created: on 2012-01-12 @author: jldupont

cfg Module

Simple Configuration Information API

Configuration files are kept in ~/.config/ (or can be configured through the module’s global base_dir). Configuration parameters are treated as “read-only”. Configuration file format can either be .json or .yaml , the priority being given to .json.

Configuration file names can either be:

  1. specified using the filename parameter
  2. specified using the ns parameter
  3. inferred using the command line argument #0
  4. inferred using the command line argument #1

The options are listed in order of priority.

sipi.cfg.get(param, default=None)[source]

Get a configuration parameter

Returns a native value e.g. list, dict, integer, string

>>> get("param1", default="value1")
'value1'
sipi.cfg.which()[source]

Reports which file will be used to provide configuration

file and ns are optional.

>>> which()
('ns', 'cfg')

tools_os Module

Sipi - Simple OS functions

sipi.tools_os.can_write(path)

Checks if the current user (i.e. the script) can delete the given path Must check both user & group level permissions

FOR THIS TEST, NEED TO CREATE A DIRECTORY /tmp/_test_root_sipi THROUGH ROOT

>>> p="/tmp/_test_sipi"
>>> rm(p)
('ok', '/tmp/_test_sipi')
>>> touch(p)
('ok', '/tmp/_test_sipi')
>>> can_write(p)
('ok', True)
>>> rm(p)
('ok', '/tmp/_test_sipi')
>>> can_write("/tmp/_test_root_sipi/some_file")
('ok', False)
sipi.tools_os.file_contents(path)

Simple “get file contents”

>>> rm('/tmp/JustTouching')
('ok', '/tmp/JustTouching')
>>> touch("/tmp/JustTouching")
('ok', '/tmp/JustTouching')
>>> file_contents("/tmp/JustTouching")
('ok', '')
>>> rm('/tmp/JustTouching')
('ok', '/tmp/JustTouching')
>>> file_contents("/tmp/JustTouching")
('error', None)
sipi.tools_os.gen_walk(path, max_files=None)

os.walk generator with an optional limit on the number of files returned

sipi.tools_os.mkdir_p(path)[source]

Silently (i.e. no exception thrown) makes a directory structure if possible

>>> mkdir_p("/tmp/JustATestDir")
('ok', '/tmp/JustATestDir')
>>> mkdir_p("/tmp/JustATestDir")
('ok', '/tmp/JustATestDir')
>>> rmdir("/tmp/JustATestDir")
('ok', '/tmp/JustATestDir')
sipi.tools_os.move(src_path, dst_path)
sipi.tools_os.quick_write(path, contents)

Best effort “write contents to file”

>>> quick_write("/tmp/QuickWriteTest", "{'param': 'value'}") 
('ok', ...)
>>> file_contents("/tmp/QuickWriteTest")
('ok', "{'param': 'value'}")
>>> rm('/tmp/QuickWriteTest') 
('ok', ...)
sipi.tools_os.remove_common_prefix(common_prefix, path)
>>> remove_common_prefix("/tmp", "/tmp/some_dir/some_file.ext")
('ok', '/some_dir/some_file.ext')
sipi.tools_os.resolve_path(path)

Resolves a path with user and environment variables

>>> resolve_path("~/.config") 
'/home/.../.config'
sipi.tools_os.rm(path)[source]

Silently (i.e. no exception thrown) removes a path if possible

>>> rm("/tmp/NotAFile") ## no need to complain if there is no file
('ok', '/tmp/NotAFile')
sipi.tools_os.rmdir(path)[source]

Silently (i.e. no exception thrown) removes a directory if possible

>>> rmdir("/tmp/JustATestDir")
('error', (2, 'ENOENT'))
>>> mkdir_p("/tmp/JustATestDir")
('ok', '/tmp/JustATestDir')
>>> rmdir("/tmp/JustATestDir")
('ok', '/tmp/JustATestDir')
sipi.tools_os.touch(path)[source]
>>> touch("/tmp/JustTouching")
('ok', '/tmp/JustTouching')
>>> rm('/tmp/JustTouching')
('ok', '/tmp/JustTouching')

tools_dict Module

Created on 2012-01-27 @author: jldupont

sipi.tools_dict.strip(d)[source]

Strip each element in a dict

>>> strip({"e1":" v1", "e2":" v2 "})
{'e1': 'v1', 'e2': 'v2'}

cmds Module

Simple Command Line tools

sipi.cmds.process(line_proc, input_ctx={}, parser_args=None, desc='')[source]

Process input files

line_proc must be a callable which takes 1 tuple as input: (ctx, (code, data))

The parameter ctx contains contextual information:

  • args: processed command line arguments (from argparse)
  • file: path of current file being processed
  • lineno: line number in current file
  • line: actual string of the line
  • total_files: the current total of files processed (starts at 1)
  • total_lines: the current total of lines processed for all files processed

Codes sent to line_proc:

  • open: processing starts
  • close: processing ends
  • file start: processing of a new file starts
  • file end: processing of current file ends
  • line: actual line of a file

The parameter parser_args is a list of tuples (pargs, kargs) for parser.add_argument of the standard library module argparse.

The parameter desc is a string description of the command line processor.

flow Module

Simple Flow tools

sipi.flow.build_pipeline(blocks, pipe=None)[source]

Build a pipeline from the list of blocks

The first element of the list must correspond to the first block of the pipeline.

Each block is a tuple (handler_function, name). The handler_function must accept 1 tuple parameter of the following signature: (next_block, (context, msg)).

sipi.flow.coroutine(func)[source]

Decorator for coroutine creation & bootstrapping

Table Of Contents

Previous topic

Welcome to sipi’s documentation!

This Page