|
102 | 102 | from __future__ importabsolute_import |
103 | 103 | from __future__ importprint_function |
104 | 104 | fromoptparseimportOptionParser |
105 | | -importos |
106 | 105 | importsys |
107 | 106 |
|
108 | 107 |
|
109 | | -# Look for standalone GN distribution. |
110 | | -defFindGNPath(): |
111 | | -foriinos.environ['PATH'].split(os.pathsep): |
112 | | -ifi.rstrip(os.sep).endswith('gn'): |
113 | | -returni |
114 | | -returnNone |
| 108 | +# This function is copied from build/gn_helpers.py in Chromium. |
| 109 | +defToGNString(value, pretty=False): |
| 110 | +"""Returns a stringified GN equivalent of a Python value. |
115 | 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|. |
116 | 119 |
|
117 | | -try: |
118 | | -# May already be in the import path. |
119 | | -importgn_helpers |
120 | | -exceptImportError: |
121 | | -# Add src/build to import path. |
122 | | -src_dir=os.path.abspath(os.path.join(os.path.dirname(__file__), |
123 | | -os.pardir, os.pardir)) |
124 | | -sys.path.append(os.path.join(src_dir, 'build')) |
125 | | -ifFindGNPath(): |
126 | | -sys.path.append(os.path.join(FindGNPath(), 'build')) |
127 | | -importgn_helpers |
| 120 | + Raises: |
| 121 | + ValueError: |value| cannot be printed to GN. |
| 122 | + """ |
| 123 | + |
| 124 | +# Emits all output tokens without intervening whitespaces. |
| 125 | +defGenerateTokens(v, level): |
| 126 | +ifisinstance(v, str): |
| 127 | +yield'"'+''.join(TranslateToGnChars(v)) +'"' |
| 128 | + |
| 129 | +elifisinstance(v, bool): |
| 130 | +yield'true'ifvelse'false' |
| 131 | + |
| 132 | +elifisinstance(v, int): |
| 133 | +yieldstr(v) |
| 134 | + |
| 135 | +elifisinstance(v, list): |
| 136 | +yield'[' |
| 137 | +fori, iteminenumerate(v): |
| 138 | +ifi>0: |
| 139 | +yield',' |
| 140 | +fortokinGenerateTokens(item, level+1): |
| 141 | +yieldtok |
| 142 | +yield']' |
| 143 | + |
| 144 | +elifisinstance(v, dict): |
| 145 | +iflevel>0: |
| 146 | +yield'{' |
| 147 | +forkeyinsorted(v): |
| 148 | +ifnotisinstance(key, str): |
| 149 | +raiseValueError('Dictionary key is not a string.') |
| 150 | +ifnotkeyorkey[0].isdigit() ornotkey.replace('_', '').isalnum(): |
| 151 | +raiseValueError('Dictionary key is not a valid GN identifier.') |
| 152 | +yieldkey# No quotations. |
| 153 | +yield'=' |
| 154 | +fortokinGenerateTokens(v[key], level+1): |
| 155 | +yieldtok |
| 156 | +iflevel>0: |
| 157 | +yield'}' |
| 158 | + |
| 159 | +else: # Not supporting float: Add only when needed. |
| 160 | +raiseValueError('Unsupported type when printing to GN.') |
| 161 | + |
| 162 | +can_start=lambdatok: tokandtoknotin',}]=' |
| 163 | +can_end=lambdatok: tokandtoknotin',{[=' |
| 164 | + |
| 165 | +# Adds whitespaces, trying to keep everything (except dicts) in 1 line. |
| 166 | +defPlainGlue(gen): |
| 167 | +prev_tok=None |
| 168 | +fori, tokinenumerate(gen): |
| 169 | +ifi>0: |
| 170 | +ifcan_end(prev_tok) andcan_start(tok): |
| 171 | +yield'\n'# New dict item. |
| 172 | +elifprev_tok=='['andtok==']': |
| 173 | +yield' '# Special case for []. |
| 174 | +eliftok!=',': |
| 175 | +yield' ' |
| 176 | +yieldtok |
| 177 | +prev_tok=tok |
| 178 | + |
| 179 | +# Adds whitespaces so non-empty lists can span multiple lines, with indent. |
| 180 | +defPrettyGlue(gen): |
| 181 | +prev_tok=None |
| 182 | +level=0 |
| 183 | +fori, tokinenumerate(gen): |
| 184 | +ifi>0: |
| 185 | +ifcan_end(prev_tok) andcan_start(tok): |
| 186 | +yield'\n'+' '*level# New dict item. |
| 187 | +eliftok=='='orprev_tokin'=': |
| 188 | +yield' '# Separator before and after '=', on same line. |
| 189 | +iftokin']}': |
| 190 | +level-=1 |
| 191 | +# Exclude '[]' and '{}' cases. |
| 192 | +ifint(prev_tok=='[') +int(tok==']') ==1or \ |
| 193 | +int(prev_tok=='{') +int(tok=='}') ==1: |
| 194 | +yield'\n'+' '*level |
| 195 | +yieldtok |
| 196 | +iftokin'[{': |
| 197 | +level+=1 |
| 198 | +iftok==',': |
| 199 | +yield'\n'+' '*level |
| 200 | +prev_tok=tok |
| 201 | + |
| 202 | +token_gen=GenerateTokens(value, 0) |
| 203 | +ret=''.join((PrettyGlueifprettyelsePlainGlue)(token_gen)) |
| 204 | +# Add terminating '\n' for dict |value| or multi-line output. |
| 205 | +ifisinstance(value, dict) or'\n'inret: |
| 206 | +returnret+'\n' |
| 207 | +returnret |
| 208 | + |
| 209 | + |
| 210 | +defTranslateToGnChars(s): |
| 211 | +forcodeins.encode('utf-8'): |
| 212 | +ifcodein (34, 36, 92): # For '"', '$', or '\\'. |
| 213 | +yield'\\'+chr(code) |
| 214 | +elif32<=code<127: |
| 215 | +yieldchr(code) |
| 216 | +else: |
| 217 | +yield'$0x%02X'%code |
128 | 218 |
|
129 | 219 |
|
130 | 220 | defLoadPythonDictionary(path): |
@@ -234,7 +324,7 @@ def main(): |
234 | 324 | else: |
235 | 325 | gn_dict[gn_key] =data[key] |
236 | 326 |
|
237 | | -print(gn_helpers.ToGNString(DeduplicateLists(gn_dict))) |
| 327 | +print(ToGNString(DeduplicateLists(gn_dict))) |
238 | 328 |
|
239 | 329 | if__name__=='__main__': |
240 | 330 | try: |
|
0 commit comments