Changeset 5704

Show
Ignore:
Timestamp:
11/17/08 17:57:53 (2 months ago)
Author:
faide
Message:

Now support extraction from html (genshi) files. We are still missing translation context (see TODO)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/toolbox/admi18n/__init__.py

    r5167 r5704  
    1616import turbogears 
    1717from turbogears import controllers, expose, i18n 
     18 
     19# this is a list of supported file extensions. 
     20supported_exts = ['.py', '.kid', '.tmpl', '.html'] 
    1821 
    1922 
     
    249252                    namelist.remove(name) 
    250253                    continue 
     254 
    251255                p = os.path.join(dirpath, name) 
    252256                if os.path.isfile(p) and \ 
    253                         os.path.splitext(name)[-1] in ['.py','.kid','.tmpl']
     257                        os.path.splitext(name)[-1] in supported_exts
    254258                    slot2.append(dict(dir=dirpath, file_name=name, 
    255259                        path=p, isdir=os.path.isdir(p), level=level+1)) 
     260 
    256261            # decide if current directory (and ancestors) should be visible 
    257262            visibility[dirpath] = bool(slot2) 
  • branches/1.1/turbogears/toolbox/admi18n/pygettext.py

    r5678 r5704  
    171171except ImportError: 
    172172    kid_parser = None 
     173 
     174try: 
     175    from genshi import XML as genshi_parser 
     176    import genshi.core 
     177except: 
     178    genshi_parser = None 
    173179 
    174180__version__ = '1.5' 
     
    452458            self.__state = self.__waiting 
    453459 
    454     def __addentry(self, msg, lineno=None, isdocstring=0, iskidstring=0): 
    455         # tokenize module always return unicode strings 
    456         # even when they are in fact coded string instances 
    457         # to deal with this we use a hack: 
    458         # evaluate string's representation without leading "u" 
    459         # to force interpration as coded string 
    460         # then we decode it using already known file's encoding 
    461         if not iskidstring: 
     460    def __addentry(self, msg, lineno=None, isdocstring=0, istemplatestring=0): 
     461        """tokenize module always return unicode strings 
     462        even when they are in fact coded string instances 
     463        to deal with this we use a hack: 
     464        evaluate string's representation without leading "u" 
     465        to force interpration as coded string 
     466        then we decode it using already known file's encoding 
     467        """ 
     468        if not istemplatestring: 
    462469            if type(msg) is str: 
    463470                msg = eval(repr(msg)) 
     471 
    464472            else: 
    465473                msg = eval(repr(msg)[1:]) 
     474 
    466475            msg = msg.decode(self.__encoding) 
     476 
    467477        if lineno is None: 
    468478            lineno = self.__lineno 
     479 
    469480        if not msg in self.__options.toexclude: 
    470481            entry = (self.__curfile, lineno) 
     
    476487 
    477488    def set_file_encoding(self, fp): 
    478         """Search for -*- coding: -*- magic comment to find out file encoding.""" 
     489        """Search for -*- coding: -*- magic comment to find out file encoding""" 
    479490        self.__encoding = 'utf-8' # reset to default for each new file 
    480491        for line in fp.readlines()[:5]: 
     
    490501    def __strip_namespace_uri(self, tag): 
    491502        return tag.split('}', 1)[-1] 
     503 
     504    def extract_genshi_strings(self): 
     505        """a simple genshi extractor that will just get all the text inside a 
     506        genshi template as long as there is no python code inside it... 
     507        The lien numbers are not reported (nor the tags) and this will just 
     508        show you some nice -1 for the translation context at the moment... 
     509        """ 
     510        if not self.__curfile: 
     511            return 
     512 
     513        if not genshi_parser: 
     514            raise ImportError, "Genshi templating is not installed." 
     515 
     516        for kind, data, pos in genshi_parser(open(self.__curfile, 'r').read()): 
     517            if kind == genshi.core.TEXT: 
     518                item = data.strip() 
     519                if item and not self.__contains_inline_python(item): 
     520                    # TODO: add the context in second arg position by adding  
     521                    # the full XPATH of the TAG, not by just copy pasting the 
     522                    # kid code below 
     523                    self.__addentry(item, istemplatestring=1) 
    492524 
    493525    def extract_kid_strings(self): 
     
    503535                    item = item.strip() 
    504536                    if item  and not self.__contains_inline_python(item): 
    505                         self.__addentry(item, tag, iskidstring=1) 
     537                        self.__addentry(item, tag, istemplatestring=1) 
     538 
    506539            elif ev == kid_parser.START: 
    507540                tag = item.tag 
     
    705738            fp = sys.stdin 
    706739            closep = 0 
     740 
    707741        else: 
    708742            if options.verbose: 
     
    711745            eater.set_file_encoding(fp) 
    712746            closep = 1 
     747 
    713748        try: 
    714749            eater.set_filename(filename) 
     
    717752                    eater.extract_kid_strings() 
    718753                except Exception, e: 
    719                     print >> sys.stderr, e 
     754                    print >> sys.stderr, "Kid eater exception:", e 
     755 
     756            elif os.path.splitext(filename)[-1].lower() == '.html': 
     757                try: 
     758                    eater.extract_genshi_strings() 
     759                except Exception, e: 
     760                    print >> sys.stderr, "Genshi eater exception:", e 
     761 
    720762            else: 
    721763                try: 
     
    724766                    print >> sys.stderr, '%s: %s, line %d, column %d' % ( 
    725767                        e[0], filename, e[1][0], e[1][1]) 
     768 
    726769        finally: 
    727770            if closep: 
     
    732775        fp = sys.stdout 
    733776        closep = 0 
     777 
    734778    else: 
    735779        if options.outpath: 
     
    737781        fp = open(options.outfile, 'wt') 
    738782        closep = 1 
     783 
    739784    try: 
    740785        eater.write(fp) 
     786 
    741787    finally: 
    742788        if closep: