Package inca :: Module UsageReporter
[hide private]
[frames] | no frames]

Source Code for Module inca.UsageReporter

  1  from inca.Reporter import Reporter 
  2   
3 -class UsageReporter(Reporter):
4 """Usage - Module for creating simple usage reports:: 5 6 from inca.UsageReporter import UsageReporter 7 reporter = new UsageReporter( 8 name = 'My Reporter', 9 version = 0.1, 10 description = 'What my reporter does', 11 url = 'http://some.where/' 12 ) 13 14 This module is a subclass of inca.Reporter that provides a simple schema 15 for reporting usage data. The reporter will return the following body if 16 the reporter is successful:: 17 18 <usage> 19 <entry> 20 <type>type1</type> 21 <name>foo</name> 22 <statistics> 23 <statistic> 24 <name>count</name> 25 <value>9</value> 26 </statistic> 27 </statistics> 28 </entry> 29 <entry> 30 31 . 32 . 33 . 34 35 </usage> 36 """ 37
38 - def __init__(self, **attributes):
39 """Class constructor that returns a new Usage object. The constructor 40 accepts the parameters supported by inca.Reporter. 41 """ 42 Reporter.__init__(self, **attributes) 43 self.entries = [] 44 self.addDependency('inca.UsageReporter');
45
46 - def addEntry(self, entry):
47 """This method is used to add an entry to the usage report. It takes a 48 dict containing the type and name of the entry as well as a dict of 49 statistics. For example:: 50 51 addEntry({ 52 'type' : 'foo', 53 'name' : 'bar', 54 'stats' : { 55 'count' : 1, 56 'blort' : 'baz' 57 } 58 }) 59 60 Will create an entry of type 'foo', with a name of 'bar' and two 61 statistics, count with a value of 1 and 'blort' with a value of 'baz'. 62 """ 63 self.entries.append(entry)
64
65 - def fail(self, error_msg):
66 """This method is used to indicate that the reporter failed. It takes a 67 single arguement which is the error message which will be returned. 68 """ 69 self.setResult(0, error_msg)
70
71 - def getEntries(self):
72 """Returns a list of all entries that have been added to the reporter.""" 73 return self.entries
74
75 - def reportBody(self):
76 """Constructs the body and returns it.""" 77 xml = [] 78 for entry in self.entries: 79 xml.append(self.xmlElement('entry', 0, *self._reportEntry(entry))) 80 return self.xmlElement('usage', 0, *xml)
81
82 - def success(self):
83 """This method is called to indicate that the reporter has succeeded.""" 84 self.setResult(1)
85
86 - def _reportStatistics(self, stats):
87 xml = [] 88 for stat in stats.keys(): 89 xml.append(self.xmlElement('statistic', 0, 90 self.xmlElement('name', 1, stat), 91 self.xmlElement('value', 1, stats[stat]) 92 )) 93 return xml
94
95 - def _reportEntry(self, entry):
96 return [ 97 self.xmlElement('type', 1, entry['type']), 98 self.xmlElement('name', 1, entry['name']), 99 self.xmlElement('statistics', 0, *self._reportStatistics(entry['stats'])) 100 ]
101