changeset 335:7620142aacd1

treesum: prepare for a grouping-separator (aka thousands separator) for sizes. BUGS: Not yet implemented but command line options prepared.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 31 Mar 2025 09:02:43 +0200
parents 5afece258bf2
children 0049f486e1cd
files cutils/treesum.py
diffstat 1 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Sun Mar 30 16:20:04 2025 +0200
+++ b/cutils/treesum.py	Mon Mar 31 09:02:43 2025 +0200
@@ -106,6 +106,16 @@
 `normal' prints just whether Python 2 or Python 3 is used, and `none'
 suppresses the output completely. The default is `normal'.""")
         gp.add_argument(
+            "--grouping-separator", action="store", dest="grouping_separator",
+            metavar="GROUPING-SEPARATOR",
+            help="""
+Use the given GROUPING-SEPARATOR as thousands separator.
+Use an empty GROUPING-SEPARATOR to disable grouping.
+The effective default depends on the --output-style: for tagged output
+it is the underscore `_', for tabbed output it is the dot `.'.
+
+""")
+        gp.add_argument(
             "--half", "-H", action=SymlinkAction, dest="follow_symlinks",
             const=FollowSymlinkConfig(True, False, False),
             help="""Follow symbolic links given on the command line but do
@@ -202,6 +212,12 @@
             help="""Print only the size of files and for each directory its
 accumulated directory size. Digests are not computed.""")
         gp.add_argument(
+            "--size-width", action="store", type=int, metavar="SIZE-WIDTH",
+            dest="size_column_width", default=15,
+            help="""Some output styles print the a filesize right-aligned
+in a column. SIZE-WIDTH is the (minimum) width to be used. The width includes
+grouping separators. Use 0 if no alignment should be done. Default is 15.""")
+        gp.add_argument(
             "--utf8", "--utf-8", action="store_true",
             help="""Encode all file paths using UTF-8 instead of
 the filesystem encoding. Add some error tag into the path if it cannot
@@ -431,6 +447,7 @@
                       follow_symlinks=FollowSymlinkConfig(False, False, False),
                       full_mode=False,
                       generator="normal",
+                      grouping_separator=None,   # the output writer selects
                       logical=None,
                       minimal=None,
                       mode=False,
@@ -440,6 +457,7 @@
                       output_style="tagged",
                       print_size=False,
                       size_only=False,
+                      size_column_width=15,
                       utf8=False):
     if not isinstance(follow_symlinks, FollowSymlinkConfig):
         raise TypeError("`follow_symlinks' must be a FollowSymlinkConfig")
@@ -476,6 +494,7 @@
         fnmatch_filters=fnmatch_filters,
         follow_symlinks=follow_symlinks,
         generator=generator,
+        grouping_separator=grouping_separator,
         logical=logical,
         minimal=minimal,
         mmap=mmap,
@@ -486,6 +505,7 @@
         output_style=output_style,
         print_size=print_size,
         size_only=size_only,
+        size_column_width=size_column_width,
         utf8=utf8)
     return opts
 
@@ -539,7 +559,10 @@
         raise NotImplementedError("`output_style'")
 
     with out_cm as outfp:
-        writer = writerstyle(outfp, use_base64=opts.base64)
+        writer = writerstyle(outfp,
+                             use_base64=opts.base64,
+                             grouping_separator=opts.grouping_separator
+                             )
         for d in opts.directories:
             V1DirectoryTreesumGenerator(
                 opts.algorithm, opts.mmap,
@@ -1293,9 +1316,17 @@
     """Because we write the output as binary files we need the official line
     separator for you OS as bytes"""
 
-    def __init__(self, outfp, use_base64=False):
+    DEFAULT_GROUPING_SEPARATOR = ""
+    """Disable the thousands separator in case no subclass redefines it"""
+
+    def __init__(self, outfp, use_base64=False, grouping_separator=None,
+                 size_column_width=None):
         self._outfp = outfp
         self.use_base64 = use_base64
+        self.grouping_separator = (grouping_separator
+                                   if grouping_separator is not None
+                                   else self.DEFAULT_GROUPING_SEPARATOR)
+        self.size_column_width = size_column_width or 0
         self.reset_crc()
 
     @property
@@ -1338,7 +1369,12 @@
 
     """
 
+    DEFAULT_GROUPING_SEPARATOR = '_'
+    """The default thousands separator"""
+
     def __init__(self, outfp, **kwds):
+        # No alignment for the size here
+        kwds["size_column_width"] = 0
         super(TaggedTreesumWriter, self).__init__(outfp, **kwds)
 
     def start(self, version):
@@ -1452,6 +1488,9 @@
 
     """
 
+    DEFAULT_GROUPING_SEPARATOR = '.'
+    """The default thousands separator"""
+
     def __init__(self, outfp, **kwds):
         super(TabularTreesumWriter, self).__init__(outfp, **kwds)