~ubuntu-branches/ubuntu/natty/stfl/natty

« back to all changes in this revision

Viewing changes to python/example.py

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2007-08-07 13:06:54 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070807130654-fmvsraotulj0nbri
Tags: 0.15-3
Again added fPIC to CFLAGS. Hopefully all build issues
are fixed now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
__author__ = "chrysn <chrysn@fsfe.org>"
 
4
__copyright__ = "Copyright 2007, chrysn"
 
5
__license__ = "GPL"
 
6
 
 
7
import sys
 
8
import csv
 
9
 
1
10
import stfl
2
 
stfl.create('{vbox @style_normal:bg=blue}').run(0)
 
11
 
 
12
class CSV(object):
 
13
        def __init__(self,file,delimiter):
 
14
                self.file=file
 
15
                self.delimiter=delimiter
 
16
 
 
17
                self.data=[l for l in csv.reader(open(self.file),delimiter=self.delimiter)]
 
18
                maxlen=max([len(l) for l in self.data])
 
19
                for l in self.data:
 
20
                        while len(l)<maxlen: l.append("")
 
21
        
 
22
        @staticmethod
 
23
        def cellname(row,col): return "cell_%d_%d"%(row,col)
 
24
        @staticmethod
 
25
        def valuename(row,col): return "value_%d_%d"%(row,col)
 
26
        @staticmethod
 
27
        def namecell(name): 
 
28
                tag,row,col=name.split('_')
 
29
                assert tag=='cell'
 
30
                row,col=int(row),int(col)
 
31
                return row,col
 
32
        
 
33
        def stfltable(self):
 
34
                return "{table .expand:1 %s}"%"{tablebr}".join([
 
35
                                " ".join([
 
36
                                        "{input[%s] text[%s]:%s style_focus:bg=blue}"%(self.cellname(row,col),self.valuename(row,col),stfl.quote(x))
 
37
                                        for col,x in zip(range(len(l)),l)
 
38
                                        ])
 
39
                                for row,l in zip(range(len(self.data)),self.data)
 
40
                        ])
 
41
        
 
42
        def updatefromstfl(self, f):
 
43
                for row in range(len(self.data)):
 
44
                        for col in range(len(self.data[row])):
 
45
                                self.data[row][col]=f.get(self.valuename(row,col))
 
46
 
 
47
        
 
48
        def save(self):
 
49
                w=csv.writer(open(self.file,'w'),delimiter=self.delimiter)
 
50
                w.writerows(self.data)
 
51
        
 
52
 
 
53
if __name__=="__main__":
 
54
        if len(sys.argv) not in [2,3]:
 
55
                print "Usage: %s file.csv [delimiter]"%sys.argv[0]
 
56
                sys.exit(1)
 
57
        
 
58
        c=CSV(sys.argv[1], len(sys.argv)==3 and sys.argv[2] or ",")
 
59
 
 
60
        table=c.stfltable()
 
61
 
 
62
        form="{vbox %s {label} {hbox .expand:0 @style_normal:attr=reverse {label text[statusbar]:}{label text[help]: .tie:r}}"%table
 
63
        
 
64
        f=stfl.create(form)
 
65
 
 
66
        def setstatus(text):
 
67
                f.set('statusbar',text)
 
68
        def sethelp(text):
 
69
                f.set('help',text)
 
70
        def currentcell():
 
71
                return c.namecell(f.get_focus())
 
72
        def setcell(row,col):
 
73
                f.set_focus(c.cellname(row,col))
 
74
        
 
75
        setstatus("editing %s"%c.file)
 
76
        sethelp("^W = write, ^C = exit")
 
77
 
 
78
        while True:
 
79
                try:
 
80
                        e=f.run(0)
 
81
                except KeyboardInterrupt:
 
82
                        e='^C' # other possible reasons?
 
83
                row,col=currentcell()
 
84
                setstatus("editing %s, row %d, col %d"%(c.file,row,col))
 
85
                if e=="ENTER":
 
86
                        row=row+1
 
87
                        setcell(row,col)
 
88
                elif e=='^C':
 
89
                        break;
 
90
                elif e=='^W':
 
91
                        c.updatefromstfl(f)
 
92
                        c.save()
 
93
                        setstatus("saved to %s"%c.file)