Prechádzať zdrojové kódy

get rid of print_() function

this function was over-coupling the cache system and the statistics
module. they are now almost decoupled insofar as the cache system has
its own rendering system now that is called separately.

furthermore, we have a much more flexible formatting system that is
used coherently between --progress and --stats

the degenerate case here is if we want to change the label in the
statistics summary: in this case we need to override the default
__str__() representation to insert our own label.
Antoine Beaupré 9 rokov pred
rodič
commit
66bfc6fce8
3 zmenil súbory, kde vykonal 15 pridanie a 14 odobranie
  1. 8 4
      borg/archiver.py
  2. 5 9
      borg/helpers.py
  3. 2 1
      borg/testsuite/helpers.py

+ 8 - 4
borg/archiver.py

@@ -167,7 +167,8 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
                 archive.end = datetime.now()
                 print('-' * 78)
                 print(str(archive))
-                print(archive.stats.print_('This archive:', cache))
+                print(str(archive.stats))
+                print(str(cache))
                 print('-' * 78)
         return self.exit_code
 
@@ -313,7 +314,8 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
             repository.commit()
             cache.commit()
             if args.stats:
-                logger.info(stats.print_('Deleted data:', cache))
+                logger.info(stats.summary.format(label='Deleted data:', stats=stats))
+                logger.info(str(cache))
         else:
             if not args.cache_only:
                 print("You requested to completely DELETE the repository *including* all archives it contains:", file=sys.stderr)
@@ -414,7 +416,8 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
         print('Time: %s' % to_localtime(archive.ts).strftime('%c'))
         print('Command line:', remove_surrogates(' '.join(archive.metadata[b'cmdline'])))
         print('Number of files: %d' % stats.nfiles)
-        print(stats.print_('This archive:', cache))
+        print(str(stats))
+        print(str(cache))
         return self.exit_code
 
     def do_prune(self, args):
@@ -459,7 +462,8 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
             repository.commit()
             cache.commit()
         if args.stats:
-            logger.info(stats.print_('Deleted data:', cache))
+            logger.info(stats.summary.format(label='Deleted data:', stats=stats))
+            logger.info(str(cache))
         return self.exit_code
 
     def do_upgrade(self, args):

+ 5 - 9
borg/helpers.py

@@ -161,16 +161,12 @@ class Statistics:
         if unique:
             self.usize += csize
 
-    def print_(self, label, cache):
-        buf = str(self) % label
-        buf += "\n"
-        buf += str(cache)
-        return buf
-
-    def __str__(self):
-        return """\
+    summary = """\
                        Original size      Compressed size    Deduplicated size
-%-15s {0.osize_fmt:>20s} {0.csize_fmt:>20s} {0.usize_fmt:>20s}""".format(self)
+{label:15} {stats.osize_fmt:>20s} {stats.csize_fmt:>20s} {stats.usize_fmt:>20s}
+"""
+    def __str__(self):
+        return self.summary.format(stats=self, label='This archive:')
 
     @property
     def osize_fmt(self):

+ 2 - 1
borg/testsuite/helpers.py

@@ -438,6 +438,7 @@ def tests_stats_progress(stats, columns = 80):
 def test_stats_format(stats):
     assert str(stats) == """\
                        Original size      Compressed size    Deduplicated size
-%-15s                 10 B                 10 B                 10 B"""
+This archive:                   10 B                 10 B                 10 B
+"""
     s = "{0.osize_fmt}".format(stats)
     assert s == "10 B"