Changeset 5704
- Timestamp:
- 11/17/08 17:57:53 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/turbogears/toolbox/admi18n/__init__.py
r5167 r5704 16 16 import turbogears 17 17 from turbogears import controllers, expose, i18n 18 19 # this is a list of supported file extensions. 20 supported_exts = ['.py', '.kid', '.tmpl', '.html'] 18 21 19 22 … … 249 252 namelist.remove(name) 250 253 continue 254 251 255 p = os.path.join(dirpath, name) 252 256 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: 254 258 slot2.append(dict(dir=dirpath, file_name=name, 255 259 path=p, isdir=os.path.isdir(p), level=level+1)) 260 256 261 # decide if current directory (and ancestors) should be visible 257 262 visibility[dirpath] = bool(slot2) branches/1.1/turbogears/toolbox/admi18n/pygettext.py
r5678 r5704 171 171 except ImportError: 172 172 kid_parser = None 173 174 try: 175 from genshi import XML as genshi_parser 176 import genshi.core 177 except: 178 genshi_parser = None 173 179 174 180 __version__ = '1.5' … … 452 458 self.__state = self.__waiting 453 459 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: 462 469 if type(msg) is str: 463 470 msg = eval(repr(msg)) 471 464 472 else: 465 473 msg = eval(repr(msg)[1:]) 474 466 475 msg = msg.decode(self.__encoding) 476 467 477 if lineno is None: 468 478 lineno = self.__lineno 479 469 480 if not msg in self.__options.toexclude: 470 481 entry = (self.__curfile, lineno) … … 476 487 477 488 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""" 479 490 self.__encoding = 'utf-8' # reset to default for each new file 480 491 for line in fp.readlines()[:5]: … … 490 501 def __strip_namespace_uri(self, tag): 491 502 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) 492 524 493 525 def extract_kid_strings(self): … … 503 535 item = item.strip() 504 536 if item and not self.__contains_inline_python(item): 505 self.__addentry(item, tag, iskidstring=1) 537 self.__addentry(item, tag, istemplatestring=1) 538 506 539 elif ev == kid_parser.START: 507 540 tag = item.tag … … 705 738 fp = sys.stdin 706 739 closep = 0 740 707 741 else: 708 742 if options.verbose: … … 711 745 eater.set_file_encoding(fp) 712 746 closep = 1 747 713 748 try: 714 749 eater.set_filename(filename) … … 717 752 eater.extract_kid_strings() 718 753 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 720 762 else: 721 763 try: … … 724 766 print >> sys.stderr, '%s: %s, line %d, column %d' % ( 725 767 e[0], filename, e[1][0], e[1][1]) 768 726 769 finally: 727 770 if closep: … … 732 775 fp = sys.stdout 733 776 closep = 0 777 734 778 else: 735 779 if options.outpath: … … 737 781 fp = open(options.outfile, 'wt') 738 782 closep = 1 783 739 784 try: 740 785 eater.write(fp) 786 741 787 finally: 742 788 if closep: