comparison babel/messages/frontend.py @ 602:7a8ee579f31e trunk

"update" command now supports "--width" option (#284)
author fschwarz
date Mon, 20 Aug 2012 20:27:35 +0000
parents 3eeaadb290be
children bf5d10a56bbe
comparison
equal deleted inserted replaced
601:3eeaadb290be 602:7a8ee579f31e
499 ('output-file=', 'o', 499 ('output-file=', 'o',
500 "name of the output file (default " 500 "name of the output file (default "
501 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), 501 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"),
502 ('locale=', 'l', 502 ('locale=', 'l',
503 'locale of the catalog to compile'), 503 'locale of the catalog to compile'),
504 ('width=', 'w',
505 'set output line width (default 76)'),
504 ('no-wrap', None, 506 ('no-wrap', None,
505 'do not break long message lines, longer than the output line width, ' 507 'do not break long message lines, longer than the output line width, '
506 'into several lines'), 508 'into several lines'),
507 ('ignore-obsolete=', None, 509 ('ignore-obsolete=', None,
508 'whether to omit obsolete messages from the output'), 510 'whether to omit obsolete messages from the output'),
517 self.domain = 'messages' 519 self.domain = 'messages'
518 self.input_file = None 520 self.input_file = None
519 self.output_dir = None 521 self.output_dir = None
520 self.output_file = None 522 self.output_file = None
521 self.locale = None 523 self.locale = None
524 self.width = None
522 self.no_wrap = False 525 self.no_wrap = False
523 self.ignore_obsolete = False 526 self.ignore_obsolete = False
524 self.no_fuzzy_matching = False 527 self.no_fuzzy_matching = False
525 self.previous = False 528 self.previous = False
526 529
530 if not self.output_file and not self.output_dir: 533 if not self.output_file and not self.output_dir:
531 raise DistutilsOptionError('you must specify the output file or ' 534 raise DistutilsOptionError('you must specify the output file or '
532 'directory') 535 'directory')
533 if self.output_file and not self.locale: 536 if self.output_file and not self.locale:
534 raise DistutilsOptionError('you must specify the locale') 537 raise DistutilsOptionError('you must specify the locale')
538 if self.no_wrap and self.width:
539 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually "
540 "exclusive")
541 if not self.no_wrap and not self.width:
542 self.width = 76
543 elif self.width is not None:
544 self.width = int(self.width)
535 if self.no_fuzzy_matching and self.previous: 545 if self.no_fuzzy_matching and self.previous:
536 self.previous = False 546 self.previous = False
537 547
538 def run(self): 548 def run(self):
539 po_files = [] 549 po_files = []
564 infile.close() 574 infile.close()
565 575
566 if not po_files: 576 if not po_files:
567 raise DistutilsOptionError('no message catalogs found') 577 raise DistutilsOptionError('no message catalogs found')
568 578
569 extra_params = {}
570 if self.no_wrap:
571 extra_params['width'] = None
572
573 for locale, filename in po_files: 579 for locale, filename in po_files:
574 log.info('updating catalog %r based on %r', filename, 580 log.info('updating catalog %r based on %r', filename,
575 self.input_file) 581 self.input_file)
576 infile = open(filename, 'U') 582 infile = open(filename, 'U')
577 try: 583 try:
587 tmpfile = open(tmpname, 'w') 593 tmpfile = open(tmpname, 'w')
588 try: 594 try:
589 try: 595 try:
590 write_po(tmpfile, catalog, 596 write_po(tmpfile, catalog,
591 ignore_obsolete=self.ignore_obsolete, 597 ignore_obsolete=self.ignore_obsolete,
592 include_previous=self.previous, **extra_params) 598 include_previous=self.previous, width=self.width)
593 finally: 599 finally:
594 tmpfile.close() 600 tmpfile.close()
595 except: 601 except:
596 os.remove(tmpname) 602 os.remove(tmpname)
597 raise 603 raise
1038 help="name of the output file (default " 1044 help="name of the output file (default "
1039 "'<output_dir>/<locale>/LC_MESSAGES/" 1045 "'<output_dir>/<locale>/LC_MESSAGES/"
1040 "<domain>.po')") 1046 "<domain>.po')")
1041 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', 1047 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
1042 help='locale of the translations catalog') 1048 help='locale of the translations catalog')
1049 parser.add_option('-w', '--width', dest='width', type='int',
1050 help="set output line width (default 76)")
1043 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true', 1051 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true',
1044 help='do not break long message lines, longer than ' 1052 help='do not break long message lines, longer than '
1045 'the output line width, into several lines') 1053 'the output line width, into several lines')
1046 parser.add_option('--ignore-obsolete', dest='ignore_obsolete', 1054 parser.add_option('--ignore-obsolete', dest='ignore_obsolete',
1047 action='store_true', 1055 action='store_true',
1095 infile.close() 1103 infile.close()
1096 1104
1097 if not po_files: 1105 if not po_files:
1098 parser.error('no message catalogs found') 1106 parser.error('no message catalogs found')
1099 1107
1100 extra_params = {} 1108 if options.width and options.no_wrap:
1101 if options.no_wrap: 1109 parser.error("'--no-wrap' and '--width' are mutually exclusive.")
1102 extra_params['width'] = None 1110 elif not options.width and not options.no_wrap:
1111 options.width = 76
1103 for locale, filename in po_files: 1112 for locale, filename in po_files:
1104 self.log.info('updating catalog %r based on %r', filename, 1113 self.log.info('updating catalog %r based on %r', filename,
1105 options.input_file) 1114 options.input_file)
1106 infile = open(filename, 'U') 1115 infile = open(filename, 'U')
1107 try: 1116 try:
1117 tmpfile = open(tmpname, 'w') 1126 tmpfile = open(tmpname, 'w')
1118 try: 1127 try:
1119 try: 1128 try:
1120 write_po(tmpfile, catalog, 1129 write_po(tmpfile, catalog,
1121 ignore_obsolete=options.ignore_obsolete, 1130 ignore_obsolete=options.ignore_obsolete,
1122 include_previous=options.previous, **extra_params) 1131 include_previous=options.previous,
1132 width=options.width)
1123 finally: 1133 finally:
1124 tmpfile.close() 1134 tmpfile.close()
1125 except: 1135 except:
1126 os.remove(tmpname) 1136 os.remove(tmpname)
1127 raise 1137 raise
Copyright (C) 2012-2017 Edgewall Software