Package translate :: Package services :: Module lookupclient
[hide private]
[frames] | no frames]

Source Code for Module translate.services.lookupclient

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  # Copyright 2006 Zuza Software Foundation 
 5  # 
 6  # This file is part of translate. 
 7  # 
 8  # translate is free software; you can redistribute it and/or modify 
 9  # it under the terms of the GNU General Public License as published by 
10  # the Free Software Foundation; either version 2 of the License, or 
11  # (at your option) any later version. 
12  # 
13  # translate is distributed in the hope that it will be useful, 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  # GNU General Public License for more details. 
17  # 
18  # You should have received a copy of the GNU General Public License 
19  # along with translate; if not, write to the Free Software 
20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
21  # 
22   
23  """Small example program for querying an XML-RPC lookup service""" 
24   
25  from translate.storage import tbx 
26  from xml.dom import minidom 
27  import xmlrpclib 
28  import sys 
29   
30  server_url = 'http://localhost:1234/' 
31  server = xmlrpclib.Server(server_url) 
32  UnitClass = tbx.tbxunit 
33   
34  text = sys.stdin.readline() 
35  while text: 
36      text = text.strip().decode("utf-8") 
37      if text != "": 
38          source = server.lookup(text) 
39          if source: 
40              print source 
41              #Lets assume life is simple: 
42              if "<termEntry>" in source: 
43                  #TBX 
44                  base = minidom.parseString(source) 
45                  unit = UnitClass.createfromxmlElement(base.documentElement, None) 
46                  #Do something interesting with unit 
47              elif "<tu><tuv>" in source: 
48                  #TMX 
49                  base = minidom.parseString 
50                  unit = tmx.createfromxmlElement(base.documentElement, None) 
51              target = server.translate(text) 
52              print "%s -> %s".decode('utf-8') % (text, target) 
53          else: 
54              print " (Not found)" 
55          candidates = server.matches(text) 
56          #alternate example, slightly faster: 
57          #candidates = server.matches(text, 5, 70) 
58          if len(candidates): 
59              print "Likely matches:" 
60              columnwidth = min(int(len(text)*1.3)+5, 35) 
61              for score, original, translation in candidates: 
62                  print "%s %-*s | %s".encode('utf-8') % (score, columnwidth, original, translation) 
63          else: 
64              print "No likely matches found" 
65      text = sys.stdin.readline() 
66