MCPcopy Create free account
hub / github.com/apache/tvm / _get_formatter

Function _get_formatter

python/tvm/script/highlight.py:99–161  ·  view source on GitHub ↗
(formatter: str | None = None)

Source from the content-addressed store, hash-verified

97
98@functools.lru_cache
99def _get_formatter(formatter: str | None = None):
100 def get_ruff_formatter():
101 if shutil.which("ruff") is None:
102 return None
103
104 def formatter(code_str):
105 proc = subprocess.Popen(
106 ["ruff", "format", "--stdin-filename=TVMScript"],
107 stdin=subprocess.PIPE,
108 stdout=subprocess.PIPE,
109 encoding="utf-8",
110 )
111 stdout, _stderr = proc.communicate(code_str)
112 return stdout
113
114 return formatter
115
116 def get_black_formatter():
117 try:
118 # pylint: disable=import-outside-toplevel
119 import black
120 except ImportError:
121 return None
122
123 def formatter(code_str):
124 return black.format_str(code_str, mode=black.FileMode())
125
126 return formatter
127
128 def get_fallback_formatter():
129 def formatter(code_str):
130 with warnings.catch_warnings():
131 warnings.simplefilter("once", UserWarning)
132 ruff_install_cmd = sys.executable + " -m pip install ruff"
133 black_install_cmd = (
134 sys.executable + ' -m pip install "black==22.3.0" --upgrade --user'
135 )
136 warnings.warn(
137 f"Neither the 'ruff' formatter nor the 'black' formatter is available. "
138 f"To print formatted TVM script, please a formatter. \n"
139 f"To install ruff: {ruff_install_cmd}\n"
140 f"To install black: {black_install_cmd}",
141 category=UserWarning,
142 )
143 return code_str
144
145 return formatter
146
147 # formatter = "black"
148 if formatter is None:
149 options = [get_ruff_formatter, get_black_formatter]
150 elif formatter == "ruff":
151 options = [get_ruff_formatter]
152 elif formatter == "black":
153 options = [get_black_formatter]
154 else:
155 raise ValueError(f"Unknown formatter: {formatter}")
156

Callers 1

_formatFunction · 0.85

Calls 1

get_fallback_formatterFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…