Package ZenUtils :: Module Utils
[hide private]
[frames] | no frames]

Module Utils

source code

Classes [hide private]
  HtmlFormatter
Formatter for the logging class
  Singleton
Metaclass that ensures only a single instance of a class is ever created.
Functions [hide private]
logging handler
setWebLoggingStream(stream)
Setup logging to log to a browser using a request object.
source code
 
clearWebLoggingStream(handler)
Clear our web logger.
source code
string
convToUnits(number=0, divby=1024.0, unitstr="B")
Convert a number to its human-readable form.
source code
list
travAndColl(obj, toonerel, collect, collectname)
Walk a series of to one rels collecting collectname into collect
source code
object
getObjByPath(base, path, restricted=0)
Get a Zope object by its path (e.g.
source code
integer or None
checkClass(myclass, className)
Perform issubclass using class name as string
source code
object or None
lookupClass(productName, classname=None)
look in sys.modules for our class
source code
class
importClass(modulePath, classname="")
Import a class from the module given.
source code
string
cleanstring(value)
Take the trailing off the end of a string
source code
list
getSubObjects(base, filter=None, descend=None, retobjs=None)
Do a depth-first search looking for objects that the function filter returns as True.
source code
list
getSubObjectsMemo(base, filter=None, descend=None, memo={})
Do a depth-first search looking for objects that the function filter returns as True.
source code
list
getAllConfmonObjects(base)
Get all ZenModelRM objects in database
source code
string
zenpathsplit(pathstring)
Split a zen path and clean up any blanks or bogus spaces in it
source code
string
zenpathjoin(pathar)
Build a zenpath in its string form
source code
object
createHierarchyObj(root, name, factory, relpath="", llog=None)
Create a hierarchy object from its path we use relpath to skip down any missing relations in the path and factory is the constructor for this object.
source code
object
getHierarchyObj(root, name, relpath=None)
Return an object using its path relations are optional in the path.
source code
string
basicAuthUrl(username, password, url)
Add the username and password to a url in the form http://username:password@host/path
source code
string
prepId(id, subchar='_')
Make an id with valid url characters.
source code
tuple
sendEmail(emsg, host, port=25, usetls=0, usr='', pwd='')
Send an email.
source code
tuple
sendPage(recipient, msg, pageCommand)
Send a page.
source code
string
zdecode(context, value)
Convert a string using the decoding found in zCollectorDecoding
source code
re match object
localIpCheck(context, ip)
Test to see if an IP should not be included in the network map.
source code
re match object
localInterfaceCheck(context, intname)
Test to see if an interface should not be included in the network map.
source code
boolean
cmpClassNames(obj, classnames)
Check to see if any of an object's base classes are in a list of class names.
source code
string
resequence(context, objects, seqmap, origseq, REQUEST)
Resequence a seqmap
source code
 
cleanupSkins(dmd)
Prune out objects
source code
string
edgesToXML(edges, start=())
Convert edges to an XML file
source code
string
sane_pathjoin(base_path, *args)
Joins paths in a saner manner than os.path.join()
source code
 
zenPath(*args)
Return a path relative to $ZENHOME specified by joining args.
source code
 
zopePath(*args)
Similar to zenPath() except that this constructs a path based on ZOPEHOME rather than ZENHOME.
source code
string
binPath(fileName)
Search for the given file in a list of possible locations.
source code
string
extractPostContent(REQUEST)
IE puts the POST content in one place in the REQUEST object, and Firefox in another.
source code
integer
unused(*args)
A no-op function useful for shutting up pychecker
source code
boolean
isXmlRpc(REQUEST)
Did we receive a XML-RPC call?
source code
string
setupLoggingHeader(context, REQUEST)
Extract out the 2nd outermost table
source code
string
executeCommand(cmd, REQUEST)
Execute the command and return the output
source code
boolean
ipsort(a, b)
Compare (cmp()) a + b's IP addresses These addresses may contain subnet mask info.
source code
unsigned int
unsigned(v)
Convert negative 32-bit values into the 2's complement unsigned value
source code
 
executeStreamCommand(cmd, writefunc, timeout=30)
Execute cmd in the shell and send the output to writefunc.
source code
function
monkeypatch(target)
A decorator to patch the decorated function into the given class.
source code
function
json(f)
Decorator that serializes the return value of the decorated function as JSON.
source code
function
formreq(f)
Decorator to pass in request.form information as arguments to a method.
source code
str
>>> readable_time(1)
'1 second'
>>> readable_time(60)
'1 minute'
>>> readable_time(60*60*3+12)
'3 hours'
>>> readable_time(60*60*3+12, 2)
'3 hours 12 seconds'
readable_time(seconds, precision=1)
Convert some number of seconds into a human-readable string.
source code
str
>>> relative_time(time.time() - 60*10)
'10 minutes ago'
>>> relative_time(time.time() - 60*10-3, precision=2)
'10 minutes 3 seconds ago'
>>> relative_time(time.time() - 60*60*24*10, precision=2)
'1 week 3 days ago'
>>> relative_time(time.time() - 60*60*24*365-1, precision=2)
'1 year 1 second ago'
>>> relative_time(time.time() + 1 + 60*60*24*7*2) # Add 1 for rounding
'in 2 weeks'
relative_time(t, precision=1, cmptime=None)
Return a human-readable string describing time relative to cmptime (defaulted to now).
source code
 
is_browser_connection_open(request)
Check to see if the TCP connection to the browser is still open.
source code
string
getExitMessage(exitCode)
Return a nice exit message that corresponds to the given exit status code
source code
Variables [hide private]
  __doc__ = """Ut...
  log = logging.getLogger("zen.Utils")
  EXIT_CODE_MAPPING = {0: 'Success', 1: 'General error', 2: 'Mis...
Function Details [hide private]

setWebLoggingStream(stream)

source code 
Setup logging to log to a browser using a request object.
Parameters:
  • stream (stream class) - IO stream
Returns: logging handler
logging handler

clearWebLoggingStream(handler)

source code 
Clear our web logger.
Parameters:
  • handler (logging handler) - logging handler

convToUnits(number=0, divby=1024.0, unitstr="B")

source code 
Convert a number to its human-readable form. ie: 4GB, 4MB, etc.
>>> convToUnits() # Don't do this!
'0.0B'
>>> convToUnits(None) # Don't do this!
''
>>> convToUnits(123456789)
'117.7MB'
>>> convToUnits(123456789, 1000, "Hz")
'123.5MHz'
Parameters:
  • number (number) - base number
  • divby (number) - divisor to use to convert to appropriate prefix
  • unitstr (string) - base unit of the number
Returns: string
number with appropriate units

travAndColl(obj, toonerel, collect, collectname)

source code 
Walk a series of to one rels collecting collectname into collect
Parameters:
  • obj (object) - object inside of Zope
  • toonerel (toonerel object) - a to-one relationship object
  • collect (list) - object list
  • collectname (string) - name inside of the to-one relation object
Returns: list
list of objects

getObjByPath(base, path, restricted=0)

source code 
Get a Zope object by its path (e.g. '/Devices/Server/Linux'). Mostly a stripdown of unrestrictedTraverse method from Zope 2.8.8.
Parameters:
  • base (string) - base part of a path
  • path (string) - path to an object inside of the DMD
  • restricted (integer) - flag indicated whether to use securityManager
Returns: object
object pointed to by the path

checkClass(myclass, className)

source code 
Perform issubclass using class name as string
Parameters:
  • myclass (object) - generic object
  • className (string) - name of a class
Returns: integer or None
the value 1 if found or None

lookupClass(productName, classname=None)

source code 
look in sys.modules for our class
Parameters:
  • productName (string) - object in Products
  • classname (string) - class name
Returns: object or None
object at the classname in Products

importClass(modulePath, classname="")

source code 
Import a class from the module given.
Parameters:
  • modulePath (string) - path to module in sys.modules
  • classname (string) - name of a class
Returns: class
the class in the module

cleanstring(value)

source code 
Take the trailing off the end of a string
Parameters:
  • unitstr (string) - sample string
Returns: string
cleaned string

getSubObjects(base, filter=None, descend=None, retobjs=None)

source code 
Do a depth-first search looking for objects that the function filter returns as True. If descend is passed it will check to see if we should keep going down or not
Parameters:
  • base (object) - base object to start search
  • filter (function or None) - filter to apply to each object to determine if it gets added to the returned list
  • descend (function or None) - function to apply to each object to determine whether or not to continue searching
  • retobjs (list) - list of objects found
Returns: list
list of objects found

getSubObjectsMemo(base, filter=None, descend=None, memo={})

source code 

Do a depth-first search looking for objects that the function filter returns as True. If descend is passed it will check to see if we should keep going down or not.

This is a Python iterable.
Parameters:
  • base (object) - base object to start search
  • filter (function or None) - filter to apply to each object to determine if it gets added to the returned list
  • descend (function or None) - function to apply to each object to determine whether or not to continue searching
  • memo (dictionary) - dictionary of objects found (unused)
Returns: list
list of objects found

getAllConfmonObjects(base)

source code 
Get all ZenModelRM objects in database
Parameters:
  • base (object) - base object to start searching
Returns: list
list of objects

zenpathsplit(pathstring)

source code 
Split a zen path and clean up any blanks or bogus spaces in it
Parameters:
  • pathstring (string) - a path inside of ZENHOME
Returns: string
a path

zenpathjoin(pathar)

source code 
Build a zenpath in its string form
Parameters:
  • pathstring (string) - a path
Returns: string
a path

createHierarchyObj(root, name, factory, relpath="", llog=None)

source code 
Create a hierarchy object from its path we use relpath to skip down any missing relations in the path and factory is the constructor for this object.
Parameters:
  • root (object) - root from which to start
  • name (string) - path to object
  • factory (factory object) - factory object to create
  • relpath (object) - relationship within which we will recurse as objects are created, if any
  • llog (object) - unused
Returns: object
root object of a hierarchy

getHierarchyObj(root, name, relpath=None)

source code 
Return an object using its path relations are optional in the path.
Parameters:
  • root (object) - root from which to start
  • name (string) - path to object
  • relpath (object) - relationship within which we will recurse as objects are created, if any
Returns: object
root object of a hierarchy

basicAuthUrl(username, password, url)

source code 
Add the username and password to a url in the form http://username:password@host/path
Parameters:
  • username (string) - username
  • password (string) - password
  • url (string) - base URL to add username/password info
Returns: string
URL with auth information incorporated

prepId(id, subchar='_')

source code 
Make an id with valid url characters. Subs [^a-zA-Z0-9-_,.$\(\) ] with subchar. If id then starts with subchar it is removed.
Parameters:
  • id (string) - user-supplied id
Returns: string
valid id

sendEmail(emsg, host, port=25, usetls=0, usr='', pwd='')

source code 
Send an email. Return a tuple: (sucess, message) where sucess is True or False.
Parameters:
  • emsg (string) - message to send
  • host (string) - name of e-mail server
  • port (integer) - port number to communicate to the e-mail server
  • usetls (integer) - boolean-type integer to specify whether to use TLS
  • usr (string) - username for TLS
  • pwd (string) - password for TLS
Returns: tuple
(sucess, message) where sucess is True or False.

sendPage(recipient, msg, pageCommand)

source code 
Send a page. Return a tuple: (success, message) where sucess is True or False.
Parameters:
  • recipient (string) - name to where a page should be sent
  • msg (string) - message to send
  • pageCommand (string) - command that will send a page
Returns: tuple
(sucess, message) where sucess is True or False.

zdecode(context, value)

source code 
Convert a string using the decoding found in zCollectorDecoding
Parameters:
  • context (object) - Zope object
  • value (string) - input string
Returns: string
converted string

localIpCheck(context, ip)

source code 
Test to see if an IP should not be included in the network map. Uses the zLocalIpAddresses to decide.
Parameters:
  • context (object) - Zope object
  • ip (string) - IP address
Returns: re match object
regular expression match or None (if not found)

localInterfaceCheck(context, intname)

source code 
Test to see if an interface should not be included in the network map. Uses the zLocalInterfaceNames to decide.
Parameters:
  • context (object) - Zope object
  • intname (string) - network interface name
Returns: re match object
regular expression match or None (if not found)

cmpClassNames(obj, classnames)

source code 
Check to see if any of an object's base classes are in a list of class names. Like isinstance(), but without requiring a class to compare against.
Parameters:
  • obj (object) - object
  • classnames (list of strings) - class names
Returns: boolean
result of the comparison

resequence(context, objects, seqmap, origseq, REQUEST)

source code 
Resequence a seqmap
Parameters:
  • context (object) - Zope object
  • objects (list) - objects
  • seqmap (list) - sequence map
  • origseq (list) - sequence map
  • REQUEST (Zope REQUEST object) - Zope REQUEST object
Returns: string

cleanupSkins(dmd)

source code 
Prune out objects
Parameters:
  • dmd (DMD object) - Device Management Database

edgesToXML(edges, start=())

source code 
Convert edges to an XML file
Parameters:
  • edges (list) - edges
Returns: string
XML-formatted string

sane_pathjoin(base_path, *args)

source code 
Joins paths in a saner manner than os.path.join()
Parameters:
  • base_path (string) - base path to assume everything is rooted from
  • *args (strings) - path components starting from $ZENHOME
Returns: string
sanitized path

zenPath(*args)

source code 
Return a path relative to $ZENHOME specified by joining args. The path is not guaranteed to exist on the filesystem.
>>> import os
>>> zenHome = os.environ['ZENHOME']
>>> zenPath() == zenHome
True
>>> zenPath( '' ) == zenHome
True
>>> zenPath('Products') == os.path.join(zenHome, 'Products')
True
>>> zenPath('/Products/') == zenPath('Products')
True
>>> 
>>> zenPath('Products', 'foo') == zenPath('Products/foo')
True

# NB: The following is *NOT* true for os.path.join() >>> zenPath('/Products', '/foo') == zenPath('Products/foo') True >>> zenPath(zenPath('Products')) == zenPath('Products') True >>> zenPath(zenPath('Products'), 'orange', 'blue' ) == zenPath('Products', 'orange', 'blue' ) True

# Pathological case # NB: need to expand out the array returned by split() >>> zenPath() == zenPath( *'/'.split(zenPath()) ) True
Parameters:
  • *args (strings) - path components starting from $ZENHOME

To Do: determine what the correct behaviour should be if $ZENHOME is a symlink!

zopePath(*args)

source code 
Similar to zenPath() except that this constructs a path based on ZOPEHOME rather than ZENHOME. This is useful on the appliance. If ZOPEHOME is not defined or is empty then return ''. NOTE: A non-empty return value does not guarantee that the path exists, just that ZOPEHOME is defined.
>>> import os
>>> zopeHome = os.environ.setdefault('ZOPEHOME', '/something')
>>> zopePath('bin') == os.path.join(zopeHome, 'bin')
True
>>> zopePath(zopePath('bin')) == zopePath('bin')
True
Parameters:
  • *args (strings) - path components starting from $ZOPEHOME

binPath(fileName)

source code 
Search for the given file in a list of possible locations. Return either the full path to the file or '' if the file was not found.
>>> len(binPath('zenoss')) > 0
True
>>> len(binPath('zeoup.py')) > 0
True
>>> len(binPath('check_http')) > 0
True
>>> binPath('Idontexistreally') == ''
True
Parameters:
  • fileName (string) - name of executable
Returns: string
path to file or '' if not found

extractPostContent(REQUEST)

source code 
IE puts the POST content in one place in the REQUEST object, and Firefox in another. Thus we need to try both.
Parameters:
  • REQUEST (Zope REQUEST object) - Zope REQUEST object
Returns: string
POST content

unused(*args)

source code 
A no-op function useful for shutting up pychecker
Parameters:
  • *args (objects) - arbitrary arguments
Returns: integer
count of the objects

isXmlRpc(REQUEST)

source code 
Did we receive a XML-RPC call?
Parameters:
  • REQUEST (Zope REQUEST object) - Zope REQUEST object
Returns: boolean
True if REQUEST is an XML-RPC call

setupLoggingHeader(context, REQUEST)

source code 
Extract out the 2nd outermost table
Parameters:
  • context (Zope object) - Zope object
  • REQUEST (Zope REQUEST object) - Zope REQUEST object
Returns: string
response

executeCommand(cmd, REQUEST)

source code 
Execute the command and return the output
Parameters:
  • cmd (string) - command to execute
  • REQUEST (Zope REQUEST object) - Zope REQUEST object
Returns: string
result of executing the command

ipsort(a, b)

source code 
Compare (cmp()) a + b's IP addresses These addresses may contain subnet mask info.
Parameters:
  • a (string) - IP address
  • b (string) - IP address
Returns: boolean
result of cmp(a.ip,b.ip)

unsigned(v)

source code 
Convert negative 32-bit values into the 2's complement unsigned value
>>> str(unsigned(-1))
'4294967295'
>>> unsigned(1)
1L
>>> unsigned(1e6)
1000000L
>>> unsigned(1e10)
10000000000L
Parameters:
  • v (negative 32-bit number) - number
Returns: unsigned int
2's complement unsigned value

executeStreamCommand(cmd, writefunc, timeout=30)

source code 
Execute cmd in the shell and send the output to writefunc.
Parameters:
  • cmd (string) - command to execute
  • writefunc (function) - output function
  • timeout (number) - maxium number of seconds to wait for the command to execute

monkeypatch(target)

source code 
A decorator to patch the decorated function into the given class.
>>> @monkeypatch('Products.ZenModel.DataRoot.DataRoot')
... def do_nothing_at_all(self):
...     print "I do nothing at all."
...
>>> from Products.ZenModel.DataRoot import DataRoot
>>> hasattr(DataRoot, 'do_nothing_at_all')
True
>>> DataRoot('dummy').do_nothing_at_all()
I do nothing at all.
Parameters:
  • target (class object) - class
Returns: function
decorator function return

json(f)

source code 

Decorator that serializes the return value of the decorated function as JSON.

Use of the ZenUtils.Utils.json decorator is deprecated. Please import from ZenUtils.json.
>>> @json
... def f():
...     return (dict(a=1L), u"123", 123)
...
>>> print f()
[{"a": 1}, "123", 123]
Parameters:
  • f (class object) - class
Returns: function
decorator function return

Deprecated: import from Products.ZenWidgets.json

formreq(f)

source code 

Decorator to pass in request.form information as arguments to a method.

These are intended to decorate methods of BrowserViews.
Parameters:
  • f (class object) - class
Returns: function
decorator function return

readable_time(seconds, precision=1)

source code 
Convert some number of seconds into a human-readable string.
Parameters:
  • t (int) - The number of seconds to convert
  • precision - The maximum number of time units to include.
Returns: str
>>> readable_time(1)
'1 second'
>>> readable_time(60)
'1 minute'
>>> readable_time(60*60*3+12)
'3 hours'
>>> readable_time(60*60*3+12, 2)
'3 hours 12 seconds'

relative_time(t, precision=1, cmptime=None)

source code 
Return a human-readable string describing time relative to cmptime (defaulted to now).
Parameters:
  • t (int) - The time to convert, in seconds since the epoch.
  • precision - The maximum number of time units to include.
  • cmptime (int) - The time from which to compute the difference, in seconds since the epoch
Returns: str
>>> relative_time(time.time() - 60*10)
'10 minutes ago'
>>> relative_time(time.time() - 60*10-3, precision=2)
'10 minutes 3 seconds ago'
>>> relative_time(time.time() - 60*60*24*10, precision=2)
'1 week 3 days ago'
>>> relative_time(time.time() - 60*60*24*365-1, precision=2)
'1 year 1 second ago'
>>> relative_time(time.time() + 1 + 60*60*24*7*2) # Add 1 for rounding
'in 2 weeks'

is_browser_connection_open(request)

source code 

Check to see if the TCP connection to the browser is still open.

This might be used to interrupt an infinite while loop, which would preclude the thread from being destroyed even though the connection has been closed.

getExitMessage(exitCode)

source code 
Return a nice exit message that corresponds to the given exit status code
Parameters:
  • exitCode (integer) - process exit code
Returns: string
human-readable version of the exit code

Variables Details [hide private]

__doc__

Value:
"""Utils

General utility functions module

"""

EXIT_CODE_MAPPING

Value:
{0: 'Success', 1: 'General error', 2: 'Misuse of shell builtins', 126:\
 'Command invoked cannot execute, permissions problem or command is no\
t an executable', 127: 'Command not found', 128: 'Invalid argument to \
exit, exit takes only integers in the range 0-255', 130: 'Fatal error \
signal: 2, Command terminated by Control-C'}