MCPcopy Index your code
hub / github.com/nodejs/node / ToGNString

Function ToGNString

tools/gypi_to_gn.py:109–207  ·  view source on GitHub ↗

Returns a stringified GN equivalent of a Python value. Args: value: The Python value to convert. pretty: Whether to pretty print. If true, then non-empty lists are rendered recursively with one item per line, with indents. Otherwise lists are rendered without new line. R

(value, pretty=False)

Source from the content-addressed store, hash-verified

107
108# This function is copied from build/gn_helpers.py in Chromium.
109def ToGNString(value, pretty=False):
110 """Returns a stringified GN equivalent of a Python value.
111
112 Args:
113 value: The Python value to convert.
114 pretty: Whether to pretty print. If true, then non-empty lists are rendered
115 recursively with one item per line, with indents. Otherwise lists are
116 rendered without new line.
117 Returns:
118 The stringified GN equivalent to |value|.
119
120 Raises:
121 ValueError: |value| cannot be printed to GN.
122 """
123
124 # Emits all output tokens without intervening whitespaces.
125 def GenerateTokens(v, level):
126 if isinstance(v, str):
127 yield '"' + ''.join(TranslateToGnChars(v)) + '"'
128
129 elif isinstance(v, bool):
130 yield 'true' if v else 'false'
131
132 elif isinstance(v, int):
133 yield str(v)
134
135 elif isinstance(v, list):
136 yield '['
137 for i, item in enumerate(v):
138 if i > 0:
139 yield ','
140 for tok in GenerateTokens(item, level + 1):
141 yield tok
142 yield ']'
143
144 elif isinstance(v, dict):
145 if level > 0:
146 yield '{'
147 for key in sorted(v):
148 if not isinstance(key, str):
149 raise ValueError('Dictionary key is not a string.')
150 if not key or key[0].isdigit() or not key.replace('_', '').isalnum():
151 raise ValueError('Dictionary key is not a valid GN identifier.')
152 yield key # No quotations.
153 yield '='
154 for tok in GenerateTokens(v[key], level + 1):
155 yield tok
156 if level > 0:
157 yield '}'
158
159 else: # Not supporting float: Add only when needed.
160 raise ValueError('Unsupported type when printing to GN.')
161
162 can_start = lambda tok: tok and tok not in ',}]='
163 can_end = lambda tok: tok and tok not in ',{[='
164
165 # Adds whitespaces, trying to keep everything (except dicts) in 1 line.
166 def PlainGlue(gen):

Callers 1

mainFunction · 0.85

Calls 2

GenerateTokensFunction · 0.85
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…