paul j3 added the comment:

One way of marking a string for special wrap handing is to make it an instance 
of a subclass of 'str'.

This patch adds class _WhitespaceStyle(str), and 5 subclasses corresponding to 
the possible values of the CSS whitespace:.

     Normal
     Pre
     NoWrap
     PreLine
     PreWrap 

Together they define methods:

    _str_format() - apply % style formatting
    format() - apply the py3 {} style formatting
    _split_lines() - style specific split_lines (may or may not pass through 
text_wrap)
    _fill_text() - style specific fill_text (again without without test_wrap)

All return a text object of the same class (as self).  This make it possible to 
apply the % formatting to a string, and then apply the wrapping, without 
loosing class information:

    Pre('sample text 
%(default)s')._str_format(dict(default='Boo'))._fill_text(30, '   ')

This subclass information is lost when the string pass through other 'str' 
operations, for example '\n'.join().  I needed to add _str_format because % 
formatting is applied to them before text_wrap.

The HelpFormatter has:

    _str_format() - all the previous % formatting instances
    _split_lines()
    _fill_text()

These delegate the action to the respective white_space classes, or use the 
(default) Normal subclass if the text is a plain 'str'.

test_argparse.py has 2 test cases that use the 'Pre' class to replicate the 
behaviour of the Raw...HelpFormatter class tests.  Undoubtedly it needs further 
tests to handle all of these new classes.

I haven't made any doc changes yet.

I wrote these classes based on the descriptions of what the CSS options do, but 
I have not tried to compare the handling of sample text.  I can also imagine 
users wanting to refine the wrap handling further (e.g. 
http://bugs.python.org/issue12806).

I intend to write test files to show how these new classes could be used in the 
various issues that I listed in the previous post.

------------

Since I had to collect the % formatting cases into one _str_format() method (to 
preserve class information), I am also exploring the use of Py3 {} formatting.  

Py3FormatHelpFormatter - a new Formatter class that redefines _str_format() to 
handle {} style formatting (if present).  I put this in a separate class 
because there is a slight possibility that existing code has text that might be 
confused for Py3 style formatting, e.g.

    help='help text {default: %(default)s}'  

I think the issue of using Py3 formatting was raised and rejected.  So I'm not 
committed to including this feature.

----------
keywords: +patch
Added file: http://bugs.python.org/file36023/issue22029_1.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22029>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to