Changeset 38

Show
Ignore:
Timestamp:
Thu Jun 16 07:59:22 2005
Author:
fredrikc
Message:

Basic tmx import function added to tm.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/transolution/tm/server/estreen.py

    r2 r38  
    4 4 from transolution.tm.engine import index  
    5 5 from transolution.tm.lookup import fuzzyLookup  
    6    
      6 from transolution.path_mod import path  
    6 6 port = 6315  
    7 7  
    8 8 class TMException(Exception):pass  
    9 9  
    10   tmidx = index.VectorIndex()  
    11   tmidx.nodesize = 40  
    12   tmidx.fuzz = 0  
    13    
    14 10 def getPlainText(nodelist):  
    15 11     text_nodes = [node.data for node in nodelist if node.nodeType == node.TEXT_NODE]  
     
    80 76     raise TMException('No good enough match found')  
    81 77  
      78 def parseArgs():  
      79     import sys  
      80     from optparse import OptionParser  
      81  
      82     usage = "usage: %prog [options] [tmxpath]"  
      83  
      84     op = OptionParser(usage=usage)  
      85     op.add_option('-s', '--slang',default = 'en',  
      86                   action='store', type='string', dest='slang',  
      87                   help='TM source language')  
      88     op.add_option('-r', '--recursive',  
      89                   action='store_true', dest='recursive',  
      90                   help='Process files recursive.')  
      91     op.add_option('-f', '--fmask',  
      92                   action='store', type='string', dest='fmask',  
      93                   help='File mask to use when running recursive.')  
      94     op.add_option('-p', '--port',default=port,  
      95                   action='store', type='int', dest='port',  
      96                   help='Port to use.')  
      97  
      98  
      99     options, args = op.parse_args(sys.argv[1:])  
      100     try:  
      101         pname = path(args[0])  
      102     except IndexError,e:  
      103         op.error("Invalid number if arguments, use -h for help")  
      104         options.fnames = []  
      105     else:         
      106         if options.recursive:  
      107             if not options.fmask:  
      108                 op.error("must give filemask option to when recurse")  
      109             else:  
      110                 fnames = pname.walkfiles(options.fmask)  
      111         else:  
      112             if options.fmask:  
      113                 if not pname.isdir():  
      114                     op.error("must give a directory path when option fmask is given")  
      115                 else:  
      116                     fnames = pname.listdir(options.fmask)  
      117             else:  
      118                 fnames = [path(f) for f in args]  
      119  
      120         options.fnames = fnames  
      121     return options  
      122  
      123 from transolution.tmx import getTMXFromFile  
      124 def importTMX(tmx,tmidx):  
      125 ##    import codecs  
      126  
      127     tm = getTMXFromFile(tmx)  
      128     totents = 0  
      129     ents = 0  
      130     for tu in tm.body.tus:  
      131         try:  
      132             key = tu.getKey(slang)  
      133         except IndexError,e:  
      134             continue  
      135         val = ( getSeg(tu,slang), getSeg(tu,tlang) )  
      136         tmidx.addItem(index.IndexItem(key,val))  
      137         ents += 1  
      138         if ents % 100 == 0:  
      139             print ents  
      140     print "Total TU's added:%d"%(ents,)  
      141     del tm  
      142     return tmidx  
      143  
      144 def getSeg(tu,lang):  
      145     for tuv in tu.tuvs:  
      146         if tuv.lang == lang:  
      147             return tuv.seg.node.cloneNode(True)  
    82 148  
    83 149 def main():  
      150     global slang,tlang,tmidx  
      151     options = parseArgs()  
      152     slang = options.slang  
      153     tlang = 'sv-se'  
      154  
      155     tmidx = index.VectorIndex()  
      156     tmidx.nodesize = 40  
      157     tmidx.fuzz = 0  
      158  
      159     for tmxfile in options.fnames:  
      160         print "importing:%s"%(tmxfile,)  
      161         tmidx = importTMX(tmxfile,tmidx)  
      162         print "Done"  
      163  
    84 164     import SimpleXMLRPCServer  
    85 165     tm = TMServer()  
    86       server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", port))  
      166     server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", options.port))  
    86 166     server.register_instance(tm)  
    87 167