public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH] Bundle a minimalistic derivation of Python's formatter module (bug 547732)
@ 2015-04-25 20:37 Zac Medico
  2015-04-29  0:07 ` Brian Dolbec
  0 siblings, 1 reply; 5+ messages in thread
From: Zac Medico @ 2015-04-25 20:37 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Zac Medico

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
---
 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(-)
 create mode 100644 pym/portage/util/formatter.py

diff --git a/bin/repoman b/bin/repoman
index e9c89c2..00457fa 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)
+
-- 
2.3.5



^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-05-06 18:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-25 20:37 [gentoo-portage-dev] [PATCH] Bundle a minimalistic derivation of Python's formatter module (bug 547732) Zac Medico
2015-04-29  0:07 ` Brian Dolbec
2015-04-29  1:48   ` Zac Medico
2015-05-06 17:39     ` Zac Medico
2015-05-06 18:03       ` Brian Dolbec

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox