Changeset 78

Show
Ignore:
Timestamp:
03/10/04 18:24:31 (4 years ago)
Author:
ug
Message:

Encoding support in process

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 05/devel/kombilo.py

    r72 r78  
    868868            f = open(filename) 
    869869            sgf = f.read() 
    870             if self.DBlist[DBindex]['encoding']: 
     870            c = Cursor(sgf, 1) 
     871            encoding = c.encoding or self.DBlist[DBindex]['encoding'] 
     872            if encoding: 
    871873                try: 
    872                     sgf = sgf.decode(self.DBlist[DBindex]['encoding']).encode('utf-8') 
     874                    sgf = sgf.decode(encoding).encode('utf-8') 
    873875                except: 
    874876                    pass 
    875877             
    876878            f.close() 
    877             node = Cursor(sgf, 1).getRootNode(gameNumber) 
     879            node = c.getRootNode(gameNumber) 
    878880        except: 
    879881            return 
     
    26442646 
    26452647        encoding = self.encodingVar.get() 
     2648         
     2649        encoding1 = 0 
     2650        if self.encoding1Var.get() == 'Do not change SGF': encoding1 = 0 
     2651        elif self.encoding1Var.get() == 'Add CA tag': encoding1 = 1 
     2652        elif self.encoding1Var.get() == 'Transcode SGF to utf-8': encoding1 = 2 
     2653         
    26462654             
    26472655        filelist.sort() 
     
    26712679            try: 
    26722680                file = open(f) 
    2673                 s = file.read() 
     2681                sOrig = file.read() 
    26742682                file.close() 
    26752683            except IOError: 
     
    26772685                continue 
    26782686 
    2679             if encoding: 
    2680                 try: 
    2681                     s = s.decode(encoding).encode('utf-8') 
    2682                 except: 
    2683                     pass 
    26842687            try: 
    2685                 cursor = Cursor(s, self.options.sloppySGF.get()) 
     2688                cursor = Cursor(sOrig, self.options.sloppySGF.get()) 
    26862689            except: 
    26872690                messages.insert(END, 'Error in SGF file '+f+': Skipped.\n') 
    26882691                continue 
    2689  
     2692             
     2693            if encoding1 == 1 and not cursor.encoding: 
     2694                d = cursor.getRootNode(0) 
     2695                d['CA'] = [encoding] 
     2696                cursor.encoding = encoding 
     2697                cursor.updateRootNode(d,0) 
     2698                try: 
     2699                    sgf_out = cursor.output() 
     2700                    file = open(f, 'w') 
     2701                    file.write(sgf_out) 
     2702                    file.close() 
     2703                except: 
     2704                    messages.insert(END, 'Unable to write CA-tagged file ' + f + '\n') 
     2705            currentEncoding = cursor.encoding or encoding 
     2706            if currentEncoding != 'utf-8': 
     2707                try: 
     2708                    s = sOrig.decode(currentEncoding).encode('utf-8') 
     2709                    cursor = Cursor(s, self.options.sloppySGF.get()) 
     2710                    if encoding1 == 2 and not cursor.encoding: 
     2711                        file = open(f, 'w') 
     2712                        file.write(s) 
     2713                        file.close() 
     2714                except IOError: 
     2715                    messages.insert(END, 'Unable to write transcoded file ' + f + '\n') 
     2716                except: 
     2717                    messages.insert(END, 'Unable to transcode file ' + f + '\n') 
     2718                     
    26902719            if cursor.root.numChildren > 1: 
    26912720                collection = 1 
     
    56305659        encodingMenu.grid(row=1, column=1, sticky=W) 
    56315660 
     5661        self.encoding1Var = StringVar() 
     5662        encoding1Menu = Pmw.OptionMenu(f3, labelpos='w', label_text='', 
     5663                                       menubutton_textvariable = self.encoding1Var, 
     5664                                       items = ['Do not change SGF', 'Add CA tag', 'Transcode SGF to utf-8'], 
     5665                                       initialitem = 0) 
     5666        encoding1Menu.grid(row=1, column=2, sticky=W) 
     5667 
    56325668        recursionButton = Checkbutton(f3, text = "Recursively add subdir's", highlightthickness=0, 
    56335669                                      variable = self.options.recProcess)