forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1422302 - Create python/mozterm for sharing terminal blessings ac…
…ross modules r=gps This is a new module that will provide a place to store some common abstractions around the 'blessings' module. The main entrypoint is: from mozterm import Terminal term = Terminal() If blessings is available, this will return a blessings.Terminal() object. If it isn't available, or something went wrong on import, this will return a NullTerminal() object, which is a drop-in replacement that does no formatting. MozReview-Commit-ID: 6c63svm4tM5
- Loading branch information
Showing
10 changed files
with
136 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from .terminal import Terminal, NullTerminal # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
import os | ||
import sys | ||
|
||
|
||
class NullTerminal(object): | ||
"""Replacement for `blessings.Terminal()` that does no formatting.""" | ||
number_of_colors = 0 | ||
width = 0 | ||
height = 0 | ||
|
||
def __init__(self, stream=None, **kwargs): | ||
self.stream = stream or sys.__stdout__ | ||
try: | ||
self.is_a_tty = os.isatty(self.stream.fileno()) | ||
except: | ||
self.is_a_tty = False | ||
|
||
class NullCallableString(unicode): | ||
"""A dummy callable Unicode stolen from blessings""" | ||
def __new__(cls): | ||
new = unicode.__new__(cls, '') | ||
return new | ||
|
||
def __call__(self, *args): | ||
if len(args) != 1 or isinstance(args[0], int): | ||
return '' | ||
return args[0] | ||
|
||
def __getattr__(self, attr): | ||
return self.NullCallableString() | ||
|
||
|
||
def Terminal(raises=False, disable_styling=False, **kwargs): | ||
if disable_styling: | ||
return NullTerminal(**kwargs) | ||
|
||
try: | ||
import blessings | ||
except Exception: | ||
if raises: | ||
raise | ||
return NullTerminal(**kwargs) | ||
return blessings.Terminal(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[DEFAULT] | ||
subsuite = mozterm | ||
|
||
[test_terminal.py] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
import os | ||
import sys | ||
|
||
import mozunit | ||
import pytest | ||
|
||
from mozterm import Terminal, NullTerminal | ||
|
||
|
||
def test_terminal(): | ||
blessings = pytest.importorskip('blessings') | ||
term = Terminal() | ||
assert isinstance(term, blessings.Terminal) | ||
|
||
term = Terminal(disable_styling=True) | ||
assert isinstance(term, NullTerminal) | ||
|
||
del sys.modules['blessings'] | ||
orig = sys.path[:] | ||
for path in orig: | ||
if 'blessings' in path: | ||
sys.path.remove(path) | ||
|
||
term = Terminal() | ||
assert isinstance(term, NullTerminal) | ||
|
||
with pytest.raises(ImportError): | ||
term = Terminal(raises=True) | ||
|
||
sys.path = orig | ||
|
||
|
||
def test_null_terminal(): | ||
term = NullTerminal() | ||
assert term.red("foo") == "foo" | ||
assert term.red == "" | ||
assert term.color(1) == "" | ||
assert term.number_of_colors == 0 | ||
assert term.width == 0 | ||
assert term.height == 0 | ||
assert term.is_a_tty == os.isatty(sys.stdout.fileno()) | ||
|
||
|
||
if __name__ == '__main__': | ||
mozunit.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters