1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  """Server program to serve messages over XML-RPC 
 24   
 25  As this is implemented using the base classes (see storage.base), the 
 26  work is minimal to use this with any storage format that is implemented 
 27  using the base classes. Strictly speaking, only an init... function needs 
 28  to be registered.""" 
 29   
 30  from translate.convert import convert 
 31  from translate.storage import tbx 
 32  from translate.storage import tmx 
 33  from translate.storage import po 
 34  from translate.storage import csvl10n 
 35  from translate.search import match 
 36  from translate.misc.multistring import multistring 
 37   
 38  from SimpleXMLRPCServer import SimpleXMLRPCServer 
 39  from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler 
 40   
 42      """Sets up the requested file for parsing""" 
  43       
 44       
 45       
 55   
 57          try: 
 58               
 59               
 60              func = getattr(self, 'public_' + method) 
 61          except AttributeError: 
 62              raise Exception('no method called "%s"' % method) 
 63          else: 
 64              try: 
 65                  return func(*params) 
 66              except Exception, e: 
 67                  print str(e) 
 68                  return "" 
  69       
 71          """Could perhaps include some intelligence in future, like case trying with different casing, etc.""" 
 72          message = message.strip() 
 73          if message == "": 
 74              return None 
 75          if not isinstance(message, unicode): 
 76              message = unicode(message) 
 77          try: 
 78              unit = self.storage.findunit(message) 
 79          except Exception: 
 80              return None 
 81          return unit 
  82       
 84          """Returns the source string of whatever was found. Keep in mind that this might not be what you want.""" 
 85          unit = self.internal_lookup(message) 
 86          if unit: 
 87              return str(unit) 
 88          else: 
 89              return "" 
  90   
 92          """Translates the message from the storage and returns a plain string""" 
 93          unit = self.internal_lookup(message) 
 94          if unit and unit.target: 
 95              return unit.target 
 96          else: 
 97              return "" 
  98   
 99 -    def public_matches(self, message, max_candidates=15, min_similarity=50): 
 100          """Returns matches from the storage with the associated similarity""" 
101          self.matcher.setparameters(max_candidates=max_candidates, min_similarity=min_similarity) 
102          if not isinstance(message, unicode): 
103              message = unicode(message) 
104          candidates = self.matcher.matches(message) 
105          clean_candidates = [] 
106          for unit in candidates: 
107              score = unit.getnotes() 
108              original = unit.source 
109              translation = unit.target 
110               
111               
112              if isinstance(original, multistring): 
113                  original = unicode(original) 
114              if isinstance(translation, multistring): 
115                  translation = unicode(translation) 
116              clean_candidates += [(score, original, translation)] 
117          return clean_candidates 
 118   
120      """Parser that calls instantiates the lookupServer""" 
 134   
135 -def inittbx(inputfile, columnorder=None): 
 137   
138 -def inittmx(inputfile, columnorder=None): 
 140   
141 -def initpo(inputfile, columnorder=None): 
 143   
144 -def initcsv(inputfile, columnorder=None): 
 146   
148      formats = {"tbx": (None, inittbx), "tmx": (None, inittmx), "po": (None, initpo), "csv": (None, initcsv)} 
149      parser = lookupOptionParser(formats, usepots=False, description=__doc__) 
150      parser.add_option("-a", "--address", dest="address", default="localhost", 
151                        help="the host to bind to") 
152      parser.add_option("-p", "--port", dest="port", default=1234, 
153                        help="the port to listen on") 
154      parser.add_option("-l", "--language", dest="targetlanguage", default=None,  
155                        help="set target language code", metavar="LANG") 
156      parser.add_option("", "--source-language", dest="sourcelanguage", default='en',  
157                        help="set source language code", metavar="LANG") 
158      parser.remove_option("--output") 
159      parser.remove_option("--exclude") 
160      parser.passthrough.append("sourcelanguage") 
161      parser.passthrough.append("targetlanguage") 
162      parser.add_option("", "--columnorder", dest="columnorder", default=None, 
163                        help="specify the order and position of columns for CSV (comment,source,target)") 
164      parser.passthrough.append("columnorder") 
165      parser.run() 
 166   
167  if __name__ == '__main__': 
168      main() 
169