@@ -33,9 +33,6 @@ def _is_file(f):
3333# minimum extra space in headers
3434MIN_PADDING = 2
3535
36- # Whether or not to preserve leading/trailing whitespace in data.
37- PRESERVE_WHITESPACE = False
38-
3936_DEFAULT_FLOATFMT = "g"
4037_DEFAULT_INTFMT = ""
4138_DEFAULT_MISSINGVAL = ""
@@ -163,7 +160,6 @@ def _grid_line_with_colons(colwidths, colaligns):
163160 return "+" + "+" .join (segments ) + "+"
164161
165162
166-
167163def _mediawiki_row_with_attrs (separator , cell_values , colwidths , colaligns ):
168164 alignment = {
169165 "left" : "" ,
@@ -1100,13 +1096,13 @@ def _choose_width_fn(has_invisible, enable_widechars, is_multiline):
11001096 return width_fn
11011097
11021098
1103- def _align_column_choose_padfn (strings , alignment , has_invisible ):
1099+ def _align_column_choose_padfn (strings , alignment , has_invisible , preserve_whitespace ):
11041100 if alignment == "right" :
1105- if not PRESERVE_WHITESPACE :
1101+ if not preserve_whitespace :
11061102 strings = [s .strip () for s in strings ]
11071103 padfn = _padleft
11081104 elif alignment == "center" :
1109- if not PRESERVE_WHITESPACE :
1105+ if not preserve_whitespace :
11101106 strings = [s .strip () for s in strings ]
11111107 padfn = _padboth
11121108 elif alignment == "decimal" :
@@ -1120,7 +1116,7 @@ def _align_column_choose_padfn(strings, alignment, has_invisible):
11201116 elif not alignment :
11211117 padfn = _padnone
11221118 else :
1123- if not PRESERVE_WHITESPACE :
1119+ if not preserve_whitespace :
11241120 strings = [s .strip () for s in strings ]
11251121 padfn = _padright
11261122 return strings , padfn
@@ -1163,9 +1159,12 @@ def _align_column(
11631159 has_invisible = True ,
11641160 enable_widechars = False ,
11651161 is_multiline = False ,
1162+ preserve_whitespace = False ,
11661163):
11671164 """[string] -> [padded_string]"""
1168- strings , padfn = _align_column_choose_padfn (strings , alignment , has_invisible )
1165+ strings , padfn = _align_column_choose_padfn (
1166+ strings , alignment , has_invisible , preserve_whitespace
1167+ )
11691168 width_fn = _align_column_choose_width_fn (
11701169 has_invisible , enable_widechars , is_multiline
11711170 )
@@ -1271,15 +1270,21 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
12711270 return f"{ val } "
12721271 elif valtype is int :
12731272 if isinstance (val , str ):
1274- val_striped = val .encode ('unicode_escape' ).decode ('utf-8' )
1275- colored = re .search (r'(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$' , val_striped )
1273+ val_striped = val .encode ("unicode_escape" ).decode ("utf-8" )
1274+ colored = re .search (
1275+ r"(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$" , val_striped
1276+ )
12761277 if colored :
12771278 total_groups = len (colored .groups ())
12781279 if total_groups == 3 :
12791280 digits = colored .group (2 )
12801281 if digits .isdigit ():
1281- val_new = colored .group (1 ) + format (int (digits ), intfmt ) + colored .group (3 )
1282- val = val_new .encode ('utf-8' ).decode ('unicode_escape' )
1282+ val_new = (
1283+ colored .group (1 )
1284+ + format (int (digits ), intfmt )
1285+ + colored .group (3 )
1286+ )
1287+ val = val_new .encode ("utf-8" ).decode ("unicode_escape" )
12831288 intfmt = ""
12841289 return format (val , intfmt )
12851290 elif valtype is bytes :
@@ -1645,6 +1650,7 @@ def tabulate(
16451650 disable_numparse = False ,
16461651 colglobalalign = None ,
16471652 colalign = None ,
1653+ preserve_whitespace = False ,
16481654 maxcolwidths = None ,
16491655 headersglobalalign = None ,
16501656 headersalign = None ,
@@ -2323,7 +2329,15 @@ def tabulate(
23232329 if tablefmt == "colon_grid" :
23242330 aligns_copy = ["left" ] * len (cols )
23252331 cols = [
2326- _align_column (c , a , minw , has_invisible , enable_widechars , is_multiline )
2332+ _align_column (
2333+ c ,
2334+ a ,
2335+ minw ,
2336+ has_invisible ,
2337+ enable_widechars ,
2338+ is_multiline ,
2339+ preserve_whitespace ,
2340+ )
23272341 for c , a , minw in zip (cols , aligns_copy , minwidths )
23282342 ]
23292343
@@ -2820,7 +2834,16 @@ def _main():
28202834 opts , args = getopt .getopt (
28212835 sys .argv [1 :],
28222836 "h1o:s:F:I:f:" ,
2823- ["help" , "header" , "output=" , "sep=" , "float=" , "int=" , "colalign=" , "format=" ],
2837+ [
2838+ "help" ,
2839+ "header" ,
2840+ "output=" ,
2841+ "sep=" ,
2842+ "float=" ,
2843+ "int=" ,
2844+ "colalign=" ,
2845+ "format=" ,
2846+ ],
28242847 )
28252848 except getopt .GetoptError as e :
28262849 print (e )
0 commit comments