commit:     971802f170e43fd36241decfa63e527dc1b3cf4f
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sat Apr 25 20:31:15 2015 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Wed May  6 18:19:01 2015 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=971802f1

Bundle a minimalistic derivation of Python's formatter module (bug 547732)

Python's formatter module is scheduled for removal in Python 3.6, so
replace it with a minimalistic derivation.

X-Gentoo-Bug: 547732
X-Gentoo-Bug-url: https://bugs.gentoo.org/show_bug.cgi?id=547732
Acked-by: Brian Dolbec <dolsen <AT> gentoo.org>

 bin/repoman                     |  2 +-
 pym/_emerge/JobStatusDisplay.py |  4 +--
 pym/portage/output.py           |  4 +--
 pym/portage/util/formatter.py   | 69 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 5 deletions(-)

diff --git a/bin/repoman b/bin/repoman
index 37e11b2..a758aea 100755
--- a/bin/repoman
+++ b/bin/repoman
@@ -11,7 +11,6 @@ from __future__ import print_function, unicode_literals
 import codecs
 import copy
 import errno
-import formatter
 import io
 import logging
 import re
@@ -56,6 +55,7 @@ except (ImportError, SystemError, RuntimeError, Exception):
 from portage import os
 from portage import _encodings
 from portage import _unicode_encode
+import portage.util.formatter as formatter
 import repoman.checks
 from repoman.checks import run_checks
 from repoman.check_missingslot import check_missingslot

diff --git a/pym/_emerge/JobStatusDisplay.py b/pym/_emerge/JobStatusDisplay.py
index 9f6f09b..b8e142a 100644
--- a/pym/_emerge/JobStatusDisplay.py
+++ b/pym/_emerge/JobStatusDisplay.py
@@ -1,14 +1,14 @@
-# Copyright 1999-2013 Gentoo Foundation
+# Copyright 1999-2015 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from __future__ import unicode_literals
 
-import formatter
 import io
 import sys
 import time
 
 import portage
+import portage.util.formatter as formatter
 from portage import os
 from portage import _encodings
 from portage import _unicode_encode

diff --git a/pym/portage/output.py b/pym/portage/output.py
index 7846627..bb7542b 100644
--- a/pym/portage/output.py
+++ b/pym/portage/output.py
@@ -1,4 +1,4 @@
-# Copyright 1998-2014 Gentoo Foundation
+# Copyright 1998-2015 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from __future__ import division
@@ -7,7 +7,6 @@ __docformat__ = "epytext"
 
 import errno
 import io
-import formatter
 import re
 import subprocess
 import sys
@@ -16,6 +15,7 @@ import portage
 portage.proxy.lazyimport.lazyimport(globals(),
        'portage.util:writemsg',
 )
+import portage.util.formatter as formatter
 
 from portage import os
 from portage import _encodings

diff --git a/pym/portage/util/formatter.py b/pym/portage/util/formatter.py
new file mode 100644
index 0000000..ce6799e
--- /dev/null
+++ b/pym/portage/util/formatter.py
@@ -0,0 +1,69 @@
+# Copyright 2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+#
+# This is a minimalistic derivation of Python's deprecated formatter module,
+# supporting only the methods related to style, literal data, and line breaks.
+
+import sys
+
+
+class AbstractFormatter(object):
+       """The standard formatter."""
+
+       def __init__(self, writer):
+               self.writer = writer            # Output device
+               self.style_stack = []           # Other state, e.g. color
+               self.hard_break = True          # Have a hard break
+
+       def add_line_break(self):
+               if not self.hard_break:
+                       self.writer.send_line_break()
+               self.hard_break = True
+
+       def add_literal_data(self, data):
+               if not data: return
+               self.hard_break = data[-1:] == '\n'
+               self.writer.send_literal_data(data)
+
+       def push_style(self, *styles):
+               for style in styles:
+                       self.style_stack.append(style)
+               self.writer.new_styles(tuple(self.style_stack))
+
+       def pop_style(self, n=1):
+               del self.style_stack[-n:]
+               self.writer.new_styles(tuple(self.style_stack))
+
+
+class NullWriter(object):
+       """Minimal writer interface to use in testing & inheritance.
+
+       A writer which only provides the interface definition; no actions are
+       taken on any methods.  This should be the base class for all writers
+       which do not need to inherit any implementation methods.
+       """
+       def __init__(self): pass
+       def flush(self): pass
+       def new_styles(self, styles): pass
+       def send_line_break(self): pass
+       def send_literal_data(self, data): pass
+
+
+class DumbWriter(NullWriter):
+       """Simple writer class which writes output on the file object passed in
+       as the file parameter or, if file is omitted, on standard output.
+       """
+
+       def __init__(self, file=None, maxcol=None):
+               NullWriter.__init__(self)
+               self.file = file or sys.stdout
+
+       def flush(self):
+               self.file.flush()
+
+       def send_line_break(self):
+               self.file.write('\n')
+
+       def send_literal_data(self, data):
+               self.file.write(data)
+

Reply via email to