From: Zac Medico <zmedico@gentoo.org>
To: gentoo-portage-dev@lists.gentoo.org
Cc: Zac Medico <zmedico@gentoo.org>
Subject: [gentoo-portage-dev] [PATCH] Bundle a minimalistic derivation of Python's formatter module (bug 547732)
Date: Sat, 25 Apr 2015 13:37:10 -0700 [thread overview]
Message-ID: <1429994230-3771-1-git-send-email-zmedico@gentoo.org> (raw)
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
next reply other threads:[~2015-04-25 20:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-25 20:37 Zac Medico [this message]
2015-04-29 0:07 ` [gentoo-portage-dev] [PATCH] Bundle a minimalistic derivation of Python's formatter module (bug 547732) Brian Dolbec
2015-04-29 1:48 ` Zac Medico
2015-05-06 17:39 ` Zac Medico
2015-05-06 18:03 ` Brian Dolbec
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1429994230-3771-1-git-send-email-zmedico@gentoo.org \
--to=zmedico@gentoo.org \
--cc=gentoo-portage-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox