PageRenderTime 102ms CodeModel.GetById 61ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

/examples/mail/Logger.py

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