lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new check-translation script - enjoy!


From: Marc Weber
Subject: new check-translation script - enjoy!
Date: Fri, 16 Sep 2005 22:23:07 +0200
User-agent: Mutt/1.5.8i

Hi. I was tired of looking for the right files so I've written this
python script.. Just copy the text (I hope it doesn't get messed up
because of indentation.) Put it into the scripts directory and enjoy.


-- givemenextfiletoprocess.py ---

# This python script should it make a little bit more easy to maintain/create 
the translation.

# features: 
# it does open the editor (gvim) for you (see def translatefile and def 
updatefile. there you can change to another one...)
# You have to feed the script with all files which you want to check/ 
translate. It should look like this:
# python scripts/givemenextfiletoprocess.py --lang='de' $(find site -name 
'*.html')
# It will then sort the files to process those listed in TRANSLATION with 
highest priority first.
# now take the example site/index.html.
# the script will test wether <lang>/index.html already exists
#   no -> remove $$ from the version number, copy it and launch vim with 
original (site/index.html) and the translation file (<lang>/index.html)
#         it will also add the text 'unfinished' after the version.
#  yes -> 
#     contains word unfinished -> launch gvim like above
#     else run cvs (like check-translation does) and print diff and launch 
files.
#       I would like to vimdiff the translation file, file from which has been 
translated and current original file, but this is not implemented, yet
#       if you can't finish: no prob. add unfinished somewhere and this file 
will be opened the next time.. 
#       I think it would also nice open files containing todos.. ;) shouldn't 
be hard to add
# 
# to call the script easily I've added some lines to GNUmakefile (marked by '+')

#    HTML = $(shell find $(SITE) -name '*.html')
#  + HTMLFILESTOTRANSLATE= $(shell find site -name '*.html')

#   check-translation:
#       python $(SCRIPTDIR)/check-translation.py $(HTML)
# 
#  + give-me-next-file-to-process:
#  +    python $(SCRIPTDIR)/givemenextfiletoprocess.py --language=$(LANG) 
$(HTMLFILESTOTRANSLATE)

# and remember, there are also some .ihtml files.. ;)

import __main__
import getopt
import gettext
import os
import os.path
from os.path import exists
import re
import string
import sys
import shutil


verbose=0

# The directory to hold the translated and menuified tree.
C = 'site'
lang = C
LANGUAGES = (
        (C, 'English'),
        ('nl', 'Nederlands'),
        ('de', 'German'),
        )

def dir_lang (file, lang):
        return string.join ([lang] + string.split (file, '/'), '/')

REVISION_RE = re.compile ('.*CVS Revision: ([0-9]+[.][0-9]+)', re.DOTALL)
CVS_DIFF = 'cvs diff -kk -u -r%(revision)s -rHEAD %(original)s'


def translatefile(original,translated):
  if not exists(translated):
    instring=open(original).read()
    # remove $$ of revisions:
    reg=re.compile('\$(?P<revision>.{}?)\$')
    outfile=file(translated,'w')
    outfile.write(reg.sub('\g<revision> unfinished',instring,1))
    outfile.close()
  # call editor (vim):
  print 'starting gvim with files %(original)s %(translated)s' % vars()
  os.system('gvim --nofork -c ":imap <M-p> LilyPond" -c "set scrollbind" -c 
"vsplit" -c "bn" -c  "set scrollbind" %(original)s %(translated)s' % vars())


def updatefile(original, translated,diff):
  #  I would like to have a vimdiff between 
  #  the file I've made the translation from
  #  the up to date original file
  #  my translation
  # but that's too much work for now, simply print the diff and call gvim

  print diff
  translatefile(original, translated)


  #suffx='old'
  #translationMadeFrom=translated+suffix
  #copy (original, translationMadeFrom )
  # now patch .. hopefully original was up to date !
  #diffmodified=replace(diff,origin
  


# check_file opens editor to translate or to adjust translated file 
# will open file for translation if no translation file already exists
# will open  file for adjustment if revision number has changed (after cvs 
update)
def check_file (original, translated):
          if not exists(translated):
            translatefile(original,translated)
          else: 
            #check translation
            print "checking wether file %(translated)s is still up to date via 
cvs" % vars()
            s = open (translated).read ()
            if string.find(s,'unfinished')>=0:
              translatefile(original,translated)
            else:
              m = REVISION_RE.match (s)
              if not m:
                      raise translated + ': no Revision: x.yy found'
              revision = m.group (1)

              c = CVS_DIFF % vars ()
              if verbose:
                      os.stderr.write ('running: ' + c)
              process=os.popen(c)
              diff=process.read()
              if len(diff)==0:
                if verbose: print 'translated file %s is up to date' % 
translated
              else:
                updatefile(original,translated,diff)

def do_file (file_name):
        if verbose:
                sys.stderr.write ('%s...\n' % file_name)
        original = dir_lang (file_name, C)
        translated = dir_lang (file_name, lang)
        print original,translated, lang
        check_file (original, translated)

# the following lines are copied from check-translation:
def usage ():
        sys.stdout.write (r'''
Usage:
givemenextfiletoprocess.py [--language=LANG] [--verbose] FILE...

This script is licensed under the GNU GPL.
''')

def do_options ():
      global lang, verbose
      (options, files) = getopt.getopt (sys.argv[1:], '',
                                        ['help', 'language=', 'verbose'])
      for (o, a) in options:
              if 0:
                      pass
              elif o == '--help':
                      usage ()
              elif o == '--language':
                      lang = a
              elif o == '--verbose':
                      verbose = 1
              else:
                      assert unimplemented
      return  files

def sortfiles (files):
  # read the files from Translation:

      s=open('TRANSLATION').read()
      l=list()
      for match in re.finditer('(?P<priority>\\d) (?P<file>.{}?)  -- ',s):
            l.append((int(match.group('priority')),match.group('file')))
      def comp(x, y):
        return x[0]-y[0]
      # list seems to be sorted by default
      #l.sort(comp)
      l=[x[1] for x in l] # <- getting file out of tuple
      verbose=1
      if verbose: print 'sorted list from TRANSLATION is: \n',l,'\n'

      def comp2(f1, f2):
        try: ind1=l.index(f1) 
        except: ind1=-1
        try: ind2=l.index(f2) 
        except: ind2=-1
        if (ind1>=0) and (ind2>=0):
          return ind1-ind2
        else:
          return ind2-ind1
      print files
      files.sort(comp2)
      print files
      return files
    
def main ():
      files = do_options ()
      #remove site/ from the beginning and pass files:
      files = sortfiles([file[len('site/'):] for file in files])

      for i in files:
            do_file (i)

if __name__ == '__main__':
      main ()





reply via email to

[Prev in Thread] Current Thread [Next in Thread]