API

unitils.cat(files, number=False)[source]

iterate through each file in files and yield each line in turn.

Parameters:
  • files (list of open file-like objects) – The files to concatenate
  • number (boolean) – If True, yield two-tuples of (line_number, line)
unitils.cp(src, dst, no_clobber=False, recursive=False)[source]

Copy src to dst.

Parameters:
  • src (str) – The file(s) to copy
  • dst (str) – The destination for src
  • no_clobber (boolean) – If True, do not overwrite files if they already exist
  • recursive (boolean) – If True recursively copy the contents of src to dst
unitils.find(path='.', name=None, iname=None, ftype='*')[source]

Search for files in a directory heirarchy.

This is dramatically different from the GNU version of find. There is no Domain Specific language.

Parameters:
  • path (str) – The directory to start in
  • name (str) – The name spec (glob pattern) to search for
  • iname (str) – The case-insensitive name spec (glob pattern) to search for
  • ftype (str) – The type of file to search for must be one of b, c, d, p, f, k, s or *
unitils.grep(expr, files, line_numbers=False, filenames=False, color=False, invert_match=False, ignore_case=False)[source]

search the contents of files for expr, yield the results

files can be a filename as str, a list of filenames, a file-like object or a list of file-like objects. In any case, all files will be searched line-by-line for any lines which contain expr which will be yielded.

This does not support sending text in directly to search, the reason is that this operation is fairly simple in Python:

import re

expr = re.compile(r"^\d+\s\w+")
matches = (l for l in text.splitlines() if expr.search(line))
for line in matching_lines:
    print(line)
Parameters:
  • files (str, list, file) – files to search for expr
  • expr (str, compiled regex) – the regular expression to search for
  • line_numbers (bool) – If True, line numbers will be prepended to results
  • filenames (bool) – If True, filenames will be prepended to results
unitils.head(files, lines=10, verbose=False, quiet=False)[source]

Read the first 10 lines (by default) of each file in files.

As this is supposed to imitate the behavior of head, but also be used as a Python callable, some liberties have been taken to accomodate the functionality.

If one file is passed in, a generator is returned which will yield the first n lines in the file.

If more than one file is passed in, a dict is returned keyed by filename mapping to a generator which will yield the first n lines of that file.

Parameters:
  • files (str list) – The files to examine
  • lines (int) – The number of lines to yield from each file
  • verbose (boolean) – Always include the filename (as described above)
  • quiet (boolean) – Never include the filename (as described above)
unitils.ls(path='.', _all=False, almost_all=False)[source]

Iterator yielding information about path (defaults to current directory)

Currently this will only list the contents of a directory. More features will be added in the near future, but if there is a certain feature you are in need of, please don’t hesitate to submit an issue in our issue tracker or better yet submit a pull request

Parameters:
  • path (str) – The directory to list
  • _all (boolean) – If True files starting with ”.” are not ignored
  • almost_all (boolean) – Like _all, but do not include ”.” and ”..”
unitils.mv(src, dst)[source]

Move or rename src to dst.

Parameters:
  • src (str) – The file/directory to move
  • dst – The destination for the files to be moved
  • dst – str
unitils.system_call(command, stdin=-1, stdout=-1, stderr=-2, shell=False)[source]

Helper function to shell out commands. This should be platform agnostic.

Arguments are the same as to subprocess.Popen. Returns (stdout, stderr, returncode)

unitils.watch(command, interval=2)[source]

Iterator yielding a tuple of (stdout, stderr, returncode) returned by issuing command to the system repeatedly. By default sleeps for 2 seconds between issuing commands.

Parameters:
  • command (str, list) – The command to issue to the system
  • interval (int) – The number of seconds to wait before issuing command again
Returns:

Iterator of three-tuples containing (stdout, stderr, returncode)

unitils.wc(files, lines=False, byte_count=False, chars=False, words=False, max_line_length=False)[source]

Yields newline, word and byte counts for each file and a total line if more than one file is specified

Parameters:
  • files (iterable) – An iterable of open, file-like objects or strings containing filenames
  • lines (boolean) – Whether to include line counts
  • byte_count (boolean) – Whether to include bytes count
  • chars (boolean) – Whether to include chars count
  • words (boolean) – Whether to include word count
  • max_line_lenth (boolean) – Whether to include max_line_length
unitils.which(cmd, _all=False)[source]

Search the PATH for the first occurance of cmd.

Parameters:
  • cmd (str) – The command for which to search
  • _all (boolean) – If True, all occurances of cmd on the PATH will be returned