('no-wrap', None,
'do not break long message lines, longer than the output line width, '
'into several lines'),
+ ('sort-output', None,
+ 'generate sorted output (default False)'),
+ ('sort-by-file', None,
+ 'sort output by file location (default False)'),
('input-dirs=', None,
'directories that should be scanned for messages'),
]
boolean_options = [
- 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap'
+ 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap',
+ 'sort-output', 'sort-by-file'
]
def initialize_options(self):
self.input_dirs = None
self.width = 76
self.no_wrap = False
+ self.sort_output = False
+ self.sort_by_file = False
def finalize_options(self):
if self.no_default_keywords and not self.keywords:
self.keywords = self._keywords
if self.no_wrap and self.width:
- raise DistutilsOptionError("'--no-wrap' and '--width' are mutually"
+ raise DistutilsOptionError("'--no-wrap' and '--width' are mutually "
"exclusive")
if self.no_wrap:
self.width = None
else:
self.width = int(self.width)
+
+ if self.sort_output and self.sort_by_file:
+ raise DistutilsOptionError("'--sort-output' and '--sort-by-file' "
+ "are mutually exclusive")
if not self.input_dirs:
self.input_dirs = dict.fromkeys([k.split('.',1)[0]
write_pot(outfile, catalog, project=self.distribution.get_name(),
version=self.distribution.get_version(), width=self.width,
charset=self.charset, no_location=self.no_location,
- omit_header=self.omit_header)
+ omit_header=self.omit_header, sort_output=self.sort_output,
+ sort_by_file=self.sort_by_file)
finally:
outfile.close()
parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true',
help='do not break long message lines, longer than '
'the output line width, into several lines')
+ parser.add_option('--sort-output', dest='sort_output',
+ action='store_true',
+ help='generate sorted output (default False)')
+ parser.add_option('--sort-by-file', dest='sort_by_file',
+ action='store_true',
+ help='sort output by file location (default False)')
parser.set_defaults(charset='utf-8', keywords=[],
no_default_keywords=False, no_location=False,
- omit_header = False, width=76, no_wrap=False)
+ omit_header = False, width=76, no_wrap=False,
+ sort_output=False, sort_by_file=False)
options, args = parser.parse_args(argv)
if not args:
parser.error('incorrect number of arguments')
options.width = 76
elif not options.width and options.no_wrap:
options.width = 0
+
+ if options.sort_output and options.sort_by_file:
+ parser.error("'--sort-output' and '--sort-by-file' are mutually "
+ "exclusive")
try:
catalog = Catalog()
write_pot(outfile, catalog, width=options.width,
charset=options.charset, no_location=options.no_location,
- omit_header=options.omit_header)
+ omit_header=options.omit_header,
+ sort_output=options.sort_output,
+ sort_by_file=options.sort_by_file)
finally:
if options.output:
outfile.close()
return u'""\n' + u'\n'.join([escape(l) for l in lines])
def write_pot(fileobj, catalog, project='PROJECT', version='VERSION', width=76,
- charset='utf-8', no_location=False, omit_header=False):
+ charset='utf-8', no_location=False, omit_header=False,
+ sort_output=False, sort_by_file=False):
r"""Write a ``gettext`` PO (portable object) template file for a given
message catalog to the provided file-like object.
catalog.project = project
catalog.version = version
catalog.charset = charset
+
+ if sort_output:
+ messages = list(catalog)
+ messages.sort(lambda x,y: cmp(x.id, y.id))
+ elif sort_by_file:
+ messages = list(catalog)
+ messages.sort(lambda x,y: cmp(x.locations, y.locations))
+ else:
+ messages = catalog
- for message in catalog:
+ for message in messages:
if not message.id: # This is the header "message"
if omit_header:
continue