PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/kitchensink/Logger.py

http://pyjamas.googlecode.com/
Python | 62 lines | 50 code | 11 blank | 1 comment | 5 complexity | 260acced87ebc44e0baf6c24a07024a4 MD5 | raw file
 1from ui import Grid
 2
 3class Logger(Grid):
 4    instances = []
 5    
 6    def __init__(self):
 7        Logger.instances.append(self)
 8
 9        Grid.__init__(self)
10
11        self.targets=[]
12        self.targets.append("app")
13        #self.targets.append("ui")
14        self.resize(len(self.targets)+1, 2)
15        self.setBorderWidth(1)
16        self.counter=0
17        
18        self.setHTML(0, 0, "<b>Log</b>")
19        self.setText(1, 0, "app")
20        for i in range(len(self.targets)):
21            target=self.targets[i]
22            self.setText(i+1, 0, target)
23    
24    @classmethod
25    def getSingleton(self):
26        return Logger.singleton
27    
28    def setSingleton(self):
29        Logger.singleton = self
30    
31    def addTarget(self, target):
32        self.targets.append(target)
33        self.resize(len(self.targets)+1, 2)
34        self.setText(len(self.targets), 0, target)
35        return self.targets.index(target)
36    
37    @classmethod
38    def write(cls, target, message):    
39        for logger in cls.instances:
40            logger.onMessage(target, message)
41    
42    def onMessage(self, target, message):
43        self.counter+=1
44        
45        if target=='':
46            target='app'
47        target_idx=self.targets.index(target)
48        
49        # add new target
50        if target_idx<0:
51            target_idx=self.addTarget(target)
52        
53        target_row=target_idx+1     
54        old_text=self.getHTML(target_row, 1)
55        log_line=self.counter + ": " + message
56
57        if old_text=='&nbsp;':
58            new_text=log_line            
59        else:
60            new_text=old_text + "<br>" + log_line
61        self.setHTML(target_row, 1, new_text) 
62