#!/usr/bin/python """ Single-class module containing sparkLine Class """ __author__ = "Rian van Staden" __copyright__ = "Copyright 2005, Rian van Staden" __contributors__ = [] __version__ = "0.0.2" __license__ = "GPL" __history__ = """ """ import SOAPpy import binascii class sparkLine: """ Serves as class for a sparkLine stub object using a SOAP Central service at http://soap.rianvanstaden.com:2020 If run directly, the module creates two test sparkline PNG files called smooth.png and discrete.png """ def __init__(self,datalist=()): """ Constructor for sparkLine Takes dataset as optional argument, sets default values for all SOAP object arguments """ self._serv = SOAPpy.SOAPProxy("http://soap.rianvanstaden.com:2020") self._args = {} # default args for all sparklines self._args['step'] = '1' self._args['height'] = '20' # default args for discrete sparkline self._args['upper'] = '50' self._args['below-color'] = '#078007' self._args['above-color'] = '#f30404' # default args for smooth sparkline self._args['min-color'] = '#0000FF' self._args['max-color'] = '#00FF00' self._args['last-color'] = '#FF0000' self._args['min-m'] = 'true' #show min dot self._args['max-m'] = 'true' #show max dot self._args['last-m'] = 'true' #show last dot self._data = datalist self._type = 'discrete' self._update = False self._spark = None def setAttribute(self,attrib,value): """ Set an attribute of the sparkLine @param attrib: Attribute name @type attrib: String @param value: Attribute value @type value: String or Integer """ self._args[attrib] = str(value) self._update = True def getAttribute(self,attrib): """ Get an attribute of the sparkLine @param attrib: Attribute name @type attrib: String @return: Returns the current value of the attribute @rtype: String """ if self._args.has_key(attrib): result = self._args[attrib] else: result = None return result def setData(self,data): """ Set the dataset of the sparkLine @param data: Sparkline dataset @type data: List of Integers """ self._data = data self._update = True def getData(self): """ Get the dataset of the sparkLine @return: Returns the current dataset @rtype: List of Integers """ return self._data def setType(self,type): """ Set the type of the sparkLine @param type: sparkLine type, one of "discrete" or "smooth" @type type: String """ self._type = type self._update = True def getType(self): """ Get the type of the sparkLine @return: Returns the current type of the attribute @rtype: String, one of "discrete" or "smooth" """ return self._type def getSparkLine(self): """ Get the sparkLine PNG representation. Cached to reduce SOAP traffic. @return: Returns the sparkLine @rtype: Binary representation of a PNG image """ if self._update: try: base64_sparkline = self._serv.sparkline(self._type,self._data,self._args) self._spark = binascii.a2b_base64(base64_sparkline) except: self._spark = None return self._spark def test(): """ Test routine to do a few basic tests (*not* a unit test replacement!) """ s = sparkLine((10,20,30,40,50,60,70,80,50,20,10,10,50,100,90,70,50,30,10,0,0,10,20,30,40,50)) # Same as: # s = sparkLine() # s.setData((10,20,30,40,50,60,70,80,50,20,10,10,50,100,90,70,50,30,10,0,0,10,20,30,40,50)) print 'Data: %s' % str(s.getData()) s.setAttribute('step',2) print 'Step: %s' % s.getAttribute('step') s.setAttribute('height',25) print 'Height: %s' % s.getAttribute('height') for t in ('smooth','discrete'): s.setType(t) fname = '%s.png' % (t) f = open(fname,'w+b') f.write(s.getSparkLine()) f.close() print 'Wrote: %s.png' % t print "Done creating test PNGs..." if __name__=="__main__": test()