File: add_tag.py

package info (click to toggle)
xpn 1.2.6-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,500 kB
  • ctags: 1,685
  • sloc: python: 12,693; xml: 206; makefile: 16; sh: 1
file content (77 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gtk
import os
from locale import getdefaultlocale
from xpn_src.UserDir import get_wdir


class Tags_Window:
    def delete_event(self,widget,event,data=None):
        return False  

    def destroy(self,widget):
        self.window.destroy()
        if __name__=="__main__":
            gtk.main_quit()
    
    def append_tag(self,widget):
        try:
            f=open(self.FILENAME,"a")
        except IOError:
            f=open(self.FILENAME,"w")
        try: system_enc=getdefaultlocale()[1]
        except: system_enc="US-ASCII"
        tag=self.entry.get_text().decode("utf-8").encode(system_enc,"replace")
        if tag!="":
            f.write(tag+"\n")
            self.statusbar.push(1,_("TagLine Added"))
            self.entry.set_text("")
        f.close()

    def show_length(self,widget):
        length=len(self.entry.get_text().decode("utf-8"))
        self.statusbar.push(1,_("TagLine length: ")+repr(length))

    def load_state(self):
        try:
            f=open(self.FILENAME,"r")
        except IOError:
            length="0"
        else:
            length=repr(len(f.readlines()))
            f.close()
        self.statusbar.push(1,_("Found %s tags") % (length,))
        
    def __init__(self):
        self.FILENAME=os.path.join(get_wdir(),"tags.txt")
        self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event",self.delete_event)
        self.window.connect("destroy",self.destroy)
        self.window.set_title(_("XPN TagLines Manager"))
        self.window.set_position(gtk.WIN_POS_CENTER)
        vbox=gtk.VBox(False,0)
        vbox.set_border_width(2)
        label=gtk.Label()
        label.set_markup(_("\n<b>Insert here a tagline</b>\n"))
        vbox.pack_start(label,True,True,0)
        self.entry=gtk.Entry()
        self.entry.connect("changed",self.show_length)
        vbox.pack_start(self.entry,False,True,0)
        hbox_buttons=gtk.HBox()
        hbox_buttons.set_border_width(4)
        self.button_ok=gtk.Button(None,gtk.STOCK_ADD)
        self.button_ok.connect("clicked",self.append_tag)
        self.button_close=gtk.Button(None,gtk.STOCK_CLOSE)
        self.button_close.connect("clicked",self.destroy)
        hbox_buttons.pack_start(self.button_close,True,True,2)
        hbox_buttons.pack_start(self.button_ok,True,True,2)
        vbox.pack_start(hbox_buttons,True,True,0)
        self.statusbar=gtk.Statusbar()
        vbox.pack_start(self.statusbar,False,True,0)
        self.window.add(vbox)
        self.window.set_default_size(550,60)
        self.window.show_all()
        self.load_state()

if __name__=="__main__":
    tags_win=Tags_Window()
    gtk.main()