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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#!/usr/bin/env python
import sys import urllib import urllib2 import HTMLParser
try: import pygtk pygtk.require("2.0") import gtk import pango except: print "Cannot load gtk lib!" sys.exit(2)
langs=<'en','ru','fr','it','sp','la','de','uk'>
class ABBYY(HTMLParser.HTMLParser): OK=0 get_data=0 line="" result="" tabs=1
def __init__(self,word,sl,tl,func): HTMLParser.HTMLParser.__init__(self) self.u='%s-%s/%s' % (sl,tl,word) self.u = urllib.quote(self.u) self.u = 'http://lingvo.abbyyonline.com/ru/%s' % self.u self.req = urllib2.urlopen(self.u) self.code = self.req.read() if self.code.find("NoTranslation")<0: self.OK=1 else: self.result="Not Found" self.feed(self.code) if len(self.result)==0:self.result="Cant translate" func(self.result)
def handle_starttag(self,tag,attrs): if self.OK: if tag.lower() == 'span': for attr,value in attrs: if attr == 'class' and value == 'lol-dict-name':self.get_data=1 if attr == 'class' and value.lower() == 'Abbre':self.tabs=2 elif tag.lower() == 'p': for attr,value in attrs: if attr == 'class' and value<0:1>.lower() == 'p': self.get_data=2 if len(value)>1: self.tabs = int(value<1:2>) + 1 else: self.tabs = 1
def handle_data(self,data): if self.get_data==0:return elif self.get_data==1: self.result+="%s -->\n" % data self.get_data=0 elif self.get_data==2: if data<0:1>=='<': self.get_data=0 return self.line+=data
def handle_endtag(self,tag): if self.get_data==2: if tag.lower() == 'p': self.result+="%s%s\n" % (self.tabs*'\t',self.line) self.line="" self.get_data=0;
class clientwindow: def delete_event(self,widget,event,data=None): self.win_show(widget) return True
def destroy(self,widget,data=None): gtk.main_quit()
def click(self,widget,data=None): self.translate = ABBYY(self.word.get_text(),langs<self.sl.get_active>,langs<self.tl.get_active>,self.buffer.set_text)
def key_press_event(self,widget,key): if key.string == "\r": self.click(widget)
def win_show(self,widget,data=None): if self.window.get_property('visible'): self.window.hide() else: self.window.show_all() self.word.set_text("")
def popup(self,widget,button,time,data=None): if button == 3 and data: data.show_all() data.popup(None,None,None,3,time)
def __init__(self): self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("ABBYY Dictionary") self.window.set_position(gtk.WIN_POS_CENTER) self.window.set_resizable(False) self.window.connect("delete_event",self.delete_event)
self.mainbox = gtk.VBox(False,0)
self.hbox = gtk.HBox(False,0)
self.sl = gtk.combo_box_new_text() for i in langs:self.sl.append_text(i) self.sl.set_active(0) self.hbox.pack_start(self.sl,False,False,0) self.label = gtk.Label("-") self.hbox.pack_start(self.label,False,False,0) self.tl = gtk.combo_box_new_text() for i in langs:self.tl.append_text(i) self.tl.set_active(1) self.hbox.pack_start(self.tl,False,False,0)
self.button = gtk.Button("Translate") self.button.connect("clicked",self.click) self.hbox.pack_end(self.button,False,False,0)
self.word = gtk.Entry() self.word.connect("key_press_event",self.key_press_event) self.hbox.pack_end(self.word,False,False,0)
self.label = gtk.Label("Word:") self.hbox.pack_end(self.label,False,False,0)
self.mainbox.pack_start(self.hbox,False,False,0)
self.sw = gtk.ScrolledWindow() self.sw.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC) self.textview = gtk.TextView() self.textview.set_size_request(550,300) self.textview.set_wrap_mode(gtk.WRAP_WORD) self.buffer = self.textview.get_buffer() self.sw.add(self.textview) self.mainbox.pack_start(self.sw,True,True,4) self.window.add(self.mainbox) self.menu = gtk.Menu() self.menuitem = gtk.ImageMenuItem(gtk.STOCK_OPEN) self.menuitem.connect("activate",self.win_show) self.menu.append(self.menuitem) self.menuitem = gtk.ImageMenuItem(gtk.STOCK_QUIT) self.menuitem.connect("activate",self.destroy) self.menu.append(self.menuitem)
self.tray = gtk.StatusIcon() self.tray.set_from_stock(gtk.STOCK_DND) self.tray.connect('activate',self.win_show) self.tray.connect('popup-menu',self.popup,self.menu)
self.d = gtk.gdk.get_default_root_window()
if __name__ == '__main__': a = clientwindow() gtk.main() </self.tl.get_active></self.sl.get_active>
|