# Based upon Mike Meyer's p4.py
# License:
# This file and any derivatives or translations of it may be freely
# copied and redistributed so long as:
# 1) This license and copyright notice are not changed.
# 2) Any fixes or enhancements are reported back to either the
# author (mwm@phone.net).
# and any of:
# a) The source is redistributed with it.
# b) The source is compiled unmodified, and instructions for finding
# the original source are included.
# c) The source is made available by some other means.
from P4Client import *
import string
class Error(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return `self.value`
class P4Handler:
"""Class to wrap P4Client to parse various Output* output formats.
"""
def __init__(my, **options):
"Create the P4Client object I'm wrapping."
my.data = []
my.inputData = ""
my.debug = 0
my.statData = []
my.p4 = apply(P4Client, (my,), options)
my.p4.SetProtocol( "specstring", "" )
my.p4.SetProtocol("tag", "")
my.p4.Init()
def Dropped(my):
"Check to see if the P4Client connection has been dropped."
return my.p4.Dropped()
def Final(my):
"Close the connection to the P4Client."
return my.p4.Final()
def Run(my, command, *args):
"Run the given command."
my.data = []
my.statData = []
my.p4.SetArgv(args)
my.p4.Run(command)
return my.statData
def OutputInfo(my, data, level):
"Parse the given data, adding it to my data."
for line in string.split(data, '\n'):
my.data.append(line)
def InputData(my):
"Returns input data."
if my.debug: print "Python: InputData"
return my.inputData
def OutputStat(my, d):
my.statData.append(d)
def OutputText(my, text):
"Adds text lines to the output data."
for line in string.split(text, '\n'):
my.data.append(line)
def HandleError(my, text, severity = None):
"Raise an error from P4."
raise Error(text)
OutputError = HandleError