mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
ui: disable colorama on write_json when highlighting is disabled (#9186)
On a large json output (say ~300MB), this saves about ~6 seconds of time.
This commit is contained in:
+15
-1
@@ -13,6 +13,8 @@ from typing import (
|
||||
Union,
|
||||
)
|
||||
|
||||
import colorama
|
||||
|
||||
from dvc.utils.objects import cached_property
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -27,6 +29,15 @@ if TYPE_CHECKING:
|
||||
from dvc.ui.table import Headers, Styles, TableData
|
||||
|
||||
|
||||
@contextmanager
|
||||
def disable_colorama():
|
||||
colorama.deinit()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
colorama.reinit()
|
||||
|
||||
|
||||
class Formatter:
|
||||
def __init__(
|
||||
self, theme: Optional[Dict] = None, defaults: Optional[Dict] = None
|
||||
@@ -121,7 +132,10 @@ class Console:
|
||||
sort_keys=sort_keys,
|
||||
)
|
||||
if not highlight:
|
||||
return self.write(json.text, stderr=stderr)
|
||||
# we don't need colorama to try to strip ansi codes
|
||||
# when highlighting is disabled
|
||||
with disable_colorama():
|
||||
return self.write(json.text, stderr=stderr)
|
||||
return self.rich_print(json, stderr=stderr, soft_wrap=True)
|
||||
|
||||
def rich_print(
|
||||
|
||||
Reference in New Issue
Block a user